Skip to content

Commit 8159256

Browse files
committed
Add WAL archiving tests before backups to ensure reliability
1 parent 8a63976 commit 8159256

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

scripts/backup-functions.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,47 @@ EOF
405405
archive_cmd_check=$(su-exec postgres psql -d "$pg_database" -t -c "SHOW archive_command;" 2>/dev/null | sed 's/^[ \t]*//;s/[ \t]*$//')
406406
log "INFO" "Current archive_command: $archive_cmd_check"
407407

408+
# Test archive-push manually with a dummy WAL file
409+
log "INFO" "Testing archive-push functionality..."
410+
411+
# Create a dummy WAL file for testing
412+
test_wal_file="/tmp/test_wal_segment"
413+
echo "dummy wal content for testing" > "$test_wal_file"
414+
415+
# Test the archive-push command
416+
if su-exec postgres pgbackrest --stanza="${stanza_name}" archive-push "$test_wal_file"; then
417+
log "INFO" "Archive-push test successful"
418+
else
419+
log "ERROR" "Archive-push test failed"
420+
# Show more details about the failure
421+
log "ERROR" "Testing archive-push with debug output..."
422+
su-exec postgres pgbackrest --stanza="${stanza_name}" --log-level-console=debug archive-push "$test_wal_file" || true
423+
fi
424+
425+
# Clean up test file
426+
rm -f "$test_wal_file"
427+
428+
# Force a WAL switch to trigger archiving
429+
log "INFO" "Forcing WAL switch to trigger archiving..."
430+
su-exec postgres psql -d "$pg_database" -c "SELECT pg_switch_wal();" || true
431+
432+
# Wait a moment for archiving to complete
433+
sleep 5
434+
435+
# Check if any WAL files have been archived
436+
log "INFO" "Checking for archived WAL files..."
437+
if [ -d "/var/lib/pgbackrest/archive/${stanza_name}" ]; then
438+
archived_count=$(find "/var/lib/pgbackrest/archive/${stanza_name}" -name "*.gz" -o -name "*.lz4" -o -name "*.xz" -o -name "*.bz2" -o -name "*-*" | wc -l)
439+
log "INFO" "Found ${archived_count} archived WAL files"
440+
if [ "$archived_count" -gt 0 ]; then
441+
log "INFO" "WAL archiving is working correctly"
442+
else
443+
log "WARN" "No archived WAL files found yet"
444+
fi
445+
else
446+
log "WARN" "WAL archive directory does not exist"
447+
fi
448+
408449
return 0
409450
}
410451

@@ -421,6 +462,44 @@ perform_pgbackrest_backup() {
421462
return 1
422463
fi
423464

465+
# For full backups, ensure we have some WAL files archived first
466+
if [ "$backup_type" = "full" ]; then
467+
log "INFO" "Checking for archived WAL files before starting full backup..."
468+
469+
# Force a few WAL switches to ensure we have archived WAL files
470+
for i in 1 2 3; do
471+
log "INFO" "Forcing WAL switch $i/3..."
472+
su-exec postgres psql -d "${POSTGRES_DB:-postgres}" -c "SELECT pg_switch_wal();" || true
473+
sleep 2
474+
done
475+
476+
# Wait for archiving to complete
477+
sleep 10
478+
479+
# Check if we have archived WAL files
480+
if [ -d "/var/lib/pgbackrest/archive/${stanza_name}" ]; then
481+
archived_count=$(find "/var/lib/pgbackrest/archive/${stanza_name}" -type f | wc -l)
482+
log "INFO" "Found ${archived_count} archived WAL files"
483+
484+
if [ "$archived_count" -eq 0 ]; then
485+
log "WARN" "No archived WAL files found. Backup may fail."
486+
log "INFO" "Attempting to run archive-push manually..."
487+
488+
# Try to manually archive any pending WAL files
489+
wal_dir="${PGDATA:-/var/lib/postgresql/data}/pg_wal"
490+
if [ -d "$wal_dir" ]; then
491+
for wal_file in "$wal_dir"/[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]; do
492+
if [ -f "$wal_file" ]; then
493+
log "INFO" "Trying to archive WAL file: $(basename "$wal_file")"
494+
su-exec postgres pgbackrest --stanza="${stanza_name}" archive-push "$wal_file" || true
495+
break
496+
fi
497+
done
498+
fi
499+
fi
500+
fi
501+
fi
502+
424503
# Perform the backup using su-exec
425504
local backup_output
426505
if ! backup_output=$(su-exec postgres bash -c "export PGBACKREST_STANZA=\"${stanza_name}\" && pgbackrest --type=\"${backup_type}\" backup" 2>&1); then

0 commit comments

Comments
 (0)