-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch_for_backup.py
65 lines (47 loc) · 1.42 KB
/
watch_for_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
import sys
import hashlib
import shutil
import os
import time
from pathlib import Path
# hacky script for backing up a file;
# provide commandline args as:
# file, output dir, [checking interval in ms, 500]
# n.b. should use inode watchers rather than polling ...
args = sys.argv
target = Path(args[1])
target_filename = target.name
output = Path(args[2])
interval_ms = int(args[3]) if len(args) > 3 else 500
interval = interval_ms / 1000
print(f"will backup {target} to {output/target_filename} every {interval} seconds")
def get_hash():
hasher = hashlib.sha1()
content = target.read_bytes()
hasher.update(content)
return hasher.hexdigest()
def backup():
dest = output / f"{int(time.time())}.{target_filename}"
shutil.copyfile(target, dest)
print('backup: %s %s' % (time.strftime('%H:%M:%S'), dest))
_state = None
h_old = None
t_old = None
while True:
try:
# check timestamps (cheap)
t_new = os.path.getmtime(target)
_state = True
if t_new != t_old:
t_old = t_new
# check hash (less cheap)
h_new = get_hash()
if h_new != h_old:
backup()
h_old = h_new
else:
print('timestamp change, no backup: %s' % t_new)
except FileNotFoundError:
if _state is not False: print('file does not exist yet, waiting ...')
_state = False
time.sleep(interval)