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)
subprocess.run(["/usr/sbin/zfs", "clone",
"-o", f"mountpoint={backup_path}",
snapshot, "rpool/restic"],
check=True,
# --------------------------------------------------------------------------------------
# Prepare the ZFS snapshot to backup with restic.
# --------------------------------------------------------------------------------------
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:
# ----------------------------------------------------------------------------------
# Check if bucket exists. If not, create and initialise the bucket.
# ----------------------------------------------------------------------------------
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:
subprocess.run(restic_cmd_base + ["init"], env=environ, check=True)
subprocess.run(restic_cmd_base + ["backup", "."],
cwd=backup_path, env=environ, check=True)
print(snapshots_err.stdout.decode("ascii"), flush=True)
raise
# ----------------------------------------------------------------------------------
# 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(
restic_cmd_base + ["forget", "--prune",
"--keep-daily", str(config["restic_keep_daily"]),
"--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:
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