Skip to content

Commit 0929313

Browse files
committed
Set pgBackRest permissions and improve archive command with env var
1 parent 3496e92 commit 0929313

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

scripts/backup-functions.sh

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,18 @@ EOF
389389

390390
log "INFO" "Pgbackrest stanza created successfully"
391391

392+
# Ensure proper permissions for pgBackRest directories
393+
log "INFO" "Setting proper permissions for pgBackRest directories..."
394+
chown -R postgres:postgres /var/lib/pgbackrest /var/log/pgbackrest /etc/pgbackrest
395+
chmod -R 750 /var/lib/pgbackrest /var/log/pgbackrest
396+
chmod 640 /etc/pgbackrest/pgbackrest.conf
397+
392398
# Now update the archive_command to use pgbackrest
393399
log "INFO" "Updating archive_command in postgresql.conf..."
394400
local pgdata="${PGDATA:-/var/lib/postgresql/data}"
395401

396-
# Update the archive_command to use pgbackrest
397-
sed -i "s|archive_command = '/bin/true'|archive_command = 'pgbackrest --stanza=${stanza_name} archive-push %p'|g" "$pgdata/postgresql.conf"
402+
# Update the archive_command to use pgbackrest with proper environment
403+
sed -i "s|archive_command = '/bin/true'|archive_command = 'PGBACKREST_STANZA=${stanza_name} pgbackrest --stanza=${stanza_name} archive-push %p'|g" "$pgdata/postgresql.conf"
398404

399405
# Reload PostgreSQL configuration
400406
log "INFO" "Reloading PostgreSQL configuration..."
@@ -490,6 +496,24 @@ EOF
490496
local current_archive_cmd=$(su-exec postgres psql -d "$pg_database" -t -c "SHOW archive_command;" 2>/dev/null | sed 's/^[ \t]*//;s/[ \t]*$//')
491497
log "ERROR" "Current archive_command: $current_archive_cmd"
492498

499+
# Try to manually test archive command
500+
log "INFO" "Testing archive command manually..."
501+
local wal_dir="${PGDATA:-/var/lib/postgresql/data}/pg_wal"
502+
if [ -d "$wal_dir" ]; then
503+
# Find a WAL file to test with
504+
local test_wal_file=$(find "$wal_dir" -name "[0-9A-F]*" -type f | head -1)
505+
if [ -n "$test_wal_file" ]; then
506+
log "INFO" "Testing with WAL file: $(basename "$test_wal_file")"
507+
if su-exec postgres bash -c "export PGBACKREST_STANZA=\"${stanza_name}\" && pgbackrest --stanza=\"${stanza_name}\" archive-push \"$test_wal_file\""; then
508+
log "INFO" "Manual archive test succeeded"
509+
else
510+
log "ERROR" "Manual archive test failed"
511+
fi
512+
else
513+
log "WARN" "No WAL files found for testing"
514+
fi
515+
fi
516+
493517
return 1
494518
fi
495519

0 commit comments

Comments
 (0)