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:
commit
68ca48e1cc
|
@ -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')
|
Loading…
Reference in New Issue