From 68ca48e1cc346a6bb9a66e1fa27b6b8214677faa Mon Sep 17 00:00:00 2001 From: Steve Dogiakos Date: Sat, 30 Dec 2023 14:35:12 -0700 Subject: [PATCH] Add SQL dump splitter script This commit adds a Python script designed to split a large SQL dump file into separate files, each corresponding to a different database. The script reads through the SQL dump, identifies database sections, and creates new SQL files named after each database. This utility is particularly useful for managing and organizing large SQL dumps with multiple databases. --- split_sql_dump.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 split_sql_dump.py diff --git a/split_sql_dump.py b/split_sql_dump.py new file mode 100644 index 0000000..7e93143 --- /dev/null +++ b/split_sql_dump.py @@ -0,0 +1,31 @@ +import re + +def split_sql_dump(dump_file): + try: + with open(dump_file, 'r', encoding='utf-8') as file: + current_file = None + for line in file: + # Check for the start of a new database + match = re.search(r'-- Database: `(.+?)`', line) + if match: + # Close the current file if it's open + if current_file is not None: + current_file.close() + + # Open a new file named after the database + db_name = match.group(1) + current_file = open(f"{db_name}.sql", 'w', encoding='utf-8') + + # Write the line to the current file + if current_file is not None: + current_file.write(line) + + # Close the last opened file + if current_file is not None: + current_file.close() + + except IOError as e: + print(f"An error occurred: {e}") + +# Using 'localhost.sql' as the SQL dump file +split_sql_dump('localhost.sql')