aboutsummaryrefslogtreecommitdiff
path: root/zb-cleanup
blob: 5f7c07bb01f9898e350288982b115cb810ed9584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash

if [[ -z "$1" || -z "$2" ]] ; then
	echo "usage: $0 <zfs_object> <density_percent>" >&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 100 ]
	then
		zfs destroy "$snapname"
	else
		lasttime="$curtime"
	fi
done