Sign inSign up

dimitriosdockerhub/backup-postgres

By dimitriosdockerhub

•Updated over 2 years ago

PostgreSQL, cron and tini. Schedule database dumps.

Image
Databases & storage
0

210

dimitriosdockerhub/backup-postgres repository overview

⁠Docker Compose

The following is an example of a docker-compose.yml file that can be used to create a PostgreSQL container and a backup container. The backup container will run a cron job to backup the PostgreSQL database.

If the dump fails, the error file will contain the error message. Otherwise, it will be empty. You may want to check the error file to see if the dump was successful using monitoring tools like Zabbix or Prometheus.

version: "3.9"

services:
  postgres:
    container_name: ${POSTGRES_CONTAINER_NAME:-postgres}
    image: postgres:${POSTGRES_IMAGE_VERSION:-latest}
    hostname: ${POSTGRES_CONTAINER_NAME:-postgres}
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-default_database}
      POSTGRES_USER: ${POSTGRES_USER:-user}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "${POSTGRES_EXT_PORT:-10000}:5432" # for development, close in production
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s
    networks:
      - postgres_network

  backup_postgres:
    container_name: backup_${POSTGRES_CONTAINER_NAME:-postgres}
    image: dimitriosdockerhub/backup-postgres:${POSTGRES_IMAGE_VERSION:-latest}
    hostname: backup_${POSTGRES_CONTAINER_NAME:-postgres}
    restart: unless-stopped
    depends_on:
      - postgres
    volumes:
      - ${POSTGRES_PATH_TO_DUMP:-/backups}:/backups:rw
    environment:
      CRONTAB: |
        ${POSTGRES_CRONTAB:-50 5 * * *} bash -c "PGPASSWORD=${POSTGRES_PASSWORD} pg_dumpall --host=${POSTGRES_CONTAINER_NAME:-postgres} --user=${POSTGRES_USER:-user} > ${POSTGRES_PATH_TO_DUMP:-/backups}/${POSTGRES_BACKUP_NAME:-all-databases-backup}.sql 2>${POSTGRES_PATH_TO_DUMP:-/backups}/${POSTGRES_BACKUP_NAME:-backup-name}.err && gzip -9 -f ${POSTGRES_PATH_TO_DUMP:-/backups}/${POSTGRES_BACKUP_NAME:-all-databases-backup}.sql"
    healthcheck:
      test: ["CMD-SHELL", "pidof cron && command -v pg_dumpall"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s
    networks:
      - postgres_network

volumes:
  postgres_data:

networks:
  postgres_network:
    driver: bridge

⁠Environment Variables

The following environment variables can be used to configure the PostgreSQL container and the backup container:

POSTGRES_CONTAINER_NAME=postgres-container
POSTGRES_IMAGE_VERSION=latest
POSTGRES_DATABASE=default_database
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_EXT_PORT=10002
POSTGRES_PATH_TO_DUMP=/backups
POSTGRES_BACKUP_NAME=all-databases-backup
POSTGRES_CRONTAB=*/2 * * * *

Tag summary

Content type

Image

Digest

sha256:bab9939b4…

Size

150.2 MB

Last updated

over 2 years ago

docker pull dimitriosdockerhub/backup-postgres