From fe75d10e9a6eaa1d40681178c0f5d742cee71464 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Tue, 10 Jul 2012 16:34:54 +0200 Subject: [PATCH] more accurate grouping --- scripts/clean-backupdb.py | 66 +++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/scripts/clean-backupdb.py b/scripts/clean-backupdb.py index 45e5026..106ba94 100755 --- a/scripts/clean-backupdb.py +++ b/scripts/clean-backupdb.py @@ -9,8 +9,9 @@ # (*) recent stuff: unchanged # +import sys import os, os.path -from datetime import datetime +from datetime import datetime, timedelta from glob import glob import re import traceback @@ -79,9 +80,6 @@ class File: def age_days (self): return self.age.days - def age_weeks (self): - return self.age.days/7 - # oldest first @staticmethod def sort_age (file1, file2): @@ -105,23 +103,31 @@ class File: elif w1!=PREFERRED_WEEKDAY and w2==PREFERRED_WEEKDAY: return 1 - month_marrier=None + month_barrier=None + week_barrier=None @staticmethod - def compute_month_barrier (): - if not File.month_marrier: - # find the exact datetime for a month change - # KEEP_FILES_IN_MONTH_AFTER months ago + - year=now.year - month=now.month - day=1 - if now.month>=KEEP_FILES_IN_MONTH_AFTER+1: - month -= KEEP_FILES_IN_MONTH_AFTER - else: - year -= 1 - month += (12-KEEP_FILES_IN_MONTH_AFTER) - File.month_marrier = datetime (year=year, month=month, day=day) - - return File.month_marrier + def _compute_barriers (): + if File.month_barrier: + return + # find the exact datetime for a month change + # KEEP_FILES_IN_MONTH_AFTER months ago + + year=now.year + month=now.month + day=1 + if now.month>=KEEP_FILES_IN_MONTH_AFTER+1: + month -= KEEP_FILES_IN_MONTH_AFTER + else: + year -= 1 + month += (12-KEEP_FILES_IN_MONTH_AFTER) + File.month_barrier = datetime (year=year, month=month, day=day) + # find the next monday morning + remaining_days=(7-File.month_barrier.weekday())%7 + File.week_barrier=File.month_barrier+timedelta(days=remaining_days) + + @staticmethod + def compute_month_barrier(): File._compute_barriers(); return File.month_barrier + @staticmethod + def compute_week_barrier(): File._compute_barriers(); return File.week_barrier # returns a key for grouping files, the cleanup then # preserving one entry in the set of files with same group @@ -134,7 +140,9 @@ class File: if self.datetime <= File.compute_month_barrier(): return self.datetime.strftime("%Y%m") else: - return "week%d"%self.age_weeks() + weeks=(self.datetime-File.compute_week_barrier()).days/7 + weeks += 1 + return "week%02d"%weeks # all files in a given timeslot (either month or week) class Group: @@ -191,7 +199,7 @@ class Kind: print " << %s - %s d old"%(f.filename, f.age_days()), if entries >=2: f=self.list[-1] - print ">> %s - %s d old"%(f.filename, f.age_days()) + print "|| %s - %s d old >>"%(f.filename, f.age_days()) groupnames=self.groups.keys() groupnames.sort() groupnames.reverse() @@ -204,7 +212,7 @@ class Kind: print " %s"%file elif self.options.verbose: print " Found %d groups"%len(groupnames), - for g in groupnames: print "%s->%d"%(k,len(self.groups[g].files)), + for g in groupnames: print "%s->%d"%(g,len(self.groups[g].files)), print '' # sort on number of entries @@ -261,8 +269,20 @@ def main (): help="run in extra verbose mode") parser.add_option ("-n","--dry-run",dest='dry_run',action='store_true',default=False, help="dry run") + parser.add_option ("-o","--offset",dest='offset',action='store',type='int',default=0, + help="pretend we run days in the future") (options, args) = parser.parse_args() if options.extra_verbose: options.verbose=True + try: + #options.offset=int(options.offset) + print 'offset=%d'%options.offset + if options.offset !=0: + global now + now += timedelta(days=options.offset) + except: + traceback.print_exc() + print "Offset not understood %s - expect an int. number of days"%options.offset + sys.exit(1) # args can be directories, or patterns, like # main /db-backup /db-backup-f8/*bz2 -- 2.43.0