# -*- coding: utf-8 -*- """ Backup plan for Odoo databases :::::::::::::::::::::::::::::: Keep : - all saves the last week - one save per week the last month - one save per month the last year """ from datetime import datetime from os import unlink, environ from glob import glob OBPLAN = environ.get('OBPLAN', u'365,31,7') OBPLAN = map(int, OBPLAN.split(u',')) DT = datetime.now() def notkeep(fname): """ Filter all obsolete save files """ fname = fname.split('/')[-1] fname = fname.split('_') if len(fname) <= 1: return False save_date = fname[0] save_date = datetime.strptime(save_date, '%Y-%m-%d') delta = DT - save_date if delta.days > OBPLAN[0]: return True elif delta.days > OBPLAN[1]: if save_date.day != 1: return True elif delta.days > OBPLAN[2]: if (save_date.weekday() != 0) and (save_date.day != 1): return True else: return False # Filters zip files and keeps only legitimate ones # ================================================ FILES = glob('backups/*.zip') + glob('backups/*.dump') BACKUPS_TO_REMOVE = filter(notkeep, FILES) if len(BACKUPS_TO_REMOVE) > 0: print('Save files outdated, will be deleted : \n\n* ' + '\n* '.join(BACKUPS_TO_REMOVE)) for b in BACKUPS_TO_REMOVE: unlink(b)