...
 
Commits (5)
......@@ -29,7 +29,8 @@ def candidates(l, n):
# keep all
return l, []
else:
indices = list(map(lambda x: int(x/len(l)), range(n)))
indices = list(map(lambda x: int(x * len(l) / n), range(n)))
#print("Keep {} of {}: {}".format(n,len(l),indices))
# I'm sure there's a more pythonic way to do this
keep = []
discard = []
......@@ -48,8 +49,8 @@ class Calendar:
self.month_target = 30
self.year_target = 4
times = list(map(lambda x: datetime.utcfromtimestamp(float(x)), sorted(snapshots.keys())))
now = datetime.utcnow()
times = list(map(lambda x: datetime.fromtimestamp(float(x)), sorted(snapshots.keys())))
now = datetime.now()
self.today_by_hour = partition([x for x in times if x.date() == now.date()], lambda x: x.hour)
self.this_month_by_day = partition([x for x in times if x.year == now.year and x.month == now.month and x.day < now.day], lambda x: x.day)
......@@ -75,17 +76,21 @@ class Calendar:
k,r = all_candidates(self.today_by_hour, self.hour_target)
keep.extend(k)
remove.extend(r)
#print("### Hourly: Keep {}, Remove {}, K: {}".format(len(k),len(r),k))
k,r = all_candidates(self.this_month_by_day, self.day_target)
keep.extend(k)
remove.extend(r)
#print("### Daily: Keep {}, Remove {}, K: {}".format(len(k),len(r),k))
k,r = all_candidates(self.this_year_by_month, self.month_target)
keep.extend(k)
remove.extend(r)
#print("### Monthly Keep {}, Remove {}".format(len(k),len(r)))
k,r = all_candidates(self.previous_years_by_year, self.year_target)
keep.extend(k)
remove.extend(r)
#print("### Yearly: Keep {}, Remove {}".format(len(k),len(r)))
return keep, remove
return list(map(lambda x: str(int(x.timestamp())), keep)), list(map(lambda x: str(int(x.timestamp())), remove))
......@@ -161,6 +161,15 @@ def prune():
cal = calendar.Calendar(snapshots[name])
count = len(snapshots[name])
keep, remove = cal.prune()
print ("%s: Total: %d Keep %d Remove %d" % (name,count,len(keep),len(remove)))
print("%s: Total: %d Keep %d Remove %d" % (name,count,len(keep),len(remove)))
for s in remove:
snap = snapshots[place.name][s]
print("Removing snapshot {}".format(s))
if snap.hasTarsnap() == Snapshot.Status.complete or snap.hasTarsnap() == Snapshot.Status.partial:
print(" ... tarsnap portion".format(snap))
tarsnap.deleteSnap(snap)
if snap.hasZFS() == Snapshot.Status.complete:
print(" ... zfs portion".format(snap))
zfs.deleteSnap(snap)
newCommand("prune", None, "Suggest which snapshots to prune", prune)