-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
96 lines (78 loc) · 2.76 KB
/
run.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
import mysql.connector
import configparser
import os
from datetime import datetime
import time
import shutil
### ALL PARAMS IS GLOBAL ###
############################
# set date
now=datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
#get config
config = configparser.ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), "config", "app.ini"))
# config params
hostname=config["server"]["hostname"]
username=config["server"]["username"]
password=config["server"]["password"]
excludes=config["database"]["exclude"]
# backup folder validity
validity_format_options = {"second":1,"minute":60,"day":86400,"week":604800,"month":2592000,"year":31536000}
# get from config
validity_format_config = str(config["validity"]["format"])
validity_value_config = int(config["validity"]["value"])
# build validity value as integer
if validity_format_config in validity_format_options:
validity = validity_value_config * validity_format_options[validity_format_config]
else:
# set default as "day"
validity_format_config = "day"
validity = validity_value_config * validity_format_options["day"]
# backup params
base_path = os.path.join(os.path.dirname(__file__), "backup")
backup_path = os.path.join(base_path, now)
# mysql connection
mysql_conn = mysql.connector.connect(host=hostname,username=username,password=password)
### END PARAMS ####
###################
# clean old folder
for f in os.listdir(base_path):
# get full path
f=os.path.join(base_path, f)
# remove older than validity params
if os.stat(f).st_mtime < time.time() - validity:
if os.path.isdir(f):
print("deleting folder older than " + str(validity_value_config) + " " + validity_format_config + " : " + f + "...")
shutil.rmtree(f)
print("folder " + f + " has been deleted.\n")
# create folder by date
os.mkdir(backup_path)
# get databases
def getDatabases():
# mysql cursor
cur = mysql_conn.cursor()
# query
sql_query = cur.execute("SHOW DATABASES")
# result
results = cur.fetchall()
# output
output = []
for result in results:
dbname = "".join(result)
if dbname not in excludes:
output.append(dbname)
# return dbs
print("total databases to backup = " + str(len(output)) + "\n")
return output
# backup db
def backupDatabases(databases):
# run query
for db in databases:
cmd="mysqldump --set-gtid-purged=OFF -h " + hostname + " -u " + username + " -p" + password + " " + db + " > " + backup_path + "/" + db + ".sql"
print("backup database " + db + " in progress...")
os.system(cmd)
print("backup database " + db + " done.")
print("\nall databases has been backup.")
## RUN SCRIPT ##
all_databases = getDatabases()
backupDatabases(all_databases)