This image is based on official Postgres image so you can use everything mentioned there. The image regularly backups the database and uploads the backup into AWS S3 using infrequent storage class. The database is backed up every hour.
How to setup AWS S3 Bucket
- Create Bucket in your region e.g. com-yourcompany
- Create folder for database backups inside the bucket e.g. dbbackup
- Inside dbbackup folder create three other folders with names
- month - there will be monthly db backups kept forever
- day - there will be daily db backups kept for 64 days
- hour - there will be hourly db backups kept for 4 days
- The structure looks like
- com-yourcompany\dbbackup\month
- com-yourcompany\dbbackup\day
- com-yourcompany\dbbackup\hour
- Setup the expiration of daily and hourly backups
- Add Lifecycle rule on the bucket com-yourcompany
- Apply the Rule to A prefix: dbbackup/day
- Permanently Delete 64 Days after the object's creation date
- Create and Activate the rule
- Add second Lifecycle rule on the bucket com-yourcompany
- Apply the Rule to A prefix: dbbackup/hour
- Permanently Delete 4 Days after the object's creation date
- Create and Activate the rule
- Done
When starting the database with backup feature on you have to provide credentials for the upload through docker environment variables.
- AWS_KEY - access key to AWS S3
- AWS_SECRET - secret key to AWS S3
- S3BUCKET - AWS S3 bucket where the back ups will be stored
- S3REGION - region where the backup will be stored default value is us-east-1
Run command will looks like
docker run -d --name db -p 5432:5432 -e AWS_KEY=access_key -e AWS_SECRET=secret_key -e S3BUCKET=s3://com-yourcompany/dbbackup/ -e POSTGRES_USER=myapp -e POSTGRES_PASSWORD=my-password tonda100/postgres-s3
If the AWS_KEY, AWS_SECRET or S3BUCKET will not be set. The image will works like ordinary postgres docker image.
The database backup are are triggered by cron at:
- monthly backups - at 0:15 on 1st day of every month
- daily backups - at 0:30 every day
- hourly backups - at x:45 every hour
The image uses the Europe/Prague timezone as default one. The timezone could be change using environment variable TZ e.g. -e TZ=America/Toronto
Project is available on GitHub