Here’s a script I cooked up after much trial and error that can be run as a cron script on a docker host machine to snapshot a full database dump out of a standard postgres
container.
The bits you’d want to change are double-quoted:
#!/bin/bash -e
# load RESTIC_ environment variables
set -a
HOME=/root
. /root/.resticrc
set +a
cd "/srv/myapp-prod"
docker-compose exec -T db \
bash -c 'pg_dumpall --clean -U$POSTGRES_USER' \
| gzip --rsyncable \
| restic backup \
--host "myapp-prod-db" \
--stdin --stdin-filename \
postgres.sql.gz
/root/.resticrc
looks like this:
RESTIC_REPOSITORY=/srv/restic
RESTIC_PASSWORD=abcdef1234567890
The call to pg_dumpall
is single-quoted and wrapped with bash so that environment variables defined on the container like $POSTGRES_USER
can be used.