From 1e2343f908e6692f6e4e6ef63633e0cc5de9000c Mon Sep 17 00:00:00 2001 From: Thorsten Spille Date: Sat, 4 Mar 2023 21:39:52 +0100 Subject: [PATCH] Add min_keep parameter --- bashclub-zsync/usr/bin/bashclub-zsync | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/bashclub-zsync/usr/bin/bashclub-zsync b/bashclub-zsync/usr/bin/bashclub-zsync index bd1b6ba..cce0b00 100644 --- a/bashclub-zsync/usr/bin/bashclub-zsync +++ b/bashclub-zsync/usr/bin/bashclub-zsync @@ -32,6 +32,9 @@ subvol_source="inherited|received" # pipe separated list of snapshot name filters snapshot_filter="hourly|daily|weekly|monthly" +# minimum count of snapshots per filter to keep +min_keep=3 + usage() { cat >&2 <<-EOF usage: $prog [-h] [-d] [-c CONFIG] @@ -69,10 +72,11 @@ else cat << EOF > $conf target=$target source=$source -sshport=22 +sshport=$sshport tag=$tag subvol_source="$subvol_source" snapshot_filter="$snapshot_filter" +min_keep=$min_keep EOF log "Initial config file created. Please adjust and restart script. Exiting..." usage 0 @@ -158,9 +162,17 @@ for name in "${syncvols[@]}"; 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") 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 - log "Deleting $snap" - $zfs destroy $debug $snap + 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" + if [[ $debug == "-v" ]]; then log "[DEBUG] delete_count=$delete_count, min_keep=$min_keep"; fi + $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 fi done