1 | |
---|
2 | import tempfile |
---|
3 | import shutil |
---|
4 | import os |
---|
5 | import subprocess |
---|
6 | import time |
---|
7 | |
---|
8 | class MysqlManager: |
---|
9 | |
---|
10 | |
---|
11 | def __init__(self): |
---|
12 | |
---|
13 | pass |
---|
14 | |
---|
15 | #def init |
---|
16 | |
---|
17 | def get_time(self): |
---|
18 | |
---|
19 | return get_backup_name("MysqlManager") |
---|
20 | |
---|
21 | #def get_time |
---|
22 | |
---|
23 | |
---|
24 | def backup(self,dir="/backup/"): |
---|
25 | |
---|
26 | try: |
---|
27 | |
---|
28 | file_path=dir+"/"+self.get_time() |
---|
29 | tmp_dir=tempfile.mkdtemp() |
---|
30 | |
---|
31 | ret=os.system("mysql_root_passwd -g") |
---|
32 | |
---|
33 | if ret!=0: |
---|
34 | os.system("mysql_root_passwd -i") |
---|
35 | |
---|
36 | cmd="mysqldump -u root -p$(mysql_root_passwd -g) --routines --all-databases --flush-privileges > %s/backup.sql"%(tmp_dir) |
---|
37 | os.system(cmd) |
---|
38 | |
---|
39 | if os.path.exists(tmp_dir+"/backup.sql"): |
---|
40 | tar=tarfile.open(file_path,"w:gz") |
---|
41 | tar.add(tmp_dir+"/backup.sql",arcname="backup.sql") |
---|
42 | if os.path.exists("/root/.my.cnf"): |
---|
43 | tar.add("/root/.my.cnf",arcname="my.cnf") |
---|
44 | tar.close() |
---|
45 | |
---|
46 | os.system("chmod 600 %s"%file_path) |
---|
47 | |
---|
48 | return [True,file_path] |
---|
49 | |
---|
50 | except Exception as e: |
---|
51 | return [False,str(e)] |
---|
52 | |
---|
53 | return [False,"Mysqldump failed"] |
---|
54 | |
---|
55 | #def test |
---|
56 | |
---|
57 | def restore(self,file_path=None): |
---|
58 | |
---|
59 | if file_path==None: |
---|
60 | for f in sorted(os.listdir("/backup"),reverse=True): |
---|
61 | if "MysqlManager" in f: |
---|
62 | file_path="/backup/"+f |
---|
63 | break |
---|
64 | |
---|
65 | if file_path==None: |
---|
66 | return [False,"No backup file found"] |
---|
67 | |
---|
68 | try: |
---|
69 | |
---|
70 | if os.path.exists(file_path): |
---|
71 | |
---|
72 | tmp_dir=tempfile.mkdtemp() |
---|
73 | tar=tarfile.open(file_path,"r") |
---|
74 | tar.extractall(tmp_dir) |
---|
75 | tar.close() |
---|
76 | #os.system("chmod 600 %s"%file_path) |
---|
77 | |
---|
78 | if os.path.exists(tmp_dir+"/my.cnf"): |
---|
79 | shutil.copy(tmp_dir+"/my.cnf","/root/.my.cnf") |
---|
80 | os.system("chmod 600 /root/.my.cnf") |
---|
81 | os.system("mysql_root_passwd -i") |
---|
82 | |
---|
83 | if os.path.exists(tmp_dir+"/backup.sql"): |
---|
84 | cmd="mysql -u root -p$(mysql_root_passwd -g) < %s"%(tmp_dir+"/backup.sql") |
---|
85 | os.system(cmd) |
---|
86 | |
---|
87 | |
---|
88 | return [True,""] |
---|
89 | |
---|
90 | except Exception as e: |
---|
91 | print e |
---|
92 | return [False,file_path + ": " + str(e)] |
---|
93 | |
---|
94 | #def test |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | #class CupsManager |
---|
100 | |
---|
101 | if __name__=="__main__": |
---|
102 | |
---|
103 | m=MysqlManager() |
---|
104 | |
---|
105 | m.backup() |
---|
106 | |
---|
107 | |
---|
108 | |
---|