-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupScript
136 lines (111 loc) · 5.09 KB
/
backupScript
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
import datetime
import os
import socket
import subprocess
import sys
import tempfile
from config import *
DATE = datetime.datetime.now().strftime("%Y-%m-%d")
TIME = datetime.datetime.now().strftime("%H:%M:%S")
HOSTNAME = socket.gethostname()
os.environ["BORG_PASSPHRASE"] = PASSPHRASE
# Set up logging
logging.basicConfig(filename=LOGFILE_PATH, level=LOGGING_LEVEL,
format="%(asctime)s - %(levelname)s - %(message)s")
def borg_repository_exists():
if not os.path.exists(REPOSITORY_LOCATION):
return False
elif os.listdir(REPOSITORY_LOCATION) == []:
return False
else:
return True
def init_borg_repository():
process_init = subprocess.run(["borg", "init", "--encryption=repokey-blake2", REPOSITORY_LOCATION],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if process_init.returncode == 0:
logging.info("Initialized BorgBackup repository.")
elif process_init.returncode == 1:
logging.warning("Initialized BorgBackup, however there is a warning. " + process_init.stderr.decode("utf-8"))
else:
logging.critical("Could not initialize BorgBackup repository. " + process_init.stderr.decode("utf-8"))
sys.exit(1)
def get_file_with_excluded_dicts():
temp_file = tempfile.NamedTemporaryFile(mode="r+")
for excluded_directory in EXCLUDED_DIRECTORIES:
temp_file.write(excluded_directory + "\n")
temp_file.flush()
return temp_file
def create_backup(temp_file=get_file_with_excluded_dicts()):
process_create = subprocess.run(["borg", "create",
REPOSITORY_LOCATION + "::" + HOSTNAME + "-" + DATE,
DIRECTORY_TO_BACKUP,
"--exclude-from", temp_file.name,
"--compression", "lz4"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if process_create.returncode == 0:
update_datefile()
logging.info("Backup successful.")
#TODO: Optional more precise output (e.g. backup size)?
elif process_create.returncode == 1:
logging.warning("Backup reached its normal end, but there were "
"warnings. The backup date in the file " + LOGFILE_PATH +
" has not been updated. "
+ process_create.stderr.decode("utf-8"))
else:
logging.error("Could not create backup." + process_create.stderr.decode("utf-8"))
def update_datefile():
try:
with open(BACKUP_DATE_FILE_PATH, "w") as date_file:
date_file.seek(0)
date_file.write(DATE)
except PermissionError:
logging.warning("Could not update date of latest backup because of missing permissions.")
def prune_backups():
process_prune = subprocess.run(["borg", "prune", REPOSITORY_LOCATION, "--prefix",
HOSTNAME + "-",
"--keep-daily=" + KEEP_DAILY,
"--keep-weekly=" + KEEP_WEEKLY,
"--keep-monthly=" + KEEP_MONTHLY],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if process_prune.returncode == 0:
logging.info("Prune successful.")
elif process_prune.returncode == 1:
logging.warning("Warning for prune command. " + process_prune.stderr.decode("utf-8"))
else:
logging.warning("Error while executing 'prune' command." + process_prune.stderr.decode("utf-8"))
def mount():
# Note here that 'vers=1.0' is required because FRITZ!Box only supports SMB 1.0
process_mount = subprocess.run(["mount", "-t", "cifs", "-o", "credentials=" + SMB_CREDENTIALS_FILE + "," + \
"vers=1.0", "//" + SHARE_ADDRESS, MOUNT_POINT],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if process_mount.returncode == 0:
logging.info(SHARE_ADDRESS + " has been mounted.")
else:
logging.critical("Could not mount Samba share. " + process_mount.stderr.decode("utf-8"))
sys.exit(1)
def umount():
process_umount = subprocess.run(["umount", MOUNT_POINT],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if process_umount.returncode == 0:
logging.info("Umount successful.")
else:
logging.warning("Umount returncode is not 0. " + process_umount.stderr.decode("utf-8"))
if __name__ == "__main__":
logging.info("Script started ...")
if not os.path.isdir(REPOSITORY_LOCATION):
mount()
else:
logging.warning(SHARE_ADDRESS + " already mounted!")
if not borg_repository_exists():
init_borg_repository()
create_backup()
prune_backups()
umount()
logging.info("Script has been completed." + "\n" + \
"=====================================")