Sign inSign up

evoweb/postgres-cron-backup

By evoweb

•Updated 11 days ago

Periodic pg_dump backups of PostgreSQL databases, scheduled by cron

Image
0

4.5K

evoweb/postgres-cron-backup repository overview

⁠postgres-cron-backup

Periodic pg_dump backups of your PostgreSQL databases, scheduled by cron inside the container. Backups are gzipped and written to /backup — mount a host directory or a volume there.

Based on the work of Javier Delgado⁠. Source and issues: github.com/evoWeb/docker-postgres-cron-backup⁠

⁠Tags

Pick the tag matching the major version of your PostgreSQL server, so pg_dump and psql are compatible with it.

TagPostgreSQL client
18, 18.x, 18.x-<release>18
17, 17.x, 17.x-<release>17
16, 16.x, 16.x-<release>16

Platforms: linux/amd64, linux/arm64, linux/arm/v7

⁠Quick start

docker container run -d \
    --env POSTGRES_HOST=my_postgres \
    --env POSTGRES_USER=postgres \
    --env POSTGRES_PASSWORD=my_password \
    --network my_network \
    --volume /path/to/backups:/backup \
    evoweb/postgres-cron-backup:18

⁠docker compose

services:
    postgres:
        image: postgres:18
        volumes:
            - data:/var/lib/postgresql
        environment:
            POSTGRES_DB: ${POSTGRES_DB}
            POSTGRES_USER: ${POSTGRES_USER}
            POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
        restart: unless-stopped

    backup:
        image: evoweb/postgres-cron-backup:18
        depends_on:
            - postgres
        volumes:
            - ./backup:/backup
        environment:
            POSTGRES_HOST: postgres
            POSTGRES_DB: ${POSTGRES_DB}
            POSTGRES_USER: ${POSTGRES_USER}
            POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
            MAX_BACKUPS: 15
            INIT_BACKUP: 1
            CRON_TIME: 0 3 * * *
            GZIP_LEVEL: 9
        restart: unless-stopped

volumes:
    data:

Credentials can also be passed as docker secrets⁠ via the *_FILE variables, e.g. POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password.

⁠Environment variables

VariableDescriptionDefault
POSTGRES_HOST / POSTGRES_HOST_FILEHost of the PostgreSQL serverpostgres
POSTGRES_PORTPort of the PostgreSQL server5432
POSTGRES_USER / POSTGRES_USER_FILEDatabase user—
POSTGRES_PASSWORD / POSTGRES_PASSWORD_FILEDatabase password—
POSTGRES_DB / POSTGRES_DB_FILEDatabase(s) to back up; the file may list one name per lineall databases
PG_DUMP_OPTSAdditional pg_dump⁠ arguments--rows-per-insert=1
PSQL_SSL_OPTSArguments to connect via SSL⁠—
CRON_TIMECron schedule of the backup0 3 * * sun
TZContainer timezone, e.g. Europe/BerlinUTC
MAX_BACKUPSNumber of backups to keep per databaseunlimited
INIT_BACKUPCreate a backup on container start (1)—
INIT_RESTORE_LATESTRestore the latest backup on container start—
EXIT_BACKUPCreate a backup on container stop—
TIMEOUTTime to wait for the database on startup10s
GZIP_LEVELgzip compression level, 1 (fast) to 9 (small)6
USE_PLAIN_SQLWrite and restore plain SQL instead of gzip—

Backups are named <YYYYMMDDHHMM>.<database>.sql.gz, and latest.<database>.sql.gz links to the most recent one.

⁠Restore

# List backups
docker container exec <container> ls /backup

# Restore a backup; the database name is read from the dump if POSTGRES_DB is not set
docker container exec <container> /Scripts/restore.sh /backup/latest.<database>.sql.gz

⁠Healthcheck

The container is healthy once the init phase (INIT_BACKUP / INIT_RESTORE_LATEST) is done and turns unhealthy within three minutes when a backup fails. A backup fails when no database is found or pg_dump/gzip exits with an error; the incomplete dump is removed and existing backups are kept, so MAX_BACKUPS never rotates out a good backup in favor of a broken one.

Tag summary

Content type

Image

Digest

sha256:fd0d1e1d1…

Size

13.2 MB

Last updated

over 1 year ago

docker pull evoweb/postgres-cron-backup