-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_full_volume_backup.py
90 lines (56 loc) · 1.75 KB
/
mysql_full_volume_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
#!/usr/bin/python
#-*- coding=utf-8 -*-
import MySQLdb
import os
import time
# DB Config
DB_HOST = '127.0.0.1'
DB_USER = 'root'
DB_PASSWD = '123456'
# Driectory Path
DIRECTORY_BASE = "/home/xxxx/test/backup/"
CURRENT_POSITION = time.strftime("%Y%m%d")
# Init Databases store list
databases_list = []
system_database = ['mysql', 'infomation_schema', 'performance_schema']
start_time = time.time()
"""
Connect Mysql DB & Get All Databases
"""
conn = MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWD)
cursor = conn.cursor()
try:
cursor.execute("show databases")
result = cursor.fetchall()
for dbname in result:
if dbname[0] in system_database:
continue;
databases_list.append(dbname[0])
except MySQLdb.Error, e:
print "MySQLdb Error %d: %s" %(e.args[0], e.args[1])
print "Sorry, Error! Error! Please try again ..."
finally:
conn.close()
cursor.close()
# MAIN PROGRAM START
print len(databases_list)
if len(databases_list) == 0:
print "No Databases Need Backup, bye ~ bye"
exit(1)
storage_path = DIRECTORY_BASE + CURRENT_POSITION
if not os.path.exists(storage_path):
os.mkdir(storage_path)
print "Wow~ Successfully created directory", storage_path
for dbname in databases_list:
print dbname
file_name = dbname + ".sql"
execute_command = "mysqldump -h%s -u%s -p%s %s > %s" %(DB_HOST, DB_USER, DB_PASSWD, dbname, storage_path + '/' + file_name)
if os.system(execute_command) == 0:
print "%s backup is complete!" %dbname
else:
print "Sorry! %s is Backup Failed" %dbname
end_time = time.time()
print "Yeah! Successfully Backup All Databases"
print "Backup time is %s" %(time.clock())
running_time = start_time - end_time
print "System use time is %s" %running_time