zfs-backup/zb-cleanup
2014-05-30 17:20:01 +02:00

40 lines
776 B
Bash
Executable file

#!/bin/bash
if [[ -z "$1" || -z "$2" ]] ; then
echo "usage: $0 <zfs_object> <density>" >&2
exit 1
fi
timestamp () {
t="`echo \"$1\" |sed -ne 's/^.*@zb-//p' |tr 'p_' '+ '`"
[[ -z "$t" ]] && return 1
date --date="$t" +%s
return $?
}
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" -ge "$timenow" ] ; then
break
fi
#if it's too dense, delete the closer snapshot
if [ $(( $density*($curtime-$lasttime)/($timenow-$lasttime) )) -lt 1]
then
zfs destroy "$snapname"
else
lasttime="$curtime"
fi
done