Created
December 15, 2019 07:15
-
-
Save Egor3f/ac0444e5aace3e7e29515035f5c09bfa to your computer and use it in GitHub Desktop.
Split mysql dump to multiple files by database (python3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, re | |
fileName = sys.argv[1] | |
with open(fileName, 'r', encoding='utf-8') as f: | |
firstSection = '' | |
currentFile = None | |
for line in f: | |
if line.startswith('CREATE DATABASE'): | |
if currentFile: | |
currentFile.close() | |
currentDatabase = re.search('`(.*?)`', line).group(1).strip() | |
currentFile = open(fileName + '_' + currentDatabase + '.sql', 'w', encoding='utf-8') | |
currentFile.write('-- Mysql Dump Splitter by Egor Aristov (c) 2019 efprojects.com\n\n') | |
currentFile.write(firstSection) | |
currentFile.write(line) | |
else: | |
if currentFile: | |
currentFile.write(line) | |
else: | |
firstSection += line | |
if currentFile: | |
currentFile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment