This repository has been archived by the owner on Nov 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql-backup.py
executable file
·100 lines (78 loc) · 2.64 KB
/
mysql-backup.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
import sys
import os
import getopt
import time
import tempfile
import shutil
from datetime import date, datetime, timedelta
config = {'login-path': '', 'databases': '', 'directory': '', 'max-days': 15}
def usage():
print "Dumping structure and contents of MySQL databases."
print "usage: mysql-backup [options] \n"
print "--help : Display this help message and exit."
print "-v, --version : Output version information and exit."
print "-l, --login-path=name : Login path name."
print "-D, --databases=name : databases name to dump."
print "-d, --directory=path : Backup directory."
print "-m, --max-days=days : Maximum days of backup (default 15 days)."
def version():
print "Mysql Backup v2"
def export():
databases = config['databases'].split()
if(len(databases) == 0):
print "You have to specifie which databases you want to export"
sys.exit()
for database in databases:
args = ""
if(config['login-path'] != ""):
args = args+" --login-path='"+config['login-path']+"'"
args = args+" "+database
tmpFile, tmpFilePath = tempfile.mkstemp()
args = args+" > "+tmpFilePath
cmd = "mysqldump{0}".format(args)
if(os.system(cmd) == 0):
path = os.path.join(config["directory"], database)
path = os.path.join(path, time.strftime("%d-%m-%Y"))
if(not(os.path.exists(path))):
os.makedirs(path)
path = os.path.join(path, time.strftime("%H:%M:%S")+".sql")
shutil.move(tmpFilePath, path)
if os.path.isfile(path):
cleanup(database)
print "Export successful for database '"+database+"'"
else:
print "Export failed for database '"+database+"'"
else:
print "Export failed for database '"+database+"'"
def cleanup(database):
min_date = date.today() - timedelta(days=config["max-days"])
path = os.path.join(config["directory"], database)
for day in os.listdir(path):
full_date = datetime.strptime(day, "%d-%m-%Y").date()
if full_date < min_date:
shutil.rmtree(os.path.join(path, day))
def options():
try:
options, args = getopt.getopt(sys.argv[1:], "vl:B:d:m:", ["version", "login-path=", "databases=", "directory=", "max-days=", "help"])
except getopt.GetoptError:
usage()
sys.exit(2)
else:
for key, value in options:
if(key in ['-v', '--version']):
version()
sys.exit()
elif(key == "--help"):
usage()
sys.exit()
elif(key in ['-l', '--login-path']):
config["login-path"] = value
elif(key in ['-B', '--databases']):
config["databases"] = value
elif(key in ['-d', '--directory']):
config["directory"] = value
elif(key in ['-m', '--max-days']):
config["max-days"] = int(value)
options()
export()