Sign inSign up

tehsu/docker-postgres-backup-local

By tehsu

•Updated over 5 years ago

Image
0

512

tehsu/docker-postgres-backup-local repository overview

Docker Pulls

⁠postgres-backup-local

Backup PostgresSQL to the local filesystem with periodic rotating backups, based on schickling/postgres-backup-s3⁠. Backup multiple databases from the same host by setting the database names in POSTGRES_DB separated by commas or spaces.

Supports the following Docker architectures: linux/amd64, linux/arm64, linux/arm/v7.

⁠Usage

Docker:

docker run -u postgres:postgres -e POSTGRES_HOST=postgres -e POSTGRES_DB=dbname -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password  prodrigestivill/postgres-backup-local

Docker Compose:

version: '2'
services:
    postgres:
        image: postgres
        restart: always
        environment:
            - POSTGRES_DB=database
            - POSTGRES_USER=username
            - POSTGRES_PASSWORD=password
         #  - POSTGRES_PASSWORD_FILE=/run/secrets/db_password <-- alternative for POSTGRES_PASSWORD (to use with docker secrets)
    pgbackups:
        image: prodrigestivill/postgres-backup-local
        restart: always
        user: postgres:postgres
        volumes:
            - /var/opt/pgbackups:/backups
        links:
            - postgres
        depends_on:
            - postgres
        environment:
            - POSTGRES_HOST=postgres
            - POSTGRES_DB=database
            - POSTGRES_USER=username
            - POSTGRES_PASSWORD=password
         #  - POSTGRES_PASSWORD_FILE=/run/secrets/db_password <-- alternative for POSTGRES_PASSWORD (to use with docker secrets)
            - POSTGRES_EXTRA_OPTS=-Z9 --schema=public --blobs
            - SCHEDULE=@daily
            - BACKUP_KEEP_DAYS=7
            - BACKUP_KEEP_WEEKS=4
            - BACKUP_KEEP_MONTHS=6
            - HEALTHCHECK_PORT=8080

For security reasons it is recommended to run it as user postgres:postgres.

In case of running as postgres user, the system administrator must initialize the permission of the destination folder as follows:

# for default images (debian)
mkdir -p /var/opt/pgbackups && chown -R 999:999 /var/opt/pgbackups
# for alpine images
mkdir -p /var/opt/pgbackups && chown -R 70:70 /var/opt/pgbackups
⁠Environment Variables

Most variables are the same as in the official postgres image⁠.

env variabledescription
BACKUP_DIRDirectory to save the backup at. Defaults to /backups.
BACKUP_SUFFIXFilename suffix to save the backup. Defaults to .sql.gz.
BACKUP_KEEP_DAYSNumber of daily backups to keep before removal. Defaults to 7.
BACKUP_KEEP_WEEKSNumber of weekly backups to keep before removal. Defaults to 4.
BACKUP_KEEP_MONTHSNumber of monthly backups to keep before removal. Defaults to 6.
HEALTHCHECK_PORTPort listening for cron-schedule health check. Defaults to 8080.
POSTGRES_DBComma or space separated list of postgres databases to backup. Required.
POSTGRES_DB_FILEAlternative to POSTGRES_DB, but with one database per line, for usage with docker secrets.
POSTGRES_EXTRA_OPTSAdditional options for pg_dump. Defaults to -Z9.
POSTGRES_CLUSTERSet to TRUE in order to use pg_dumpall instead. Also set POSTGRES_EXTRA_OPTS to any value or empty since the default value is not compatible with pg_dumpall.
POSTGRES_HOSTPostgres connection parameter; postgres host to connect to. Required.
POSTGRES_PASSWORDPostgres connection parameter; postgres password to connect with. Required.
POSTGRES_PASSWORD_FILEAlternative to POSTGRES_PASSWORD, for usage with docker secrets.
POSTGRES_PASSFILE_STOREAlternative to POSTGRES_PASSWORD in passfile format⁠, for usage with postgres clusters.
POSTGRES_PORTPostgres connection parameter; postgres port to connect to. Defaults to 5432.
POSTGRES_USERPostgres connection parameter; postgres user to connect with. Required.
POSTGRES_USER_FILEAlternative to POSTGRES_USER, for usage with docker secrets.
SCHEDULECron-schedule⁠ specifying the interval between postgres backups. Defaults to @daily.
⁠Special Environment Variables

This variables are not intended to be used for normal deployment operations:

env variabledescription
POSTGRES_PORT_5432_TCP_ADDRSets the POSTGRES_HOST when the latter is not set.
POSTGRES_PORT_5432_TCP_PORTSets POSTGRES_PORT when POSTGRES_HOST is not set.
⁠Manual Backups

By default this container makes daily backups, but you can start a manual backup by running /backup.sh.

This script as example creates one backup as the running user and saves it the working folder.

docker run --rm -v "$PWD:/backups" -u "$(id -u):$(id -g)" -e POSTGRES_HOST=postgres -e POSTGRES_DB=dbname -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password  prodrigestivill/postgres-backup-local /backup.sh
⁠Automatic Periodic Backups

You can change the SCHEDULE environment variable in -e SCHEDULE="@daily" to alter the default frequency. Default is daily.

More information about the scheduling can be found here⁠.

Folders daily, weekly and monthly are created and populated using hard links to save disk space.

⁠Restore examples

Some examples to restore/apply the backups.

⁠Restore locally

Replace the backupfile name, $CONTAINER, $USERNAME and $DBNAME from the following command:

zcat backupfile.sql.gz | docker exec --tty --interactive $CONTAINER psql --username=$USERNAME --dbname=$DBNAME -W
⁠Restore to a remote server

Replace the backupfile name, $VERSION, $HOSTNAME, $PORT, $USERNAME and $DBNAME from the following command:

zcat backupfile.sql.gz | docker run --rm --tty --interactive postgres:$VERSION psql --host=$HOSTNAME --port=$PORT --username=$USERNAME --dbname=$DBNAME -W

Tag summary

Content type

Image

Digest

Size

65.6 MB

Last updated

over 5 years ago

docker pull tehsu/docker-postgres-backup-local