Rewrite of housekeeping, timebased retention introduced

This commit is contained in:
2022-10-28 13:53:07 +02:00
parent f20e4c4f63
commit 812253a7e0

View File

@@ -43,12 +43,14 @@ declare -i opt_keepslock=0
declare -i opt_keepdlock=0 declare -i opt_keepdlock=0
declare -i opt_overwrite=0 declare -i opt_overwrite=0
declare -i opt_online=0 declare -i opt_online=0
declare -i opt_keep_local=0 declare opt_keep_local='0s'
declare -i opt_keep_remote=0 declare opt_keep_remote='0s'
declare -r redstconf='^\/etc\/pve\/nodes\/(.*)\/qemu-server\/([0-9]+).conf$' declare -r redstconf='^\/etc\/pve\/nodes\/(.*)\/qemu-server\/([0-9]+).conf$'
declare -r recephimg='([a-zA-Z0-9]+)\:(.*)' declare -r recephimg='([a-zA-Z0-9]+)\:(.*)'
declare -r restripsnapshots='/^$/,$d' 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(){ function usage(){
shift shift
@@ -159,6 +161,20 @@ function parse_opts(){
[ -z "$opt_vm_ids" ] && { log info "VM id is not set."; end_process 1; } [ -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 if [ "$opt_vm_ids" = "all" ]; then
local all='' local all=''
local data='' local data=''
@@ -518,19 +534,38 @@ function mirror() {
} }
function do_housekeeping(){ function do_housekeeping(){
horst=$1 local horst=$1
rbdpool=$2 local rbdpool=$2
rbdimage=$3 local rbdimage=$3
keep=$4 local keep=$4
vm_id=$5 local vm=$5
snapshotstokill=$(ssh $horst rbd ls -l $rbdpool | grep $rbdimage@$opt_snapshot_prefix | cut -d ' ' -f 1|head -n -1 |head -n -$keep) local -i keeptime
#log info "VM $vm_id - Houskeeping $horst $rbdpool $rbdimage, keeping previous $keep Snapshots" local -i ts
for snap in $snapshotstokill; do local -i snapepoch
cmd="ssh $horst rbd snap rm $rbdpool/$snap" local -i age
do_run $cmd 2>/dev/null
log info "VM $vm_id - Housekeeping: $horst $rbdpool $rbdimage, keeping previous $keep Snapshots" log info "VM $vm - Housekeeping: $horst $rbdpool/$rbdimage, keeping Snapshots for $keep"
log debug "$cmd" cmd="ssh $horst rbd ls -l $rbdpool | grep $rbdimage@$opt_snapshot_prefix | cut -d ' ' -f 1|head -n -1"
return $rc 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 done
} }