--- bin/zfs-snapshot 2014-07-22 19:14:43.000000000 +0200 +++ bin/zfs-snapshot 2014-12-23 14:55:56.000000000 +0100 @@ -60,51 +60,62 @@ 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]" - ;; - *) - 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]" + ;; + *) + 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,