-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprune_backups.py
More file actions
80 lines (61 loc) · 2.68 KB
/
prune_backups.py
File metadata and controls
80 lines (61 loc) · 2.68 KB
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
import argparse
from pathlib import Path
from datetime import datetime, timedelta
if __name__ == "__main__":
# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("-t", help="specifies the target directory from which to prune backups",
action='store', dest="target")
args = parser.parse_args()
# select backups folder appropriately
# default location of backups folder in same dir as script
backups = Path(__file__).resolve().parent / "backups"
if args.target:
backups = Path(args.target)
# throw error if restoring from invalid target
if not backups.is_dir():
raise FileNotFoundError("ERROR: Target backups directory does not exist")
paths = reversed(sorted([p for p in backups.iterdir()]))
paths_to_remove = []
removed_something = False
ref_date = None
last_path = None
i = 1
delta_max = [timedelta(0), timedelta(1), timedelta(7), timedelta(30), timedelta(365), timedelta.max]
delta_min = delta_max[i]
for curr_path in paths:
# if the backups is of the same file
if last_path and str(last_path.stem)[:-15] == str(curr_path.stem)[:-15]:
dlst = str(curr_path.stem)[-15:].split("-")
cur_date = datetime(int(dlst[0]), int(dlst[1]), int(dlst[2]), int(dlst[3][:2]), int(dlst[3][2:]))
timediff = ref_date - cur_date
# if greater than current max, move to next max
while timediff > delta_max[i]:
delta_min = delta_max[i]
i += 1
if removed_something:
paths_to_remove.pop(-1)
removed_something = False
# if greater than next step to max, add back the last removed item
while timediff > delta_min + delta_max[i - 1]:
delta_min += delta_max[i - 1]
if removed_something:
paths_to_remove.pop(-1)
removed_something = False
# remove other paths greater than the minimum
if timediff > delta_min:
paths_to_remove.append(curr_path)
removed_something = True
else:
if removed_something:
paths_to_remove.pop(-1)
removed_something = False
dlst = str(curr_path.stem)[-15:].split("-")
ref_date = datetime(int(dlst[0]), int(dlst[1]), int(dlst[2]), int(dlst[3][:2]), int(dlst[3][2:]))
i = 1
last_path = curr_path
if removed_something:
paths_to_remove.pop(-1)
for p in paths_to_remove:
p.unlink()
print(f"Pruned {len(paths_to_remove)} file(s).")