-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackup.py
67 lines (48 loc) · 2.33 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
import os
import time
from shutil import copyfile, make_archive
import notifications
from config import FileBackup, MySql
def run_backup():
if not os.path.isfile("backup.py") and False:
raise "You need to start this script from the directory it's contained in. Please cd into that folder."
print("checking backup directory")
if not os.path.exists("backup/"):
print("create backup directory")
os.mkdir("backup/")
print("parsing backup name")
dir_name = time.strftime("%d-%m-%Y_%a_%H-%M-%S")
dest = "backup/" + dir_name + "/"
dest = dest.replace(" ", "-")
if os.path.exists(dest):
os.removedirs(dest)
os.mkdir(dest)
print("downloading MySql database")
os.popen("mysqldump -h %s -u %s -p%s %s > %sdb_backup.sql" % (MySql.host, MySql.user, MySql.password, MySql.db, dest)).readlines()
try:
print("copying files")
copyfile("logs/insert.log", dest + "insert.log")
# copytree("stats", dest + "stats/")
except FileNotFoundError:
print("no insert.log file, ignoring")
print("packing files")
make_archive(dest, "zip", dest)
print("cleaning up")
os.popen("rm -r " + dest).readlines()
print("saving on remote")
if FileBackup.key != "":
cmd = f"scp -o StrictHostKeyChecking=no -i 'secrets/{FileBackup.key}' -P {FileBackup.port} '{dest[:-1]}.zip' '{FileBackup.user}@{FileBackup.host}:{FileBackup.directory}'"
else:
cmd = f"sshpass -p {FileBackup.password} scp -o StrictHostKeyChecking=no -P {FileBackup.port} '{dest[:-1]}.zip' '{FileBackup.user}@{FileBackup.host}:{FileBackup.directory}'"
# cmd = "sshpass -p '%s' scp -P %s '%s.zip' '%s@%s:%s'" % (FileBackup.password, FileBackup.port, dest[:-1], FileBackup.user, FileBackup.host, FileBackup.directory)
# print(cmd)
print(os.popen(cmd).read())
def run_with_notify():
try:
run_backup()
notifications.Feed().push_notification("admin", "Backup Erfolg", "Der Backupvorgang wurde erfolgreich abgeschlossen!")
except Exception as e:
print(">!> Error while running backup!\n"+str(e))
notifications.Feed().push_notification("admin", "Backup Fehler", "Beim Backupvorgang ist es zu einem Fehler gekommen!\n" + str(e))
if __name__ == "__main__":
run_with_notify()