From 4a59d0d615b1cb232f0e838b5de1c4ace26fcf68 Mon Sep 17 00:00:00 2001 From: Lucas Oldfield Date: Sat, 7 Sep 2019 14:56:30 +1000 Subject: [PATCH] Updated zfs-auto-snapshot.sh Updated old snapshot removal to include the optional snapshot label --- src/zfs-auto-snapshot.sh | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/zfs-auto-snapshot.sh b/src/zfs-auto-snapshot.sh index 8bbf19d..e4d2eda 100644 --- a/src/zfs-auto-snapshot.sh +++ b/src/zfs-auto-snapshot.sh @@ -396,12 +396,24 @@ ZFS_LIST=$(env LC_ALL=C zfs list -H -t filesystem,volume -s name \ if [ -n "$opt_fast_zfs_list" ] then - SNAPSHOTS_OLD=$(env LC_ALL=C zfs list -H -t snapshot -o name -s name | \ - grep $opt_prefix | \ - awk '{ print substr( $0, length($0) - 14, length($0) ) " " $0}' | \ - sort -r -k1,1 -k2,2 | \ - awk '{ print substr( $0, 17, length($0) )}') \ - || { print_log error "zfs list $?: $SNAPSHOTS_OLD"; exit 137; } + # Check if a snapshot label is being used, in which case restrict the old + # snapshot removal to only snapshots with the same label format + if [ -n "$opt_label" ] + then + SNAPSHOTS_OLD=$(env LC_ALL=C zfs list -H -t snapshot -o name -s name | \ + grep "$opt_prefix"_"$opt_label" | \ + awk '{ print substr( $0, length($0) - 14, length($0) ) " " $0}' | \ + sort -r -k1,1 -k2,2 | \ + awk '{ print substr( $0, 17, length($0) )}') \ + || { print_log error "zfs list $?: $SNAPSHOTS_OLD"; exit 137; } + else + SNAPSHOTS_OLD=$(env LC_ALL=C zfs list -H -t snapshot -o name -s name | \ + grep $opt_prefix | \ + awk '{ print substr( $0, length($0) - 14, length($0) ) " " $0}' | \ + sort -r -k1,1 -k2,2 | \ + awk '{ print substr( $0, 17, length($0) )}') \ + || { print_log error "zfs list $?: $SNAPSHOTS_OLD"; exit 137; } + fi else SNAPSHOTS_OLD=$(env LC_ALL=C zfs list -H -t snapshot -S creation -o name) \ || { print_log error "zfs list $?: $SNAPSHOTS_OLD"; exit 137; }