-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch_for_backup.py
54 lines (41 loc) · 1.19 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
import sys
import hashlib
import shutil
import os
import time
# hacky script for backing up a file
# file, output dir, checking interval
args = sys.argv
target = args[1]
target_filename = os.path.split(target)[1]
output = args[2]
output_format = "%s.%s"
interval = int(args[3]) if len(args) > 3 else 1
def get_hash():
hasher = hashlib.sha1()
with open(target, 'rb') as fh:
hasher.update(fh.read())
return hasher.hexdigest()
def backup():
dest = os.path.join(output, output_format % (int(time.time()), target_filename))
shutil.copyfile(target, dest)
print 'backup: %s %s' % (time.strftime('%H:%M:%S'), dest)
if not os.path.exists(target):
print 'file does not exist yet, waiting ...'
while not os.path.exists(target):
time.sleep(interval)
h_old = None
t_old = None
while True:
# check timestamps (cheap)
t_new = os.path.getmtime(target)
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
time.sleep(interval)