def delete_backup_file(self, filename): if self.delete_prefix: new_filename = self.delete_prefix + filename report.log_state("Renaming {0} to {1}...".format(self.cc(filename), self.cc(new_filename))) dest_path = self.dest_dir + filename new_dest_path = self.dest_dir + new_filename os.rename(dest_path, new_dest_path) else: report.log_state("Deleting {0}...".format(self.cc(filename))) dest_path = self.dest_dir + filename if os.path.isfile(dest_path): os.remove(dest_path) elif os.path.isdir(dest_path): shutil.rmtree(dest_path)
def process_vars(self, rpl): self.rpl = sorted(rpl, key=lambda x: -len(x[1])) report.log_msg("Variables:") for var in self.rpl: report.log_state("{0:>16s} = {1}".format("<{0}>".format(var[0]), var[1]))
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)
def perform(self): if not self.check_if_needed(): return dest_file_name = base_date.strftime("%Y-%m-%d_%H%M%S_") + utils.get_valid_filename(self.name) self.process_vars([ ['DEST_FILENAME', dest_file_name], ['SRC_DIR', self.src_dir], ['DEST_DIR', self.dest_dir], ]) dest_path = self.dest_dir + dest_file_name if self.do_compress in ['gzip', 'store']: ext = {'gzip': 'tgz', 'store': 'tar'}[self.do_compress] dest_path_compressed = self.dest_dir + dest_file_name + "." + ext if os.path.exists(dest_path_compressed): report.log_warn("Destination file exists {0}".format(self.cc(dest_path_compressed))) return dest_path_compressed_tmp = dest_path_compressed + ".tmp" report.log_state("Changing directory to {0}".format(self.cc(self.src_dir))) os.chdir(self.src_dir) report.log_state("Creating archive {0} to {1}...".format(self.cc(self.src_dir), self.cc(dest_path_compressed_tmp))) if self.do_compress == 'gzip': cmd = "tar --create . | pigz > {0}".format(shlex.quote(dest_path_compressed_tmp)) else: cmd = "tar --create --file {0} .".format(shlex.quote(dest_path_compressed_tmp)) report.log_command(cmd) process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) processLimit = None if self.cpu_limit: limit_cmd = "cpulimit --lazy --include-children --pid={0} --limit={1}".format(process.pid, self.cpu_limit) report.log_command(limit_cmd) processLimit = subprocess.Popen(limit_cmd, shell=True) else: pass r = process.wait() if processLimit is not None: processLimit.wait() tar_output = process.stdout.read() if r == 0: report.log_state("Moving {0} to {1}".format(self.cc(dest_path_compressed_tmp), self.cc(dest_path_compressed))) os.rename(dest_path_compressed_tmp, dest_path_compressed) else: raise BackupException("tar failed", tar_output) else: if os.path.exists(dest_path): report.log_warn("Destination folder exists {0}".format(self.cc(dest_path))) return dest_path_tmp = dest_path + "_tmp" report.log_state("Copying {0} to {1}...".format(self.cc(self.src_dir), self.cc(dest_path_tmp))) try: if os.path.exists(dest_path_tmp): shutil.rmtree(dest_path_tmp) shutil.copytree(self.src_dir, dest_path_tmp) os.rename(dest_path_tmp, dest_path) except Exception as err: report.log_warn(err)