-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnoobackup.py
66 lines (52 loc) · 1.6 KB
/
snoobackup.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
#!/usr/bin/env python3
import subprocess
from contextlib import contextmanager
from pathlib import Path
BACKUPS_USER = 'snoobackup'
BACKUP_HOME = Path('/home').resolve() / BACKUPS_USER
BACKUP_DEST = BACKUP_HOME / 'backupdata'
FILELIST_PATH = BACKUP_HOME / 'filelist'
LOCKFILE = BACKUP_HOME / '.backup-in-progress'
ACL_BACKUP_FILE = BACKUP_DEST / 'acls.bak'
RSYNC_OPTIONS = [
'-aRPEXA', '--delete', '--exclude={}'.format(BACKUP_DEST)
]
@contextmanager
def backup_lock():
f = LOCKFILE
if f.exists():
raise FileExistsError("Lock file exists! {}".format(f))
f.touch()
try:
yield
finally:
f.unlink()
def main():
with FILELIST_PATH.open(encoding='utf-8') as f:
file_list = f.read().splitlines()
files = [
file for file in file_list
if file and not file.startswith('#') and Path(file).exists()
]
if not files:
return
with backup_lock():
subprocess.check_call(
['rsync'] + RSYNC_OPTIONS + files + [str(BACKUP_DEST)],
stdout=subprocess.DEVNULL
)
with ACL_BACKUP_FILE.open('w') as f:
subprocess.check_call([
'getfacl', '-R', '.'
], stdout=f, cwd=str(BACKUP_DEST))
subprocess.check_call(
['setfacl', '-R', '-m', 'u:{}:r'.format(BACKUPS_USER), '.'],
cwd=str(BACKUP_DEST)
)
subprocess.check_call([
'find', '.', '-type', 'd', '-exec',
'setfacl', '-m', 'u:{}:rx'.format(BACKUPS_USER),
'{}', ';'
], cwd=str(BACKUP_DEST))
if __name__ == '__main__':
main()