Last active
December 30, 2015 23:59
-
-
Save affix/7904372 to your computer and use it in GitHub Desktop.
dumpit.py a python alternative to mysqldump. I wrote this after an innodb corruption and mysqdump kept crashing for me. This seemed to export all the data correctly although a little slower. It seems to be more fault tollerant.
This file contains hidden or 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
#!/usr/bin/env python | |
# | |
# dumpit.py | |
# An Alternative to mysqldump written in Python | |
# Requires MySQLdb Python Module. | |
# | |
# Author : Keiran 'Affix' Smith | |
# Email : [email protected] | |
# Web : http://affix.me | |
# Copyright 2013 Keiran Smith | |
# License : GNU/GPLv3 | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
import MySQLdb, sys | |
try: | |
dbhost = sys.argv[1] | |
dbuser = sys.argv[2] | |
dbpass = sys.argv[3] | |
dbname = sys.argv[4] | |
except: | |
print "Usage : " + sys.argv[0] + " <dbhost> <dbuser> <dbpass> <dbname>" | |
sys.exit(1) | |
try: | |
con = MySQLdb.connect(dbhost, dbuser, dbpass, dbname) | |
cursor = con.cursor() | |
cursor.execute("SHOW TABLES") | |
dump = open(dbname + "-INSERT-ONLY.sql", "a+") # Open Dump File | |
for res in cursor.fetchall(): | |
print "Dumping Table : %s" % res[0] | |
dbtable = res[0] | |
## | |
# Retrieve Table Columns | |
## | |
cursor.execute("SHOW COLUMNS IN %s" % res[0]) | |
cols = "" | |
for col in cursor.fetchall(): | |
cols += ",\'" + col[0] + "\'" | |
cols = cols[1:] # Strip the leading comma | |
## | |
# Fetch the Table Data | |
## | |
curs = con.cursor() | |
curs.execute("SELECT * FROM " + dbtable) | |
queries = "" | |
for result in curs.fetchall(): | |
queries += "INSERT INTO " + dbtable + " (" + cols + ") VALUES "+ str(result) + ";\n" | |
dump.write("\n##\n# Dump for "+ dbtable +"\n##\n") | |
dump.write(queries) # Write to the dump file | |
except MySQLdb.Error, e: | |
print "Error %d: %s" % (e.args[0],e.args[1]) | |
finally: | |
print "Export of Data from %s Complete" % dbname |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment