3 ### a utility to filter backups in the /db-backup area
5 # lists all files that match <prefix>-<date>-<time>.<suffix>
6 # and preserves only the following
7 # (*) more than 2 months old : one file per month
8 # (*) more than 2 weeks old : one file per week
9 # (*) recent stuff: unchanged
14 from datetime import datetime, timedelta
19 from optparse import OptionParser
24 class FileIgnored (Exception): pass
27 LEAVE_FILES_YOUNGER_THAN = 20
28 # keep that amount of (plain) months organized in weeks
29 # should not exceed 11
30 KEEP_FILES_IN_MONTH_AFTER = 2
33 # we prefer saturday as this is the end of the week
37 ## utility to parse a filename
38 datetime_pattern="(?P<datetime>[0-9]{8}\-[0-9]{4})"
40 # (bool, prefix, suffix, datetime)
42 prefix_pattern="(?P<prefix>[\w\-]+)"
43 datetime_pattern="(?P<datetime>[0-9]{8}\-[0-9]{4})"
44 suffix_pattern="(?P<suffix>[\w\.]+)"
45 filename_matcher=re.compile("\A%s[-_]%s\.%s\Z"%(prefix_pattern,datetime_pattern,suffix_pattern))
47 parsing_failed = (False, None, None, None)
49 def parse_filename (filename):
50 match=filename_matcher.match(filename)
56 dt = datetime.strptime(d['datetime'],"%Y%m%d-%H%M")
57 return (True, d['prefix'], d['suffix'], dt)
59 print "failed to parse timestamp %s from %s"%(d['datetime'],filename)
63 # one entry like this per file, managed in the Kind class
66 def __init__ (self, dir, filename, datetime, options):
68 self.filename=filename
69 self.datetime=datetime
72 self.weekday=self.datetime.weekday()
74 if self.options.verbose: print 'Filename %s is from the future - skipped'%filename
75 raise FileIgnored,"Filename from the future %s"%filename
76 self.group = self._group_string()
79 return "%s (%s) -- weekday %s"%(self.path(),self.datetime,self.datetime.weekday())
82 return os.path.normpath(os.path.join(self.dir,self.filename))
89 def sort_age (file1, file2):
90 # for 2.7, seems safer
92 return int((file1.datetime-file2.datetime).total_seconds())
93 # otherwise resort to days, we should be clear as our backups are daily
95 return int((file1.datetime-file2.datetime).days)
98 def sort_relevance (file1, file2):
101 if w1==PREFERRED_WEEKDAY and w2==PREFERRED_WEEKDAY:
102 return File.sort_age (file1,file2)
103 elif w1!=PREFERRED_WEEKDAY and w2!=PREFERRED_WEEKDAY:
104 return File.sort_age (file1,file2)
105 elif w1==PREFERRED_WEEKDAY and w2!=PREFERRED_WEEKDAY:
107 elif w1!=PREFERRED_WEEKDAY and w2==PREFERRED_WEEKDAY:
113 def _compute_barriers ():
114 if File.month_barrier:
116 # find the exact datetime for a month change
117 # KEEP_FILES_IN_MONTH_AFTER months ago +
121 if now.month>=KEEP_FILES_IN_MONTH_AFTER+1:
122 month -= KEEP_FILES_IN_MONTH_AFTER
125 month += (12-KEEP_FILES_IN_MONTH_AFTER)
126 File.month_barrier = datetime (year=year, month=month, day=day)
127 # find the next monday morning
128 remaining_days=(7-File.month_barrier.weekday())%7
129 File.week_barrier=File.month_barrier+timedelta(days=remaining_days)
132 def compute_month_barrier(): File._compute_barriers(); return File.month_barrier
134 def compute_week_barrier(): File._compute_barriers(); return File.week_barrier
136 # returns a key for grouping files, the cleanup then
137 # preserving one entry in the set of files with same group
138 def _group_string (self):
139 if self.age.days<=LEAVE_FILES_YOUNGER_THAN:
140 if self.options.verbose: print 'Filename %s is recent (%d d) - skipped'%\
141 (self.filename,LEAVE_FILES_YOUNGER_THAN)
142 raise FileIgnored,"Filename %s is recent"%self.filename
144 if self.datetime <= File.compute_month_barrier():
145 return self.datetime.strftime("%Y%m")
147 weeks=(self.datetime-File.compute_week_barrier()).days/7
149 return "week%02d"%weeks
151 def cleanup (self, preserved):
154 if self.options.destination:
155 dst = os.path.abspath(self.options.destination) + '/' + os.path.basename(self.path())
156 if self.options.verbose:
157 print "moving %s\n\tto %s"%(self.path(), dst)
158 if not self.options.dry_run:
159 os.rename (self.path(), dst)
161 if self.options.dry_run:
162 print "Would cleanup %s"%(self.path())
163 print " (keeping %s)"%preserved.path()
165 if self.options.verbose: print "unlink",self.path()
166 os.unlink (self.path())
168 # all files in a given timeslot (either month or week)
170 def __init__ (self, groupname):
171 self.groupname=groupname
173 def insert (self, file):
174 self.files.append(file)
176 self.files.sort (File.sort_relevance)
178 for file in self.files[1:]:
179 file.cleanup(self.files[0])
181 # all files with the same (prefix, suffix)
184 def __init__ (self, prefix, suffix, options):
188 # will contain tuples (filename, datetime)
191 # allow for basic checking to be done in File
192 def add_file (self, dir, filename, datetime):
194 self.list.append ( File (dir, filename, datetime, self.options) )
195 except FileIgnored: pass
197 print 'could not append %s'%filename
198 traceback.print_exc()
202 # sort self.list according to file age, oldest first
203 self.list.sort(File.sort_age)
204 # prepare groups according to age
206 for file in self.list:
208 if groupname not in self.groups:
209 self.groups[groupname]=Group(groupname)
210 self.groups[groupname].insert(file)
211 for group in self.groups.values():
215 if not self.options.verbose: return
216 print 30*'-',"%s-<date>.%s"%(self.prefix,self.suffix)
217 entries=len(self.list)
218 print " %d entries" % entries,
221 print " << %s - %s d old"%(f.filename, f.age_days()),
224 print "|| %s - %s d old >>"%(f.filename, f.age_days())
225 groupnames=self.groups.keys()
228 if self.options.extra_verbose:
229 print " Found %d groups"%len(groupnames)
232 files=self.groups[g].files
235 elif self.options.verbose:
236 print " Found %d groups"%len(groupnames),
237 for g in groupnames: print "%s->%d"%(g,len(self.groups[g].files)),
240 # sort on number of entries
242 def sort_size (k1, k2):
243 return len(k1.list)-len(k2.list)
246 groupnames=self.groups.keys()
248 for groupname in groupnames:
249 if self.options.extra_verbose: print 'GROUP',groupname
250 self.groups[groupname].keep_one()
252 # keeps an index of all files found, index by (prefix, suffix), then sorted by time
254 def __init__ (self,options):
258 def insert (self, dir, filename, prefix, suffix, datetime):
259 key= (prefix, suffix)
260 if key not in self.index:
261 self.index[key] = Kind(prefix,suffix, self.options)
262 self.index[key].add_file (dir, filename, datetime)
264 # we're done inserting, do housecleaning
266 for (key, kind) in self.index.items():
270 # sort on number of entries
271 kinds = self.index.values()
272 kinds.sort (Kind.sort_size)
276 def insert_many (self, dir, filenames):
277 for filename in filenames:
278 (b,p,s,d) = parse_filename (filename)
280 print "Filename %s does not match - skipped"%filename
282 self.insert (dir, filename, p, s, d)
285 for kind in self.index.values(): kind.cleanup()
287 def handle_dir_pattern (index, dir, pattern):
291 print "Cannot chdir into %s - skipped"%dir
293 filenames=glob(pattern)
294 index.insert_many (dir, filenames)
297 usage="Usage: %prog [options] dir_or_files"
298 parser=OptionParser(usage=usage)
299 parser.add_option ("-v","--verbose",dest='verbose',action='store_true',default=False,
300 help="run in verbose mode")
301 parser.add_option ("-x","--extra-verbose",dest='extra_verbose',action='store_true',default=False,
302 help="run in extra verbose mode")
303 parser.add_option ("-n","--dry-run",dest='dry_run',action='store_true',default=False,
305 parser.add_option ("-m","--move-to",dest='destination',action='store',type='string',default=False,
306 help="move to <destination> instead of removing the file")
307 parser.add_option ("-o","--offset",dest='offset',action='store',type='int',default=0,
308 help="pretend we run <offset> days in the future")
309 (options, args) = parser.parse_args()
310 if options.extra_verbose: options.verbose=True
312 if options.offset !=0:
314 now += timedelta(days=options.offset)
316 traceback.print_exc()
317 print "Offset not understood %s - expect an int. number of days"%options.offset
320 if options.destination and not os.path.isdir(options.destination):
321 print "Destination should be a directory"
328 # args can be directories, or patterns, like
329 # main /db-backup /db-backup-f8/*bz2
330 # in any case we handle each arg completely separately
333 if os.path.isdir (arg):
334 if arg=='.': arg=os.getcwd()
335 dir_patterns.append ( (arg, '*',) )
337 (dir,pattern)=os.path.split(arg)
338 if not dir: dir=os.getcwd()
339 dir_patterns.append ( (dir, pattern,) )
341 index = Index (options)
342 for (dir, pattern) in dir_patterns: handle_dir_pattern (index, dir, pattern)
346 print 'Found %d entries to unlink'%counter
348 if __name__ == '__main__':