tar backuper cousin to rsync-backup

This commit is contained in:
Q
2023-10-21 22:19:41 +03:00
parent ec1c5f249e
commit 71ce8d182f
2 changed files with 39 additions and 20 deletions

View File

@@ -103,7 +103,9 @@ class RSB:
if os.path.exists(tgt_dir): if os.path.exists(tgt_dir):
raise FileExistsError("Folder {} already exists".format(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): def make_backup(self):
if self.options.no_backup: if self.options.no_backup:
@@ -123,7 +125,6 @@ class RSB:
return success return success
def write_config(self): def write_config(self):
conf = json.dumps(vars(self.options)) conf = json.dumps(vars(self.options))
with open(self.config_file, "wt") as fp: with open(self.config_file, "wt") as fp:
fp.write( fp.write(
@@ -164,7 +165,6 @@ rsync-backup \\
def get_opts(): def get_opts():
parser = ArgumentParser( parser = ArgumentParser(
description="Minimal incrementive backup utility using rsync and hard links.", description="Minimal incrementive backup utility using rsync and hard links.",
) )

View File

@@ -114,14 +114,24 @@ class TB:
os.makedirs(os.path.join(self.base_folder, self.backup_folder), exist_ok=True) os.makedirs(os.path.join(self.base_folder, self.backup_folder), exist_ok=True)
print("Backing up") print("Backing up")
ssh_command = (
[
"ssh",
*shlex.split(self.ssh_args),
]
if self.ssh_args
else []
)
ssh_command = [ with open(
"ssh", os.path.join(self.base_folder, self.backup_folder, self.tar_file), "wb"
*shlex.split(self.ssh_args), ) as fp:
] if self.ssh_args else [] with open(
os.path.join(
with open(os.path.join(self.base_folder, self.backup_folder,self.tar_file), "wb") as fp: self.base_folder, self.backup_folder, self.tar_file + ".log"
with open(os.path.join(self.base_folder, self.backup_folder,self.tar_file+".log"), "w") as fp_log: ),
"w",
) as fp_log:
command = [ command = [
*ssh_command, *ssh_command,
"tar", "tar",
@@ -131,29 +141,39 @@ class TB:
print(command) print(command)
md5 = hashlib.md5() md5 = hashlib.md5()
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=fp_log) p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=fp_log)
i=1 i = 1
while True: while True:
chunk = p.stdout.read(1048576) chunk = p.stdout.read(1048576)
if len(chunk) == 0: if len(chunk) == 0:
break break
print(f"{i} Mb", end='\r') print(f"{i} Mb", end="\r")
i+=1 i += 1
md5.update(chunk) md5.update(chunk)
fp.write(chunk) fp.write(chunk)
exitcode = p.wait() exitcode = p.wait()
success = exitcode in (0, 2) success = exitcode in (0, 2)
print(f"\nWrote {os.path.join(self.base_folder, self.backup_folder,self.tar_file)}") print(
print(f"Log file {os.path.join(self.base_folder, self.backup_folder,self.tar_file+'.log')}") f"\nWrote {os.path.join(self.base_folder, self.backup_folder,self.tar_file)}"
with open(os.path.join(self.base_folder, self.backup_folder,self.tar_file+".md5"), "w") as fp_md5: )
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") fp_md5.write(f"{md5.hexdigest()} {self.tar_file}\n")
if not success: if not success:
with open(os.path.join(self.base_folder, self.backup_folder,self.tar_file+".log")) as f: with open(
print(f.read(),end='') os.path.join(
self.base_folder, self.backup_folder, self.tar_file + ".log"
)
) as f:
print(f.read(), end="")
print(f"Exit code: {exitcode}") print(f"Exit code: {exitcode}")
return success return success
def write_config(self): def write_config(self):
conf = json.dumps(vars(self.options)) conf = json.dumps(vars(self.options))
with open(self.config_file, "wt") as fp: with open(self.config_file, "wt") as fp:
fp.write( fp.write(
@@ -198,7 +218,6 @@ tar-backup \\
def get_opts(): def get_opts():
parser = ArgumentParser( parser = ArgumentParser(
description="Backup utility using tar and ssh.", description="Backup utility using tar and ssh.",
) )