Skip to content

Instantly share code, notes, and snippets.

@luren5
Last active August 29, 2015 14:19
Show Gist options
  • Save luren5/6f00103625b88653195d to your computer and use it in GitHub Desktop.
Save luren5/6f00103625b88653195d to your computer and use it in GitHub Desktop.
#dbbackup.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import time
import sys
#db config
username = ""
password = ""
port = ""
host = ""
def invalidDB():
print "please input the db you want dump!\n"
exit();
def help():
print '''help:\n
---only table structure: python dbbackup.py dbname\n
---with the data: python dbbackup.py dbname data
'''
exit()
def dumpDB(dbname, with_data):
print "starting:%s" % time.strftime("%Yi-%m-%d %H:%M:%S")
comm = "mysqldump -u%s -p%s -P%s -h%s" % (username, password, port, host)
if with_data :
comm = comm + " --opt"
comm = comm + " -d %s > %s_%s.sql" % (dbname, dbname, time.strftime("%Y%m%d_%H%M"))
res = os.system(comm)
if res == 0:
print "Complete!"
else :
print "Error! the error info as above, suggest check your DB config"
print "ending:%s" % time.strftime("%Yi-%m-%d %H:%M:%S")
exit()
if __name__ == "__main__":
argv_num = len(sys.argv)
if argv_num < 2:
invalidDB()
elif argv_num == 2:
dumpDB(sys.argv[1], False);
elif argv_num == 3:
if sys.argv[2] == "data":
dumpDB(sys.argv[1], True)
else :
help()
else:
help()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment