From f80e09e46647f670707fd92644d5ca0dbd4355ac Mon Sep 17 00:00:00 2001 From: Joel Howard Willis Weinberger Date: Thu, 13 Dec 2012 18:31:24 -0800 Subject: [PATCH] Basic --range option for zfs-delete-snapshots working --- zfs-delete-snapshots | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/zfs-delete-snapshots b/zfs-delete-snapshots index fbec614..c802ca5 100755 --- a/zfs-delete-snapshots +++ b/zfs-delete-snapshots @@ -93,10 +93,30 @@ def main(): snaps_to_destroy.extend(snaps[0:index + 1]) except ValueError: print("Snapshot %s@%s not found. Exiting." % (dataset, snapshot)) - # If '-g' was specified, for each dataset + # If '-g' was specified, for each dataset (that is the named dataset and any + # descendants, if '-r' was specified), we need to: + # - Get the snapshots on the given dataset, following the named snapshot + # - Prune any snapshots after the named RANGE snapshot + # - If no RANGE snapshot exists, do nothing + # - Concatenate all of these snapshots to the list of snapshots to delete + # - Rinse and repeat elif snaprange: - print("--range is not yet implemented") - sys.exit() + for child in datasets: + cmd = "/sbin/zfs list -H -t snapshot -s creation -o name -d 1".split() + cmd.append(child) + output = exec_in_shell(cmd) + snaps = output.split('\n') + try: + startindex = snaps.index("%s@%s" % (child, snapshot)) + try: + endindex = snaps.index("%s@%s" % (child, snaprange)) + snaps_to_destroy.extend(snaps[startindex:endindex + 1]) + except ValueError: + # If there is no snapshot with the specified upper range, do + # nothing and move on. + pass + except ValueError: + print("Snapshot %s@%s not found. Exiting." % (dataset, snapshot)) else: for child in datasets: snaps_to_destroy.append(child + "@" + snapshot)