From 66c6b79aaadd9d64fef0bce1a246d0256cb5f575 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Wed, 4 Jan 2023 07:57:39 +0100 Subject: [PATCH] Make restic-batch script less verbose --- .../roles/backups/restic/files/restic-batch | 70 +++++++++++++++---- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/playbooks/roles/backups/restic/files/restic-batch b/playbooks/roles/backups/restic/files/restic-batch index 25ecd29..14cb20d 100644 --- a/playbooks/roles/backups/restic/files/restic-batch +++ b/playbooks/roles/backups/restic/files/restic-batch @@ -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) - 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) + 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: + 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