Make restic-batch script less verbose

This commit is contained in:
Wojciech Kozlowski 2023-01-04 07:57:39 +01:00
parent 0119db15e8
commit 66c6b79aaa

View File

@ -108,24 +108,68 @@ if __name__ == "__main__":
print(f"Backing up {bucket_name} : {snapshot}", flush=True) print(f"Backing up {bucket_name} : {snapshot}", flush=True)
subprocess.run(["/usr/sbin/zfs", "clone", # --------------------------------------------------------------------------------------
"-o", f"mountpoint={backup_path}", # Prepare the ZFS snapshot to backup with restic.
snapshot, "rpool/restic"], # --------------------------------------------------------------------------------------
check=True, try:
subprocess.run(
["/usr/sbin/zfs",
"clone", "-o", f"mountpoint={backup_path}", snapshot, "rpool/restic"],
check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
) )
except subprocess.CalledProcessError as err:
print(err.stdout.decode("ascii"), flush=True)
raise
try: try:
# ----------------------------------------------------------------------------------
# Check if bucket exists. If not, create and initialise the bucket.
# ----------------------------------------------------------------------------------
try: try:
subprocess.run(restic_cmd_base + ["snapshots"], env=environ, check=True) subprocess.run(restic_cmd_base + ["snapshots"], env=environ,
check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as snapshots_err:
try:
ps = subprocess.run(restic_cmd_base + ["init"], env=environ, check=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(ps.stdout.decode("ascii"), flush=True)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
subprocess.run(restic_cmd_base + ["init"], env=environ, check=True) print(snapshots_err.stdout.decode("ascii"), flush=True)
subprocess.run(restic_cmd_base + ["backup", "."], raise
cwd=backup_path, env=environ, check=True)
# ----------------------------------------------------------------------------------
# Perform the backup.
# ----------------------------------------------------------------------------------
subprocess.run(restic_cmd_base + ["backup", "."], cwd=backup_path, env=environ,
check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# ----------------------------------------------------------------------------------
# Forget and prune old snapshots.
# ----------------------------------------------------------------------------------
subprocess.run( subprocess.run(
restic_cmd_base + ["forget", "--prune", restic_cmd_base + ["forget", "--prune",
"--keep-daily", str(config["restic_keep_daily"]), "--keep-daily", str(config["restic_keep_daily"]),
"--keep-monthly", str(config["restic_keep_monthly"])], "--keep-monthly", str(config["restic_keep_monthly"])],
env=environ, check=True, env=environ, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
) )
subprocess.run(restic_cmd_base + ["check"], env=environ, check=True)
# ----------------------------------------------------------------------------------
# Check for errors.
# ----------------------------------------------------------------------------------
subprocess.run(restic_cmd_base + ["check"], env=environ,
check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
print(err.stdout.decode("ascii"), flush=True)
raise
finally: finally:
subprocess.run(["/usr/sbin/zfs", "destroy", "rpool/restic"], check=True) # ----------------------------------------------------------------------------------
# Always conclude by cleaning up the snapshot.
# ----------------------------------------------------------------------------------
try:
subprocess.run(["/usr/sbin/zfs", "destroy", "rpool/restic"],
check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
print(err.stdout.decode("ascii"), flush=True)
raise