@@ -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}
330342pg1-socket-path=${socket_dir}
331343pg1-port=${pg_port}
332344pg1-user=${pg_user}
333- pg1-database=postgres
345+ pg1-database=${pg_database}
334346EOF
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
0 commit comments