From 11d051122b93042cd8c0c1e496879af31e8983a2 Mon Sep 17 00:00:00 2001 From: Edwin Eefting Date: Sat, 16 Feb 2019 02:06:06 +0100 Subject: [PATCH] now hold important snapshots to prevent accidental deletion by administrator or other scripts --- zfs_autobackup | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/zfs_autobackup b/zfs_autobackup index 2009258..8a7295c 100755 --- a/zfs_autobackup +++ b/zfs_autobackup @@ -100,7 +100,7 @@ def zfs_get_selected_filesystems(ssh_to, backup_name): else: if source=="local" and ( value=="true" or value=="child"): direct_filesystems.append(name) - + if source=="local" and value=="true": selected_filesystems.append(name) verbose("Selected: {0} (direct selection)".format(name)) @@ -224,6 +224,27 @@ def zfs_get_snapshots(ssh_to, filesystems, backup_name): return(ret) +def default_tag(): + return("zfs_autobackup:"+args.backup_name) + +"""hold a snapshot so it cant be destroyed accidently by admin or other processes""" +def zfs_hold_snapshot(ssh_to, snapshot, tag=None): + cmd=[ + "zfs", "hold", tag or default_tag(), snapshot + ] + + run(ssh_to=ssh_to, test=args.test, tab_split=False, cmd=cmd, valid_exitcodes=[ 0, 1 ]) + + +"""release a snapshot""" +def zfs_release_snapshot(ssh_to, snapshot, tag=None): + cmd=[ + "zfs", "release", tag or default_tag(), snapshot + ] + + run(ssh_to=ssh_to, test=args.test, tab_split=False, cmd=cmd, valid_exitcodes=[ 0, 1 ]) + + """transfer a zfs snapshot from source to target. both can be either local or via ssh. @@ -536,6 +557,9 @@ def zfs_autobackup(): else: resume_token=None + #hold the snapshot we're sending on the source + zfs_hold_snapshot(ssh_to=args.ssh_source, snapshot=source_filesystem+"@"+send_snapshot) + zfs_transfer( ssh_source=args.ssh_source, source_filesystem=source_filesystem, first_snapshot=latest_target_snapshot, second_snapshot=send_snapshot, @@ -543,11 +567,17 @@ def zfs_autobackup(): resume_token=resume_token ) + #hold the snapshot we just send to the target + zfs_hold_snapshot(ssh_to=args.ssh_target, snapshot=target_filesystem+"@"+send_snapshot) + #now that we succesfully transferred this snapshot, the previous snapshot is obsolete: if latest_target_snapshot: + zfs_release_snapshot(ssh_to=args.ssh_target, snapshot=target_filesystem+"@"+latest_target_snapshot) target_obsolete_snapshots[target_filesystem].append(latest_target_snapshot) + + zfs_release_snapshot(ssh_to=args.ssh_source, snapshot=source_filesystem+"@"+latest_target_snapshot) source_obsolete_snapshots[source_filesystem].append(latest_target_snapshot) #we just received a new filesytem? else: @@ -653,4 +683,3 @@ except Exception as e: else: print("* ABORTED *") print(str(e)) -