From 563ae0b11390073bdb317ece8e7e1d1ed5aa27ee Mon Sep 17 00:00:00 2001
From: Adam Thorn <alt36@cam.ac.uk>
Date: Mon, 31 Oct 2022 10:02:24 +0000
Subject: [PATCH] analyse-snaphot-usage: add optional second argument for
 specifying the initial snapshot

---
 ROOT/DEBIAN/control                       |  2 +-
 ROOT/usr/local/bin/analyse-snapshot-usage | 15 +++++++++++----
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/ROOT/DEBIAN/control b/ROOT/DEBIAN/control
index 1b0fa14..d512a15 100644
--- a/ROOT/DEBIAN/control
+++ b/ROOT/DEBIAN/control
@@ -3,6 +3,6 @@ Priority: optional
 Section: otherosfs
 Maintainer: Chemistry COs <support@ch.cam.ac.uk>
 Architecture: all
-Version: 0.9-ch96
+Version: 0.9-ch97
 Depends: zfs-dkms, postgresql-13 | postgresql-9.5 | postgresql-9.4 , liblockfile-simple-perl, libdbi-perl, libjson-perl, libzfs-perl-chem, libnet-openssh-perl, libdbd-pg-perl, mbuffer, rsync, nfs-kernel-server, pv, libwww-curl-perl
 Description: a backup system using ZFS (repository 'backup-scheduler')
diff --git a/ROOT/usr/local/bin/analyse-snapshot-usage b/ROOT/usr/local/bin/analyse-snapshot-usage
index a305a57..6ba8c43 100755
--- a/ROOT/usr/local/bin/analyse-snapshot-usage
+++ b/ROOT/usr/local/bin/analyse-snapshot-usage
@@ -8,11 +8,13 @@ if [ $EUID -eq 0 ]; then
    exit 1
 fi
 
-if [ $# -ne 1 ] ; then
-  echo usage: $0 zfsname
+if [ $# -ne 1 ] && [ $# -ne 2 ] ; then
+  echo usage: $0 zfsname [base snapshot name]
   echo
   echo Prints out a report of the space that would be reclaimed by successfully
-  echo deleting snapshots of a ZFS, from the oldest through to the newest.
+  echo deleting snapshots of a ZFS. By default the report starts with the oldest
+  echo snapshot, or you can specify the initial snapshot name as the second argument.
+  exit 1
 fi
 
 ZFS=$1
@@ -22,7 +24,11 @@ if ! zfs list $ZFS >/dev/null 2>&1 ; then
  exit 1
 fi
 
-OLDEST=$(zfs list -H -t snapshot -oname $ZFS | head -n 1 | cut -d '@' -f 2)
+if [ $# -eq 2 ] ; then
+  OLDEST=$2
+else
+  OLDEST=$(zfs list -H -t snapshot -oname $ZFS | head -n 1 | cut -d '@' -f 2)
+fi
 
 ALLSNAPS=$(zfs list -H -t snapshot -oname $ZFS | cut -d '@' -f 2)
 
@@ -31,6 +37,7 @@ ALLSNAPS=$(zfs list -H -t snapshot -oname $ZFS | cut -d '@' -f 2)
 # zfs destroy -nv $ZFS@1654118566%1654118566 # i.e. specifying the same snapshot as the start and end of the range
 # so we don't need to special-case how we handle ${OLDEST}
 for SNAP in $ALLSNAPS; do 
+  if [ $SNAP -lt $OLDEST ] ; then continue; fi
   RECLAIM=$(zfs destroy -nv ${ZFS}@${OLDEST}%${SNAP} | grep "would reclaim" | awk '{print $3}')
   echo "$OLDEST -> $SNAP : $RECLAIM"
 done
-- 
GitLab