postgresql-backup-restore
Service to backup and/or restore a PostgreSQL database to/from S3
How to use it
- Create an S3 bucket to hold your backups
- Turn versioning on for that bucket
- Supply all appropriate environment variables
- Run a backup and check your bucket for that backup
Environment variables
MODE
Valid values: backup
, restore
DB_HOST
hostname of the database server
DB_NAME
name of the database
DB_OPTIONS
optional arguments to supply to the backup or restore commands
DB_ROOTPASSWORD
password for the DB_ROOTUSER
DB_ROOTUSER
database administrative user, typically "postgres" for PostgreSQL databases
DB_USERPASSWORD
password for the DB_USER
DB_USER
user that accesses the database (PostgreSQL "role")
AWS_ACCESS_KEY
used for S3 interactions
AWS_SECRET_KEY
used for S3 interactions
S3_BUCKET
e.g., s3://database-backups NOTE: no trailing slash
It's recommended that your S3 bucket have versioning turned on.
Docker Hub
This image is built automatically on Docker Hub as silintl/postgresql-backup-restore
Playing with it locally
You'll need Docker, Docker Compose, and Make.
- Copy
local.env.dist
tolocal.env
. - Edit
local.env
to supply values for the variables. - Ensure you have a
gz
dump in your S3 bucket to be used for testing. A test database is provided as part of this project in thetest
folder. You can copy it to S3 as follows:
aws s3 cp test/world.sql.gz ${S3_BUCKET}/world.sql.gz
make db
# creates the Postgres DB servermake restore
# restores the DB dump filedocker ps -a
# get the Container ID of the exited restore containerdocker logs <containerID>
# review the restoration log messagesmake backup
# create a new DB dump filedocker ps -a
# get the Container ID of the exited backup containerdocker logs <containerID>
# review the backup log messagesmake restore
# restore the DB dump file from the new backupdocker ps -a
# get the Container ID of the exited restore containerdocker logs <containerID>
# review the restoration log messagesmake clean
# remove containers and networkdocker volume ls
# find the volume ID of the Postgres data containerdocker volume rm <volumeID>
# remove the data volumedocker images
# list existing imagesdocker image rm <imageID ...>
# remove images no longer needed