-
Notifications
You must be signed in to change notification settings - Fork 2
/
backup_db.py
executable file
·38 lines (29 loc) · 1004 Bytes
/
backup_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python2.7
import config_grunt
import subprocess
import os
mysql_config = config_grunt.config_dict["mysql"]
print "dumping db %s to file %s" % (
mysql_config["db"], mysql_config["dumpfile"]
)
with open(mysql_config["dumpfile"], "w") as f: # w = truncate
try:
status = subprocess.call([
"mysqldump",
"-B", mysql_config["db"],
"-u", mysql_config["user"],
"-p%s" % mysql_config["passwd"],
# export binary data in hex, since raw form may fail. thanks to
# stackoverflow.com/a/31715391/339874
"--hex-blob"
], stdout = f)
except KeyboardInterrupt:
print "\n\nctrl-c: exiting before the db dump is complete\n"
exit(0)
if status is not 0:
raise Exception("the db dump failed")
print """finished dumping the db
now run
7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on %s.sql.7z %s.sql
to compress it
""" % (mysql_config["dumpfile"], mysql_config["dumpfile"])