-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.py
118 lines (104 loc) · 3.89 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import tarfile
import ftplib
import gzip
import argparse
import os
from common import run_script, mysql_connect, get_config, get_hosts, mkdir_recursive
def add_backup_record(con, user, filename, uploaded, folder, type):
query = "INSERT INTO `vhosts`.`backups` (`host`, `filename`, `localpath`, `uploaded`, `remotepath`, `type`)" \
"VALUES ('%s', '%s', '%s', '%s', NULL, '%s');" % (
user['id'], filename, folder + filename, int(uploaded), type)
con.execute(query)
def mysql_dump(auth_data, user, folder):
now_date = str(datetime.date.today())
dump = run_script("mysqldump -u %s -p%s -h %s %s" %
(auth_data['mysql_username'],
auth_data['mysql_password'],
auth_data['mysql_host'],
user['name']), "")
filename = user['name'] + "." + now_date + ".sql.gz"
f = gzip.open(folder + filename, 'wb')
f.write(dump)
f.close()
return filename
def files_dump(user, folder):
now_date = str(datetime.date.today())
filename = user['name'] + "." + now_date + ".tar.gz"
tar = tarfile.open(folder + filename, "w:gz")
tar.add(user['root'] + "/htdocs", arcname="htdocs")
tar.close()
return filename
def upload_to_ftp(auth_data, con, db):
session = ftplib.FTP(auth_data['ftp_host'], auth_data['ftp_username'], auth_data['ftp_password'])
session.set_pasv(True)
query = "SELECT id, name, filename, localpath, date FROM uploaded_backups WHERE remotepath IS NULL"
con.execute(query)
backups = con.fetchall()
for back in backups:
id, name, filename, localpath, date = back
remotedir = "/faculty/" + date.strftime("%Y-%m-%d")
try:
session.mkd(remotedir)
except:
pass
session.cwd(remotedir)
try:
content = open(localpath, 'rb')
try:
session.storbinary('STOR ' + filename, content)
except:
pass
content.close()
except:
pass
query = "UPDATE backups SET remotepath='%s' WHERE id='%s'" % (remotedir + "\\" + filename, id)
con.execute(query)
print "Uploaded %s " % filename
db.commit()
session.quit()
def main(files, sql, onlyusers, upload):
auth_data = get_config()
db, con = mysql_connect(auth_data)
accs = get_hosts(con)
for acc in accs:
if onlyusers and (acc['name'] not in onlyusers):
print "Ignoring %s" % acc['name']
continue
if upload:
folder = auth_data['backup_dir'] + str(datetime.date.today()) + '/'
else:
folder = acc['root'] + "/backup/"
mkdir_recursive(folder)
if files:
print "Making %s's files backup" % acc['name']
filename = files_dump(auth_data, acc, folder)
add_backup_record(con, acc, filename, upload, folder, "files")
if sql:
print "Making %s's DB backup" % acc['name']
filename = mysql_dump(auth_data, acc, folder)
add_backup_record(con, acc, filename, upload, folder, "sql")
db.commit()
if upload:
print "uploading"
upload_to_ftp(auth_data, con, db)
db.close()
if upload:
print "Remove old backup"
os.removedirs(folder)
else:
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Backups hosts')
parser.add_argument("-nosql", action="store_true", required=False)
parser.add_argument("-nofiles", action="store_true", required=False)
parser.add_argument("-only", default="", required=False)
parser.add_argument("-upload", action="store_true")
(args) = parser.parse_args()
if args.only != "":
users = args.only.split(",")
else:
users = []
main(not args.nofiles, not args.nosql, users, args.upload)