Red floppy disk labled with the date 12/11/03

Scheduled Postgres Backups to S3

A managed RDS is not exactly cheap. And I like my pet projects to be cheap. Thatโ€˜s how I ran into the problem of making scheduled backups of a Postgres DB. I canโ€˜t be the only one, so here is one way of solving it with a docker image.

๐Ÿ‘‰ Link to the repository

Inside the Docker Container

Within the running container this happens:

  • pg_dump creates a dump
  • aws-cli uploads the dump to s3
  • cron schedules a job with the two steps above to run every x minutes/houres/etc.

Pretty straight forward. But there is a catch.

Version Mismatch

The pg_dump command may not work across Postgres versions. Meaning you would want the pg_dump command to match the version of the Postgres database you pull a dump from.

Therefore building this image requires the build arg POSTGRES_IMAGE_TAG.
Here is an example of how building the image could look like for Postgres v13.5:

docker build --build-arg "POSTGRES_IMAGE_TAG=13.5" -t pg_dump_s3 .

Internally it pulls the official Postgres image matching the provided tag. Here is a list of available tags for official Postgres images.

Note that this docker image does not work for all of them. E.g. *-alpine images donโ€˜t work. But there is at least one that does for every major version from v9 to v14. Have a look at this list of tested tags.

See the README for detailed information on how to build the image and run the container.

Feel free to open PRs or issues ๐Ÿ™‚

More Posts

Menu Icon