-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrb_cleanup_helper
executable file
·56 lines (44 loc) · 1.37 KB
/
rrb_cleanup_helper
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
#!/usr/bin/env python
import sys
import os
import os.path
import datetime
def execRule(now, rule, backups):
if len(backups) < rule[1]:
del backups[:]
print >> sys.stderr, "Rule", rule, "keeps all"
return
optDts = [now - datetime.timedelta(days=y) * rule[0] / rule[1] \
for y in sorted(range(rule[1]))]
for optDt in optDts:
diffs = []
for backup in backups:
diffs.append((abs(optDt - backup[0]), backup[0], backup[1]))
diffs.sort()
print >> sys.stderr, "Rule", rule, "tries to get", \
optDt.strftime("%Y-%m-%dT%H:%M:%S"), "and keeps", diffs[0][2]
backups.remove((diffs[0][1], diffs[0][2]))
def main(argv=None):
if not argv:
argv = sys.argv
if len(argv) < 5 or len(argv) % 2 != 0:
print >> sys.stderr, "Usage: " + argv[0] + " dir rule1_days rule1_nr [...]"
baseDir = argv[1]
rules = map(lambda x: (int(x[0]), int(x[1])), zip(*[iter(argv[2:])]*2))
dirs = os.listdir(baseDir)
backups = []
for dir in dirs:
try:
dt = datetime.datetime.strptime(dir, "%Y-%m-%dT%H:%M:%S")
except ValueError:
continue
backups.append((dt, dir))
backups.sort(reverse=True)
now = datetime.datetime.now()
for rule in rules:
execRule(now, rule, backups)
# Print backups to be deleted.
for backup in backups:
sys.stdout.write(os.path.join(baseDir, backup[1]) + '\0')
if __name__ == '__main__':
main()