Skip to content

Commit da2757b

Browse files
committed
"Improve archive mode verification and use postgres user for pgBackRest"
1 parent abb5886 commit da2757b

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

scripts/backup-functions.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,14 @@ configure_pgbackrest_stanza() {
337337
# Verify that archive_mode is enabled
338338
log "INFO" "Verifying PostgreSQL archive mode configuration..."
339339
local archive_mode_check
340-
archive_mode_check=$(PGPASSWORD="$POSTGRES_PASSWORD" psql -h 127.0.0.1 -U "$pg_user" -d "$pg_database" -t -c "SHOW archive_mode;" 2>/dev/null | tr -d ' ')
340+
341+
# Try connecting as postgres user via socket first (most reliable)
342+
archive_mode_check=$(su-exec postgres psql -d "$pg_database" -t -c "SHOW archive_mode;" 2>/dev/null | tr -d ' ')
343+
344+
# If that fails, try with the configured user
345+
if [ -z "$archive_mode_check" ] || [ "$archive_mode_check" = "" ]; then
346+
archive_mode_check=$(PGPASSWORD="$POSTGRES_PASSWORD" psql -h 127.0.0.1 -U "$pg_user" -d "$pg_database" -t -c "SHOW archive_mode;" 2>/dev/null | tr -d ' ')
347+
fi
341348

342349
if [ "$archive_mode_check" != "on" ]; then
343350
log "ERROR" "Archive mode is not enabled (current: $archive_mode_check). This is required for pgBackRest."
@@ -354,7 +361,7 @@ configure_pgbackrest_stanza() {
354361
pg1-path=${pgdata}
355362
pg1-socket-path=${socket_dir}
356363
pg1-port=${pg_port}
357-
pg1-user=${pg_user}
364+
pg1-user=postgres
358365
pg1-database=${pg_database}
359366
EOF
360367

scripts/docker-entrypoint.sh

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,28 +142,51 @@ EOSQL
142142
return 1
143143
fi
144144

145-
# If this is a fresh initialization, restart PostgreSQL to apply archive settings
146-
if [ ! -f "$PGDATA/.archive_configured" ]; then
147-
log "INFO" "Restarting PostgreSQL to apply archive configuration..."
148-
145+
# Verify archive_mode is properly configured
146+
log "INFO" "Verifying archive_mode configuration..."
147+
148+
# Check if archive_mode is enabled
149+
archive_mode_status=$(su-exec postgres psql -d postgres -t -c "SHOW archive_mode;" 2>/dev/null | tr -d '[:space:]' || echo "")
150+
151+
if [ "$archive_mode_status" != "on" ]; then
152+
log "WARN" "Archive mode is not enabled (current: $archive_mode_status), restarting PostgreSQL..."
153+
149154
# Stop current PostgreSQL instance
150155
kill $POSTGRES_PID 2>/dev/null || true
151156
wait $POSTGRES_PID 2>/dev/null || true
152-
157+
158+
# Ensure archive settings are in postgresql.conf
159+
if ! grep -q "archive_mode = on" "$PGDATA/postgresql.conf"; then
160+
echo "archive_mode = on" >> "$PGDATA/postgresql.conf"
161+
fi
162+
if ! grep -q "wal_level = replica" "$PGDATA/postgresql.conf"; then
163+
echo "wal_level = replica" >> "$PGDATA/postgresql.conf"
164+
fi
165+
if ! grep -q "archive_command" "$PGDATA/postgresql.conf"; then
166+
echo "archive_command = 'pgbackrest --stanza=${PGBACKREST_STANZA:-main} archive-push %p'" >> "$PGDATA/postgresql.conf"
167+
fi
168+
153169
# Start PostgreSQL again
154170
su-exec postgres postgres &
155171
POSTGRES_PID=$!
156-
172+
157173
# Wait for PostgreSQL to be ready again
158174
if ! wait_for_postgres 120; then
159175
log "ERROR" "PostgreSQL failed to restart with archive configuration"
160176
kill $POSTGRES_PID 2>/dev/null || true
161177
return 1
162178
fi
163-
164-
# Mark archive as configured
165-
touch "$PGDATA/.archive_configured"
166-
log "INFO" "PostgreSQL restarted successfully with archive configuration"
179+
180+
# Verify archive_mode is now enabled
181+
archive_mode_status=$(su-exec postgres psql -d postgres -t -c "SHOW archive_mode;" 2>/dev/null | tr -d '[:space:]' || echo "")
182+
if [ "$archive_mode_status" = "on" ]; then
183+
log "INFO" "Archive mode successfully enabled"
184+
else
185+
log "ERROR" "Failed to enable archive mode (current: $archive_mode_status)"
186+
return 1
187+
fi
188+
else
189+
log "INFO" "Archive mode is already enabled"
167190
fi
168191

169192
log "INFO" "PostgreSQL is ready"

0 commit comments

Comments
 (0)