Skip to content

Instantly share code, notes, and snippets.

@tommyready
Created October 16, 2017 17:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommyready/96f06d67f07464b10d1e4a97fc1dc8c8 to your computer and use it in GitHub Desktop.
Save tommyready/96f06d67f07464b10d1e4a97fc1dc8c8 to your computer and use it in GitHub Desktop.
Using Python to dump a Mysql database and import into another Mysql database
#!/usr/bin/env python
import ConfigParser
import os
import time
import getpass
def combine_files(self,file1,file2):
with open('file1', 'w') as outfile:
with open(file2) as infile:
for line in infile:
outfile.write(line)
def get_dbcopy():
script_dir = os.path.dirname(os.path.realpath(__file__))
print "Enter origin database user:"
origin_user = raw_input()
print "Enter origin database password: (Password will not be visible)"
origin_password = getpass.getpass()
print "Enter origin database host:"
origin_host = raw_input()
print "Enter origin database: (Which database to dump"
origin_database = raw_input()
print "Enter destination database user:"
dest_user = raw_input()
print "Enter destination database password: (Password will not be visible)"
dest_password = getpass.getpass()
print "Enter destination database host: (leave blank for same host)"
dest_host = raw_input()
if(not dest_host):
dest_host = origin_host
print "Enter destination database name:"
dest_database = raw_input()
print "Include routines? y/n (leave blank for y)"
include_routines = raw_input()
if(not include_routines):
include_routines = "y"
filestamp = time.strftime('%Y-%m-%d-%I:%M')
dump_file = script_dir+"/"+origin_database+"_"+filestamp+".sql"
if(include_routines.lower() == "y" or include_routines == ''):
os.popen("mysqldump -u %s --password%s -h %s --default-character-set=utf8 --routines --ignore-table=%s.log %s --result-file $s" % (origin_user,password,origin_host,origin_database,origin_database,dump_file))
else:
os.popen("mysqldump -u %s --password%s -h %s --default-character-set=utf8 --ignore-table=%s.log %s --result-file $s" % (origin_user,origin_password,origin_host,origin_database,origin_database,dump_file))
print "\n-- The dump file has been created @ "+dump_file+" --"
sql_update_file = script_dir+"/"+database+"_updates.sql";
#if sql_update_file exists combine with sql dump file
if(os.path.isfile(sql_update_file)):
print "\n-- combing the dump file with: "+sql_update_file
combine_files(dump_file,sql_update_file)
#import mysql dump file
os.popoen("mysql -u %s --password%s -h %s --default-character-set=utf8 %s < %s" % (dest_user,dest_password,dest_host,dest_database,dump_file))
if __name__=="__main__":
get_dbcopy()
@abubelinha
Copy link

abubelinha commented Dec 7, 2021

Hi. I don’t see how combine_files() work.
Does it take 2 or 3 parameters?
What is “self”?
What is “sql_update_file”?
Thanks
@abubelinha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment