Skip to content

Instantly share code, notes, and snippets.

@Egor3f
Created December 15, 2019 07:15
Show Gist options
  • Save Egor3f/ac0444e5aace3e7e29515035f5c09bfa to your computer and use it in GitHub Desktop.
Save Egor3f/ac0444e5aace3e7e29515035f5c09bfa to your computer and use it in GitHub Desktop.
Split mysql dump to multiple files by database (python3)
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