Sign inSign up

myros/postgres-backup

By myros

Updated over 8 years ago

Backup PostgresSQL to local filesystem or S3 with periodic backups and Slack notifying

Image
0

968

myros/postgres-backup repository overview

Automated Docker PostgreSQL Backup

This Docker image runs pg_dump to backup one or multiple databases using cron job to folder /backups and notifies Slack.

If SLACK vars are set, SLACK channel will be notified.

Features

  • Backup multiple Postgres databases
  • Slack notification
  • Automatic, scheduled backups
  • Nice documentation and samples
  • Docker-compose file included

Usage:

docker run -d \
    --env PG_HOST=postgres \
    --env PG_PORT=5432 \
    --env PG_USER=docker \
    --env PG_PASSWORD=docker \
    --volume /host_folder:/backups
    myros/postgres-backup

Periodic backups

You can change the SCHEDULE environment variable like -e SCHEDULE="@daily" or SCHEDULE="@every 6h" to change its default frequency, by default is daily.

More information about the scheduling can be found here.

Required variables

VariableDescription
PG_HOSTthe host/ip of your postgres database
PG_PORTthe port number of your postgres database
PG_USERthe username of your postgres database
PG_PASSWORDthe password of your postgres database
PG_DBthe database name(s) to dump. If you want to use multiple databases, delimit with ";"
Optional variables
VariableDescription
PG_EXTRA_OPTSthe extra options to pass to pg_dump command
SCHEDULEthe interval of cron job to run pg_dump. @daily by default, which is every day at 00:00.
Slack variables (optional)
VariableDescription
SLACK_CHANNELSlack channel
SLACK_HOSTSlack webhook
SLACK_MESSAGEMessage that will be posted. Default message: Database backup done (2018.03.03) (database1; database2...)

Docker compose

version: "2"

services:
  pgbackups:
    image: postgres-backup
    volumes:
      - ~/backups/pg:/backups
    environment:
      PG_HOST: postgres
      PG_PORT: 5432
      PG_USER: docker
      PG_PASSWORD: docker
      PG_DB: database1; database2; database3
      PG_EXTRA_OPTS: "-w -Fc"
      SCHEDULE: "@daily"
      SLACK_CHANNEL: "channel"
      SLACK_HOST: "https://hooks.slack.com/services/YOUR_DATA"

networks:
  default:
    external:
      name: NAME_OF_YOUR_POSTGRES_NETWORK

One time backup examples

Just backup
  docker run -e PG_HOST=postgres \
    -e PG_DB=database_name \
    -e PG_USER=docker \
    -e PG_PASSWORD=docker \
    -e PG_PORT=5432 \
    myros/postgres-backup /backup.sh
With network
  docker run -e PG_HOST=postgres \
    -e PG_DB=database_name \
    -e PG_USER=docker \
    -e PG_PASSWORD=docker \
    -e PG_PORT=5432 \
    --network YOUR_PG_NETWORK \
    myros/postgres-backup /backup.sh
With network and Slack
  docker run -e PG_HOST=postgres \
    -e PG_DB=database_name \
    -e PG_USER=docker \
    -e PG_PASSWORD=docker \
    -e PG_PORT=5432 \
    -e SLACK_HOST=SLACK_WEBHOOK_URL \
    -e SLACK_CHANNEL=backups --network YOUR_PG_NETWORK \
    myros/postgres-backup /backup.sh

Restore from a backup

To see the list of backups, you can run:

docker exec docker-postgres-backup ls /backups

To restore database from a certain backup, simply run:

    docker run docker run -e PG_HOST=postgres -e PG_DB=dbname -e PG_USER=user -e PG_PASSWORD=password  myros/docker-postgres-backup /restore.sh /backups/PATH_TO_YOUR_FILE

TODO

  • S3 upload/download
  • sending emails
  • proper restore & import

Tag summary

Content type

Image

Digest

Size

95.6 MB

Last updated

over 8 years ago

docker pull myros/postgres-backup