Add min_keep parameter

This commit is contained in:
Thorsten Spille 2023-03-04 21:39:52 +01:00
parent ef67fc87b1
commit 1e2343f908

View File

@ -32,6 +32,9 @@ subvol_source="inherited|received"
# pipe separated list of snapshot name filters # pipe separated list of snapshot name filters
snapshot_filter="hourly|daily|weekly|monthly" snapshot_filter="hourly|daily|weekly|monthly"
# minimum count of snapshots per filter to keep
min_keep=3
usage() { usage() {
cat >&2 <<-EOF cat >&2 <<-EOF
usage: $prog [-h] [-d] [-c CONFIG] usage: $prog [-h] [-d] [-c CONFIG]
@ -69,10 +72,11 @@ else
cat << EOF > $conf cat << EOF > $conf
target=$target target=$target
source=$source source=$source
sshport=22 sshport=$sshport
tag=$tag tag=$tag
subvol_source="$subvol_source" subvol_source="$subvol_source"
snapshot_filter="$snapshot_filter" snapshot_filter="$snapshot_filter"
min_keep=$min_keep
EOF EOF
log "Initial config file created. Please adjust and restart script. Exiting..." log "Initial config file created. Please adjust and restart script. Exiting..."
usage 0 usage 0
@ -158,9 +162,17 @@ for name in "${syncvols[@]}"; do
for interval in $filter ; do for interval in $filter ; do
guid=$($ssh $sshcipher $sshport $source "zfs list -H -o guid,name -S creation -t snapshot $name | grep $interval | cut -f1 | tail -1") guid=$($ssh $sshcipher $sshport $source "zfs list -H -o guid,name -S creation -t snapshot $name | grep $interval | cut -f1 | tail -1")
if [[ "$(echo -e "$guid" | sed 's/\n//g')" != "" ]]; then if [[ "$(echo -e "$guid" | sed 's/\n//g')" != "" ]]; then
for snap in $($zfs list -H -o name,guid -S creation -t snapshot $target/$name | grep $interval | grep --after-context=200 $guid | grep -v $guid | cut -f1); do snaps_to_delete=$($zfs list -H -o name,guid -S creation -t snapshot $target/$name | grep $interval | grep --after-context=200 $guid | grep -v $guid | cut -f1)
delete_count=$(echo $snaps_to_delete | wc -l)
for snap in $snaps_to_delete; do
if [[ $delete_count -gt $min_keep ]]; then
log "Deleting $snap" log "Deleting $snap"
if [[ $debug == "-v" ]]; then log "[DEBUG] delete_count=$delete_count, min_keep=$min_keep"; fi
$zfs destroy $debug $snap $zfs destroy $debug $snap
delete_count=$(expr $delete_count - 1)
else
if [[ $debug == "-v" ]]; then log "[DEBUG] Skipping deletion of $snap. delete_count=$delete_count, min_keep=$min_keep"; fi
fi
done done
fi fi
done done