A handy Docker container to periodically backup PostgreSQL to S3
233
This project comes as a pre-built docker image that enables you to easily periodically back up a PostgreSQL database to AWS S3, and to restore from the backup as needed.
16.SCHEDULE variable determines backup frequency. See go-cron schedules documentation here. Omit to run the backup immediately and then exit.PASSPHRASE is provided, the backup will be encrypted using GPG.docker exec <container name> sh backup.sh to trigger a backup ad-hoc.BACKUP_KEEP_DAYS is set, backups older than this many days will be deleted from S3.S3_ENDPOINT if you're using a non-AWS S3-compatible storage provider.# Postgres
POSTGRES_DATABASE=database
POSTGRES_USER=user
POSTGRES_PASSWORD=password
# Backup
SCHEDULE=@weekly
BACKUP_KEEP_DAYS=7
PASSPHRASE=wxHw26GJZQBDenA8
S3_REGION=us-east-1
S3_BUCKET=my-s3-bucket
S3_PREFIX=prefix
S3_ACCESS_KEY_ID=AKIA3M3ZKBJPQUBT2UK6
S3_SECRET_ACCESS_KEY=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
POSTGRES_HOST=postgres
docker-compose.ymlversion: "3"
services:
postgres:
container_name: postgres
restart: unless-stopped
image: postgres:16
environment:
- POSTGRES_DATABASE=${POSTGRES_DATABASE:-database}
- POSTGRES_USER=${POSTGRES_USER:-user}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password}
networks:
- network-bridge
backup:
container_name: postgres-backup
restart: unless-stopped
image: walruship/postgres-backup-s3:16
environment:
- SCHEDULE=${SCHEDULE:-@weekly}
- BACKUP_KEEP_DAYS=${BACKUP_KEEP_DAYS:-7}
- PASSPHRASE=${PASSPHRASE:-Passphrase}
- S3_REGION=${S3_REGION:-Region}
- S3_BUCKET=${S3_BUCKET:-Bucket}
- S3_PREFIX=${S3_PREFIX:-Prefix}
- S3_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID:-Key}
- S3_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY:-Secret}
- POSTGRES_HOST=${POSTGRES_HOST:-Postgres}
depends_on:
- postgres
networks:
- network-bridge
networks:
network-bridge:
driver: bridge
WARNING: DATA LOSS! All database objects will be dropped and re-created.
docker exec <container name> sh restore.sh
NOTE: If your bucket has more than 1000 files, the latest may not be restored -- only one S3
lscommand is used
docker exec <container name> sh restore.sh <timestamp>
Content type
Image
Digest
sha256:86b7c9369…
Size
67.1 MB
Last updated
almost 3 years ago
docker pull walruship/postgres-backup-s3:16