Skip to content

Commit 96f869b

Browse files
committed
Add pgBackRest stanza configuration with improved error handling
1 parent eedec2c commit 96f869b

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

scripts/backup-functions.sh

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,20 @@ configure_pgbackrest_stanza() {
319319
local socket_dir="${PGHOST:-/var/run/postgresql}"
320320
local pg_port="${PGPORT:-5432}"
321321
local pg_user="${POSTGRES_USER:-postgres}"
322+
local pg_database="${POSTGRES_DB:-postgres}"
322323

323324
log "INFO" "Configuring pgbackrest stanza: $stanza_name"
325+
log "INFO" "Using pgdata: $pgdata"
326+
log "INFO" "Using socket dir: $socket_dir"
327+
log "INFO" "Using port: $pg_port"
328+
log "INFO" "Using user: $pg_user"
329+
log "INFO" "Using database: $pg_database"
330+
331+
# Ensure PostgreSQL is running before configuring stanza
332+
if ! wait_for_postgres 60; then
333+
log "ERROR" "PostgreSQL is not ready for stanza configuration"
334+
return 1
335+
fi
324336

325337
# Create stanza configuration
326338
cat >> /etc/pgbackrest/pgbackrest.conf << EOF
@@ -330,15 +342,25 @@ pg1-path=${pgdata}
330342
pg1-socket-path=${socket_dir}
331343
pg1-port=${pg_port}
332344
pg1-user=${pg_user}
333-
pg1-database=postgres
345+
pg1-database=${pg_database}
334346
EOF
335347

336348
log "INFO" "Stanza configuration added to pgbackrest.conf"
337349

338-
# Create the stanza
350+
# Fix permissions on config file
351+
chown postgres:postgres /etc/pgbackrest/pgbackrest.conf
352+
chmod 640 /etc/pgbackrest/pgbackrest.conf
353+
354+
# Show the configuration for debugging
355+
log "DEBUG" "Current pgbackrest configuration:"
356+
cat /etc/pgbackrest/pgbackrest.conf
357+
358+
# Create the stanza using su-exec instead of su
339359
log "INFO" "Creating pgbackrest stanza..."
340-
if ! su - postgres -c "pgbackrest --stanza=${stanza_name} stanza-create"; then
360+
if ! su-exec postgres pgbackrest --stanza="${stanza_name}" stanza-create; then
341361
log "ERROR" "Failed to create pgbackrest stanza"
362+
log "ERROR" "Checking pgbackrest configuration..."
363+
su-exec postgres pgbackrest --stanza="${stanza_name}" check || true
342364
return 1
343365
fi
344366

@@ -353,10 +375,18 @@ perform_pgbackrest_backup() {
353375

354376
log "INFO" "Starting pgbackrest $backup_type backup for stanza: $stanza_name"
355377

356-
# Perform the backup
378+
# Ensure PostgreSQL is ready
379+
if ! wait_for_postgres 30; then
380+
log "ERROR" "PostgreSQL is not ready for backup"
381+
return 1
382+
fi
383+
384+
# Perform the backup using su-exec
357385
local backup_output
358-
if ! backup_output=$(su - postgres -c "pgbackrest --stanza=${stanza_name} --type=${backup_type} backup" 2>&1); then
386+
if ! backup_output=$(su-exec postgres pgbackrest --stanza="${stanza_name}" --type="${backup_type}" backup 2>&1); then
359387
log "ERROR" "Pgbackrest backup failed: $backup_output"
388+
log "ERROR" "Checking stanza status..."
389+
su-exec postgres pgbackrest --stanza="${stanza_name}" info || true
360390
return 1
361391
fi
362392

@@ -365,7 +395,7 @@ perform_pgbackrest_backup() {
365395

366396
# Get the latest backup info
367397
local backup_info
368-
if ! backup_info=$(su - postgres -c "pgbackrest --stanza=${stanza_name} info --output=json" 2>/dev/null); then
398+
if ! backup_info=$(su-exec postgres pgbackrest --stanza="${stanza_name}" info --output=json 2>/dev/null); then
369399
log "WARN" "Could not retrieve backup info"
370400
return 0
371401
fi

scripts/docker-entrypoint.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ EOSQL
138138
setup_backup_system() {
139139
log "INFO" "Setting up backup system..."
140140

141+
# Configure pgBackRest stanza
142+
log "INFO" "Configuring pgBackRest..."
143+
if ! configure_pgbackrest_stanza; then
144+
log "ERROR" "Failed to configure pgBackRest stanza"
145+
return 1
146+
fi
147+
141148
# Setup cron jobs
142149
if ! /backup/scripts/setup-cron.sh; then
143150
log "ERROR" "Failed to setup cron jobs"

0 commit comments

Comments
 (0)