--- zfs-snapshot.orig 2016-01-05 01:44:44.000000000 +0100 +++ zfs-snapshot 2016-08-27 10:49:28.541901000 +0200 @@ -63,55 +63,65 @@ type=$3 skip=$4 - # create the regex matching the type of snapshots we're currently working - # on - case "$type" in - hourly) - # hourly-2009-01-01-00 - regex="^$pool@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]$" - ;; - daily) - # daily-2009-01-01 - regex="^$pool@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$" - ;; - weekly) - # weekly-2009-01 - regex="^$pool@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]" - ;; - monthly) - # monthly-2009-01 - regex="^$pool@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]" - ;; - yearly) - # yearly-2009 - regex="^$pool@$type-[0-9][0-9][0-9][0-9]" - ;; - *) - echo "unknown snapshot type: $type" - exit 1 - esac - create_snapshot $pool $type + clean_snapshots $pool $type +} + +clean_snapshots() +{ + pool=$1 - # get a list of all of the snapshots of this type sorted alpha, which - # effectively is increasing date/time - # (using sort as zfs's sort seems to have bugs) - snapshots=`zfs list -H -o name -t snapshot | sort | grep $regex` - # count them - count=`echo $snapshots | wc -w` - if [ $count -ge 0 ]; then - # how many items should we delete - delete=`expr $count - $keep` - count=0 - # walk through the snapshots, deleting them until we've trimmed deleted - for snapshot in $snapshots; do - if [ $count -ge $delete ]; then - break - fi - delete_snapshot $snapshot - count=`expr $count + 1` - done - fi + # loop through datasets, do look for number of snapshots each + for dataset in $datasets; do + + # create the regex matching the type of snapshots we're currently working + # on + case "$type" in + hourly) + # hourly-2009-01-01-00 + regex="^$dataset@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]$" + ;; + daily) + # daily-2009-01-01 + regex="^$dataset@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$" + ;; + weekly) + # weekly-2009-01 + regex="^$dataset@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]" + ;; + monthly) + # monthly-2009-01 + regex="^$dataset@$type-[0-9][0-9][0-9][0-9]-[0-9][0-9]" + ;; + yearly) + # yearly-2009 + regex="^$dataset@$type-[0-9][0-9][0-9][0-9]" + ;; + *) + echo "unknown snapshot type: $type" + exit 1 + esac + + # get a list of all of the snapshots of this type sorted alpha, which + # effectively is increasing date/time + # (using sort as zfs's sort seems to have bugs) + snapshots=`zfs list -H -o name -t snapshot | sort | grep $regex` + # count them + count=`echo $snapshots | wc -w` + if [ $count -ge 0 ]; then + # how many items should we delete + delete=`expr $count - $keep` + count=0 + # walk through the snapshots, deleting them until we've trimmed deleted + for snapshot in $snapshots; do + if [ $count -ge $delete ]; then + break + fi + delete_snapshot $snapshot + count=`expr $count + 1` + done + fi + done } # take snapshots of type, for pools, keeping keep old ones,