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.
This commit is contained in:
Steve Dogiakos 2023-12-30 14:35:12 -07:00 committed by GitHub
commit 68ca48e1cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 0 deletions

31
split_sql_dump.py Normal file
View File

@ -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')