diff --git a/scripts/backup/manage-backups.sh b/scripts/backup/manage-backups.sh index 451b284..8147f68 100755 --- a/scripts/backup/manage-backups.sh +++ b/scripts/backup/manage-backups.sh @@ -150,8 +150,11 @@ cleanup_old_backups() { done fi } -# Prepare dated backup directory -DATED_DIR="$BACKUP_BASE/$(date_formatted)" +# Prepare dated backup directory using a stable run timestamp. +# This prevents later steps from missing files if the minute changes while +# long archive operations are running. +RUN_STAMP="$(date_formatted)" +DATED_DIR="$BACKUP_BASE/$RUN_STAMP" mkdir -p "$DATED_DIR" wait_for_database_backups || exit 1 @@ -188,7 +191,22 @@ log "Moving files from database dump to dated directory..." #log "Files to move:" ls -1 "$BACKUP_BASE/databases"/* 2>/dev/null || true log "Moving database dumps to dated directory..." -mv "$BACKUP_BASE/databases"/*_$(date_formatted)*.sql.gz "$DATED_DIR"/ 2>/dev/null || true + +dump_moved=0 +for dump_file in "$BACKUP_BASE/databases"/*_"$RUN_STAMP"*.sql.gz; do + [ -e "$dump_file" ] || break + if mv "$dump_file" "$DATED_DIR"/; then + dump_moved=$((dump_moved + 1)) + else + err "Failed to move database dump $dump_file" + fi +done + +if [ "$dump_moved" -eq 0 ]; then + log "No database dumps matched run timestamp $RUN_STAMP" +else + log "Moved $dump_moved database dump(s) to $DATED_DIR" +fi cleanup_old_backups