A Docker container that automatically backs up PostgreSQL databases and uploads them to Amazon S3.
This project was inspired by and builds upon the excellent work of Musab520 and their pgbackup-sidecar project. We extend our gratitude for the original implementation that served as the foundation for this enhanced version.
pg_dump
POSTGRES_HOST
- PostgreSQL server hostnamePOSTGRES_PORT
- PostgreSQL server port (default: 5432)POSTGRES_DB
- Database name to backupPOSTGRES_USER
- PostgreSQL usernamePOSTGRES_PASSWORD
- PostgreSQL passwordTITLE
- Title for webhook notificationsCRON_TIME
- Cron schedule for backups (e.g., "0 2 * * *" for daily at 2 AM)ENCRYPTION_KEY
- Encryption passphrase/key (if not set, backups will not be encrypted)ENCRYPTION_METHOD
- Encryption method: gpg
(default) or openssl
COMPRESSION_TYPE
- Additional compression: none
(default), gzip
, bzip2
, or xz
Note: PostgreSQL's --format=custom
already includes compression. Additional compression is applied on top of this for extra space savings, but may increase backup time.
CLEANUP_ENABLED
- Control whether to clean up local backup files after processing: true
(default) or false
Note: When true
, all local backup files are removed after processing. When false
, only intermediate files are cleaned up and the final backup file is retained locally.
S3_BUCKET
- S3 bucket name for storing backupsS3_PREFIX
- S3 key prefix (default: "backups")S3_OPTIONS
- Additional AWS CLI options (e.g., --storage-class STANDARD_IA
)Note: Local backup files are automatically cleaned up after processing, regardless of S3 configuration. This ensures your system doesn't accumulate backup files over time.
Webhook Configuration (Optional)WEBHOOK_URL
- URL to send backup status notificationsAWS_ACCESS_KEY_ID=your-access-key AWS_SECRET_ACCESS_KEY=your-secret-key AWS_DEFAULT_REGION=us-east-1Method 2: IAM Role (Recommended for EC2/ECS)
AWS_ROLE_ARN=arn:aws:iam::123456789012:role/BackupRoleMethod 3: Instance Profile
No additional environment variables needed when running on EC2 with an instance profile.
For a complete working example, see the docker-compose.yml
file in this repository which demonstrates all configuration options including database setup, encryption, compression, and optional S3 upload.
.pgdump.gpg
or .pgdump.gz.gpg
(if compressed).pgdump.enc
or .pgdump.gz.enc
(if compressed)gpg --batch --yes --passphrase "your-passphrase" --decrypt backup.pgdump.gz.gpg | gunzip > backup.pgdump
openssl enc -aes-256-cbc -d -in backup.pgdump.gz.enc -k "your-passphrase" | gunzip > backup.pgdump
Backups are stored in S3 with the following structure:
# Unencrypted, no additional compression
s3://your-bucket/backups/hostname/2024-01-15T10:30:00+00:00.pgdump
# With gzip compression
s3://your-bucket/backups/hostname/2024-01-15T10:30:00+00:00.pgdump.gz
# With GPG encryption
s3://your-bucket/backups/hostname/2024-01-15T10:30:00+00:00.pgdump.gpg
# With both gzip compression and GPG encryption
s3://your-bucket/backups/hostname/2024-01-15T10:30:00+00:00.pgdump.gz.gpg
Your AWS user or role needs the following S3 permissions:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:PutObjectAcl" ], "Resource": "arn:aws:s3:::your-backup-bucket/*" } ] }
/opt/dumps
pg_restore -h localhost -U postgres -d restored_db backup.pgdumpWith Additional Compression
# For gzip gunzip backup.pgdump.gz pg_restore -h localhost -U postgres -d restored_db backup.pgdump # For bzip2 bunzip2 backup.pgdump.bz2 pg_restore -h localhost -U postgres -d restored_db backup.pgdump # For xz unxz backup.pgdump.xz pg_restore -h localhost -U postgres -d restored_db backup.pgdumpWith Encryption and Compression
# GPG + gzip example gpg --batch --yes --passphrase "your-passphrase" --decrypt backup.pgdump.gz.gpg | gunzip > backup.pgdump pg_restore -h localhost -U postgres -d restored_db backup.pgdump # OpenSSL + bzip2 example openssl enc -aes-256-cbc -d -in backup.pgdump.bz2.enc -k "your-passphrase" | bunzip2 > backup.pgdump pg_restore -h localhost -U postgres -d restored_db backup.pgdump
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4