From 71ce8d182faa1f17b4bd8d5906693106f06dbf39 Mon Sep 17 00:00:00 2001 From: Q Date: Sat, 21 Oct 2023 22:19:41 +0300 Subject: [PATCH] tar backuper cousin to rsync-backup --- files/rsync-backup | 6 +++--- files/tar-backup | 53 +++++++++++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/files/rsync-backup b/files/rsync-backup index a0ce5c3..2672919 100755 --- a/files/rsync-backup +++ b/files/rsync-backup @@ -103,7 +103,9 @@ class RSB: if os.path.exists(tgt_dir): raise FileExistsError("Folder {} already exists".format(tgt_dir)) - subprocess.call(["cp", "-ldr", "--preserve=xattr", self.current, tgt_dir], shell=False) + subprocess.call( + ["cp", "-ldr", "--preserve=xattr", self.current, tgt_dir], shell=False + ) def make_backup(self): if self.options.no_backup: @@ -123,7 +125,6 @@ class RSB: return success def write_config(self): - conf = json.dumps(vars(self.options)) with open(self.config_file, "wt") as fp: fp.write( @@ -164,7 +165,6 @@ rsync-backup \\ def get_opts(): - parser = ArgumentParser( description="Minimal incrementive backup utility using rsync and hard links.", ) diff --git a/files/tar-backup b/files/tar-backup index e7260dc..fda5f66 100755 --- a/files/tar-backup +++ b/files/tar-backup @@ -114,14 +114,24 @@ class TB: os.makedirs(os.path.join(self.base_folder, self.backup_folder), exist_ok=True) print("Backing up") + ssh_command = ( + [ + "ssh", + *shlex.split(self.ssh_args), + ] + if self.ssh_args + else [] + ) - ssh_command = [ - "ssh", - *shlex.split(self.ssh_args), - ] if self.ssh_args else [] - - with open(os.path.join(self.base_folder, self.backup_folder,self.tar_file), "wb") as fp: - with open(os.path.join(self.base_folder, self.backup_folder,self.tar_file+".log"), "w") as fp_log: + with open( + os.path.join(self.base_folder, self.backup_folder, self.tar_file), "wb" + ) as fp: + with open( + os.path.join( + self.base_folder, self.backup_folder, self.tar_file + ".log" + ), + "w", + ) as fp_log: command = [ *ssh_command, "tar", @@ -131,29 +141,39 @@ class TB: print(command) md5 = hashlib.md5() p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=fp_log) - i=1 + i = 1 while True: chunk = p.stdout.read(1048576) if len(chunk) == 0: break - print(f"{i} Mb", end='\r') - i+=1 + print(f"{i} Mb", end="\r") + i += 1 md5.update(chunk) fp.write(chunk) exitcode = p.wait() success = exitcode in (0, 2) - print(f"\nWrote {os.path.join(self.base_folder, self.backup_folder,self.tar_file)}") - print(f"Log file {os.path.join(self.base_folder, self.backup_folder,self.tar_file+'.log')}") - with open(os.path.join(self.base_folder, self.backup_folder,self.tar_file+".md5"), "w") as fp_md5: + print( + f"\nWrote {os.path.join(self.base_folder, self.backup_folder,self.tar_file)}" + ) + print( + f"Log file {os.path.join(self.base_folder, self.backup_folder,self.tar_file+'.log')}" + ) + with open( + os.path.join(self.base_folder, self.backup_folder, self.tar_file + ".md5"), + "w", + ) as fp_md5: fp_md5.write(f"{md5.hexdigest()} {self.tar_file}\n") if not success: - with open(os.path.join(self.base_folder, self.backup_folder,self.tar_file+".log")) as f: - print(f.read(),end='') + with open( + os.path.join( + self.base_folder, self.backup_folder, self.tar_file + ".log" + ) + ) as f: + print(f.read(), end="") print(f"Exit code: {exitcode}") return success def write_config(self): - conf = json.dumps(vars(self.options)) with open(self.config_file, "wt") as fp: fp.write( @@ -198,7 +218,6 @@ tar-backup \\ def get_opts(): - parser = ArgumentParser( description="Backup utility using tar and ssh.", )