Skip to content

Commit abb5886

Browse files
committed
Enable WAL archiving and add backup performance settings
1 parent c473d12 commit abb5886

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

scripts/backup-functions.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,19 @@ configure_pgbackrest_stanza() {
334334
return 1
335335
fi
336336

337+
# Verify that archive_mode is enabled
338+
log "INFO" "Verifying PostgreSQL archive mode configuration..."
339+
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 ' ')
341+
342+
if [ "$archive_mode_check" != "on" ]; then
343+
log "ERROR" "Archive mode is not enabled (current: $archive_mode_check). This is required for pgBackRest."
344+
log "ERROR" "Please ensure archive_mode=on is set in postgresql.conf and PostgreSQL has been restarted."
345+
return 1
346+
fi
347+
348+
log "INFO" "Archive mode is enabled: $archive_mode_check"
349+
337350
# Create stanza configuration
338351
cat >> /etc/pgbackrest/pgbackrest.conf << EOF
339352

scripts/docker-entrypoint.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ start_postgresql() {
6666
echo "log_directory = 'log'" >> "$PGDATA/postgresql.conf"
6767
echo "log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'" >> "$PGDATA/postgresql.conf"
6868

69+
# Enable WAL archiving for pgBackRest (CRITICAL for backup functionality)
70+
echo "wal_level = replica" >> "$PGDATA/postgresql.conf"
71+
echo "archive_mode = on" >> "$PGDATA/postgresql.conf"
72+
echo "archive_command = 'pgbackrest --stanza=${PGBACKREST_STANZA:-main} archive-push %p'" >> "$PGDATA/postgresql.conf"
73+
echo "max_wal_senders = 3" >> "$PGDATA/postgresql.conf"
74+
echo "wal_keep_size = 1GB" >> "$PGDATA/postgresql.conf"
75+
76+
# Additional settings for better backup performance
77+
echo "checkpoint_completion_target = 0.9" >> "$PGDATA/postgresql.conf"
78+
echo "wal_buffers = 16MB" >> "$PGDATA/postgresql.conf"
79+
echo "checkpoint_timeout = 15min" >> "$PGDATA/postgresql.conf"
80+
6981
# Set up pg_hba.conf for authentication
7082
{
7183
echo "local all all trust"
@@ -130,6 +142,30 @@ EOSQL
130142
return 1
131143
fi
132144

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+
149+
# Stop current PostgreSQL instance
150+
kill $POSTGRES_PID 2>/dev/null || true
151+
wait $POSTGRES_PID 2>/dev/null || true
152+
153+
# Start PostgreSQL again
154+
su-exec postgres postgres &
155+
POSTGRES_PID=$!
156+
157+
# Wait for PostgreSQL to be ready again
158+
if ! wait_for_postgres 120; then
159+
log "ERROR" "PostgreSQL failed to restart with archive configuration"
160+
kill $POSTGRES_PID 2>/dev/null || true
161+
return 1
162+
fi
163+
164+
# Mark archive as configured
165+
touch "$PGDATA/.archive_configured"
166+
log "INFO" "PostgreSQL restarted successfully with archive configuration"
167+
fi
168+
133169
log "INFO" "PostgreSQL is ready"
134170
return 0
135171
}

0 commit comments

Comments
 (0)