Backup a dockerized Postgres database with Resticker

I got the question how to solve the problem that when backing up the storage of a dockerized Postgres database (using Resticker) it could be that the backup is corrupt because the database is still running.

This is an addition to my post of how to backup Docker volumes using resticker.

I had exactly the same problem. I wanted to backup a Postgres database running in a Docker container without having to stop the database or have a corrupt backup.

Therefore I have extended Resticker, so that it is possible to execute additional commands before the backup execution. To access other Docker containers, the Docker socket must be mounted. So it is possible to create a database dump and backup it instead of the volume of the database itself.

Here’s the relevant part from the Resticker documentation:

To do that add the following volume to the compose or swarm configuration:

- /var/run/docker.sock:/var/run/docker.sock

You can add one or multiple commands by specifying the following environment variable:

PRE_COMMANDS: |-
docker exec nextcloud-postgres pg_dumpall -U nextcloud -f /data/nextcloud.sql
docker exec other-postgres pg_dumpall -U other -f /data/other.sql
docker stop my_container

The commands specified in PRE_COMMANDS are executed one by one.

Before the backup starts, a command to dump the whole database to a file (using Postgres’ pq_dumpall) is executed. This file now contains a valid state of the database, that can easily get restored or even modified before a restore. Of course, the dump-file needs to be mounted to the Postgres container too.

Interactions & Comments