#!/bin/bash source /etc/profile >/dev/null [ -f "$HOME/.zb-rc" ] && source "$HOME/.zb-rc" if [[ -z "$1" || -z "$2" ]] ; then echo "usage: $0 [max_age]" >&2 exit 1 fi if [[ "$2" -lt 100 ]] ; then echo "density must be >= 100" >&2 exit 2 fi timestamp () { t="`echo \"$1\" |sed -ne 's/^.*@zb-//p' |tr 'p_' '+ '`" [[ -z "$t" ]] && return 1 date --date="$t" +%s return $? } max_age=0 if [[ -n "$3" ]] ; then max_age=`date --date="$3" +%s` fi density="$2" timenow=`date +%s` lasttime=0 # list snapshots zfs list -t snapshot -r -d 1 -H "$1" |awk '{print $1;}' | while read l ; do #link unix timestamps unixtime=`timestamp "$l"` || continue echo "$unixtime" "$l" done |sort -n | while read l ; do curtime=${l%% *} snapname=${l#* } if [ "$curtime" -lt "$max_age" ] ; then #too old zfs destroy "$snapname" || echo "$0: zfs destroy $snapname failed" >&2 continue fi if [ "$curtime" -ge "$timenow" ] ; then #this actually shouldn't happen, but who knows break fi #if it's too dense, delete the closer snapshot if [ $(( ($density*($curtime-$lasttime))/($timenow-$lasttime) )) -lt 100 ] then zfs destroy "$snapname" || echo "$0: zfs destroy $snapname failed" >&2 else lasttime="$curtime" fi done