From 812253a7e0b1b6b88f845c9faac18c527a00ba28 Mon Sep 17 00:00:00 2001 From: Bastian Date: Fri, 28 Oct 2022 13:53:07 +0200 Subject: [PATCH] Rewrite of housekeeping, timebased retention introduced --- crossover | 65 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/crossover b/crossover index beea20f..0059e2c 100755 --- a/crossover +++ b/crossover @@ -43,12 +43,14 @@ declare -i opt_keepslock=0 declare -i opt_keepdlock=0 declare -i opt_overwrite=0 declare -i opt_online=0 -declare -i opt_keep_local=0 -declare -i opt_keep_remote=0 +declare opt_keep_local='0s' +declare opt_keep_remote='0s' declare -r redstconf='^\/etc\/pve\/nodes\/(.*)\/qemu-server\/([0-9]+).conf$' declare -r recephimg='([a-zA-Z0-9]+)\:(.*)' declare -r restripsnapshots='/^$/,$d' +declare -r retimestamp='^.*@mirror-([0-9]+)$' +declare -r redateex='^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$' function usage(){ shift @@ -159,6 +161,20 @@ function parse_opts(){ [ -z "$opt_vm_ids" ] && { log info "VM id is not set."; end_process 1; } + if [ -n "$opt_keep_local" ]; then + if ! [[ ${opt_keep_local:(-1)} == "s" || ${opt_keep_local:(-1)} == "d" ]]; then + echo "--keeplocal: Parameter malformed. suffix s or d missing" + end_process 255 + fi + fi + + if [ -n "$opt_keep_remote" ]; then + if ! [[ ${opt_keep_remote:(-1)} == "s" || ${opt_keep_remote:(-1)} == "d" ]]; then + echo "--keepremote: Parameter malformed. suffix s or d missing" + end_process 255 + fi + fi + if [ "$opt_vm_ids" = "all" ]; then local all='' local data='' @@ -518,19 +534,38 @@ function mirror() { } function do_housekeeping(){ - horst=$1 - rbdpool=$2 - rbdimage=$3 - keep=$4 - vm_id=$5 - snapshotstokill=$(ssh $horst rbd ls -l $rbdpool | grep $rbdimage@$opt_snapshot_prefix | cut -d ' ' -f 1|head -n -1 |head -n -$keep) - #log info "VM $vm_id - Houskeeping $horst $rbdpool $rbdimage, keeping previous $keep Snapshots" - for snap in $snapshotstokill; do - cmd="ssh $horst rbd snap rm $rbdpool/$snap" - do_run $cmd 2>/dev/null - log info "VM $vm_id - Housekeeping: $horst $rbdpool $rbdimage, keeping previous $keep Snapshots" - log debug "$cmd" - return $rc + local horst=$1 + local rbdpool=$2 + local rbdimage=$3 + local keep=$4 + local vm=$5 + local -i keeptime + local -i ts + local -i snapepoch + local -i age + + log info "VM $vm - Housekeeping: $horst $rbdpool/$rbdimage, keeping Snapshots for $keep" + cmd="ssh $horst rbd ls -l $rbdpool | grep $rbdimage@$opt_snapshot_prefix | cut -d ' ' -f 1|head -n -1" + snapshots=$(do_run "$cmd") + if [ "${keep:(-1)}" == "d" ]; then + keep=${keep%?} + keeptime=$(( $keep * 86400 )) + elif [ "${keep:(-1)}" == "s" ]; then + keep=${keep%?} + keeptime=$keep + fi + for snap in $snapshots; do + [[ $snap =~ $retimestamp ]] + ts=${BASH_REMATCH[1]} + [[ $ts =~ $redateex ]] + snapepoch=$(date --date "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}/${BASH_REMATCH[3]} ${BASH_REMATCH[4]}:${BASH_REMATCH[5]}:${BASH_REMATCH[6]}" +%s) + age=$(($(date +"%s")-$snapepoch )) + if [ $age -gt $keeptime ]; then + cmd="ssh $horst rbd snap rm $rbdpool/$snap" + do_run "$cmd" 2>/dev/null + log info "VM $vm_id - Removing Snapshot $horst $rbdpool/$snap ($age""s) [rc:$?]" + return $rc + fi done }