def __init__(self, config): super().__init__(config) self.interval = utils.get_interval_from_str(config['interval']) self.do_compress = config['compress'] self.cpu_limit = config.get('cpu_limit') self.check = config['check'] self.src_dir = config['src'] if not os.path.isabs(self.src_dir): raise BackupException("Source directory - path must be absolute: {0}".format(self.src_dir)) if not os.path.exists(self.src_dir): raise BackupException("Source directory doesn't exist: {0}".format(self.src_dir))
def perform(self): self.rpl = [ ['DEST_DIR', self.dest_dir], ] self.rpl = sorted(self.rpl, key=lambda x: -len(x[1])) if self.delete_older_than is not None: interval = utils.get_interval_from_str(self.delete_older_than) for filename in os.listdir(self.dest_dir): if filename.endswith(".tmp"): continue date = utils.get_date_from_filename(filename) if date is None: continue if utils.get_date_diff_in_seconds(date, base_date) >= interval: self.delete_backup_file(filename) elif self.clean_day_parts: parts = [] # extract parts from string cur_date = base_date.date() for p in self.clean_day_parts.split(","): days = int(p.strip()) start_date = cur_date - timedelta(days - 1) end_date = cur_date cur_date = start_date - timedelta(1) parts.append({"from": start_date, "to": end_date, "file_to_keep": None}) # assign files to parts for filename in os.listdir(self.dest_dir): if filename.endswith(".tmp"): continue date = utils.get_date_from_filename(filename) if date is None: continue date = date.date() for p in parts: if date >= p["from"] and date <= p["to"]: if p["file_to_keep"] is None or utils.get_date_from_filename(p["file_to_keep"]).date() > date: p["file_to_keep"] = filename # gather all files to keep report.log_msg("Current backups state") files_to_keep = [] for p in parts: if p["file_to_keep"] is not None: files_to_keep.append(p["file_to_keep"]) file_date = utils.get_date_from_filename(p["file_to_keep"]) file_str = "{0[file_to_keep]} ({1})".format(p, ago_format(base_date - file_date)) else: file_str = 'no file' report.log_state("[{0[from]}, {0[to]}] file: {1}".format(p, file_str)) for filename in os.listdir(self.dest_dir): if filename.endswith(".tmp"): continue date = utils.get_date_from_filename(filename) if date is None: continue date = date.date() if not filename in files_to_keep: self.delete_backup_file(filename)