Add PostgreSQL with automated hot backup setup
Weekly full backups via pg_basebackup and continuous WAL archiving for point-in-time recovery, running as Docker Compose services. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
|
||||
services:
|
||||
postgres-db:
|
||||
image: postgres:18
|
||||
container_name: postgres-db
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
POSTGRES_DB: ${POSTGRES_DB}
|
||||
ports:
|
||||
- "${PGPORT}:5432"
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql
|
||||
- /data/backup/postgres/wal:/backup/wal
|
||||
- ./init-replication-hba.sh:/docker-entrypoint-initdb.d/init-replication-hba.sh:ro
|
||||
command:
|
||||
- postgres
|
||||
- -c
|
||||
- wal_level=replica
|
||||
- -c
|
||||
- archive_mode=on
|
||||
- -c
|
||||
- archive_command=test ! -f /backup/wal/%f && cp %p /backup/wal/%f
|
||||
|
||||
postgres-backup:
|
||||
image: postgres:18
|
||||
container_name: postgres-backup
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
- postgres-db
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
PGPASSWORD: ${POSTGRES_PASSWORD}
|
||||
volumes:
|
||||
- /data/backup/postgres:/backup
|
||||
entrypoint: ["/bin/bash", "-c"]
|
||||
command:
|
||||
- |
|
||||
mkdir -p /backup/wal /backup/full
|
||||
while true; do
|
||||
STAMP=$$(date +%Y-%m-%d_%H-%M-%S)
|
||||
echo "Starting full backup: full_$$STAMP"
|
||||
pg_basebackup -h postgres-db -U "$$POSTGRES_USER" -D "/backup/full/full_$$STAMP" -Fp -Xs -P
|
||||
echo "Full backup complete: full_$$STAMP"
|
||||
sleep 604800
|
||||
done
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
Reference in New Issue
Block a user