From e04bdea498d4af6e7454dbd48c20d68ee062efdf Mon Sep 17 00:00:00 2001 From: fascinated Date: Sat, 10 May 2025 04:31:14 +0100 Subject: [PATCH] 7 --- backup.sh | 60 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/backup.sh b/backup.sh index 6d2ebc4..03c1478 100755 --- a/backup.sh +++ b/backup.sh @@ -436,28 +436,50 @@ main() { if [ "${CONFIG[ENABLE_PRUNE]}" = "true" ]; then log "Applying retention policy (keeping last ${CONFIG[KEEP_LAST]} backups globally)..." - # Run the prune + # First try to unlock the repository if it's locked + log "Checking for stale locks..." + if ! restic -r "${CONFIG[RESTIC_REPOSITORY]}" unlock 2>&1 | tee -a "$BACKUP_LOG"; then + log "Warning: Failed to unlock repository, but continuing with prune attempt" + fi + + # Run the prune with retries local prune_output - if ! prune_output=$(restic -r "${CONFIG[RESTIC_REPOSITORY]}" forget --keep-last "${CONFIG[KEEP_LAST]}" --group-by "" --prune 2>&1); then - log "Prune operation failed with output:" - echo "$prune_output" | while read -r line; do - log " $line" - done + local max_retries=3 + local retry_count=0 + local prune_success=false + + while [ $retry_count -lt $max_retries ] && [ "$prune_success" = false ]; do + if [ $retry_count -gt 0 ]; then + log "Retry $retry_count of $max_retries..." + sleep 5 # Wait 5 seconds between retries + fi + + if ! prune_output=$(restic -r "${CONFIG[RESTIC_REPOSITORY]}" forget --keep-last "${CONFIG[KEEP_LAST]}" --group-by "" --prune 2>&1); then + log "Prune attempt $((retry_count + 1)) failed with output:" + echo "$prune_output" | while read -r line; do + log " $line" + done + retry_count=$((retry_count + 1)) + else + prune_success=true + log "Prune output: $prune_output" + if echo "$prune_output" | grep -q "no snapshots were removed"; then + log "No snapshots to prune" + prune_status="No snapshots to prune" + elif echo "$prune_output" | grep -q "removed"; then + log "Successfully pruned old backups" + prune_status="Successfully pruned old backups" + else + log "Prune completed but no clear status found in output" + prune_status="Prune completed" + fi + fi + done + + if [ "$prune_success" = false ]; then status="⚠️ Backup OK, Prune Failed" color="${DISCORD_COLORS[warning]}" - prune_status="Prune operation failed" - else - log "Prune output: $prune_output" - if echo "$prune_output" | grep -q "no snapshots were removed"; then - log "No snapshots to prune" - prune_status="No snapshots to prune" - elif echo "$prune_output" | grep -q "removed"; then - log "Successfully pruned old backups" - prune_status="Successfully pruned old backups" - else - log "Prune completed but no clear status found in output" - prune_status="Prune completed" - fi + prune_status="Prune operation failed after $max_retries attempts" fi else log "Pruning disabled"