Make restic-batch script less verbose
This commit is contained in:
parent
0119db15e8
commit
66c6b79aaa
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user