반응형
import re
import argparse
def extract_table_data(input_file, output_file, table_name):
with open(input_file, 'r', encoding='utf-8') as infile, open(output_file, 'w', encoding='utf-8') as outfile:
in_table = False
table_content = []
for line in infile:
if line.strip().startswith(f"DROP TABLE IF EXISTS `{table_name}`"):
in_table = True
if in_table:
table_content.append(line)
if line.strip() == "UNLOCK TABLES;" and in_table:
in_table = False
outfile.writelines(table_content)
table_content = []
def main():
parser = argparse.ArgumentParser(description="Extract specific table data from MySQL backup SQL file.")
parser.add_argument("input_file", help="Input SQL backup file path")
parser.add_argument("output_file", help="Output file path for extracted data")
parser.add_argument("table_name", help="Name of the table to extract")
args = parser.parse_args()
extract_table_data(args.input_file, args.output_file, args.table_name)
print(f"Extraction complete. Data for table '{args.table_name}' has been saved to '{args.output_file}'.")
if __name__ == "__main__":
main()
대용량 풀백업받은 sql파일에서 특정 테이블 관련 부분만 별도의 파일로 저장하는 파이썬 소스입니다.
반응형