forked from mgckind/desaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.py
66 lines (57 loc) · 1.83 KB
/
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
import Settings
import time
import schedule
#import config.mysqlconfig as ms
from subprocess import Popen, PIPE
import MySQLdb as mydb
import yaml
import sys
def dump():
command = "mysqldump -h {host} -P {port} -u {user} -p{passwd} {db} > {sqlfile}"
with open('config/mysqlconfig.yaml', 'r') as cfile:
inputs = yaml.load(cfile)['mysql']
inputs['sqlfile'] = Settings.DBFILE2
#print(command.format(**inputs))
print('Saving database')
Popen(command.format(**inputs), shell=True, stdout=PIPE, stderr=PIPE)
def restore():
try:
with open('config/mysqlconfig.yaml', 'r') as cfile:
conf = yaml.load(cfile)['mysql']
conf.pop('db', None)
con = mydb.connect(**conf)
cur = con.cursor()
except:
return False
try:
con.select_db('des')
print('des Database does exists')
return True
except mydb.OperationalError:
_, err, _ = sys.exc_info()
if err.args[0] == 1049:
print('Creating des DB')
cur.execute('create database {0}'.format('des'))
con.commit()
con.close()
print('Restoring des DB')
command = "mysql -h {host} -P {port} -u {user} -p{passwd} {db} < {sqlfile}"
with open('config/mysqlconfig.yaml', 'r') as cfile:
inputs = yaml.load(cfile)['mysql']
inputs['sqlfile'] = Settings.DBFILE2
# print(command.format(**inputs))
Popen(command.format(**inputs), shell=True, stdout=PIPE, stderr=PIPE)
return False
except Exception:
return False
def job():
print('Update time')
print('-----------\n')
conti = restore()
if conti:
dump()
if __name__ == '__main__':
schedule.every().hour.do(job)
while True:
schedule.run_pending()
time.sleep(1)