Sign inSign up

walruship/postgres-backup-s3

By walruship

Updated almost 3 years ago

A handy Docker container to periodically backup PostgreSQL to S3

Image
0

233

walruship/postgres-backup-s3 repository overview

Postgres-Backup-S3

This project comes as a pre-built docker image that enables you to easily periodically back up a PostgreSQL database to AWS S3, and to restore from the backup as needed.

Features

  • Images are tagged by the major Postgres version supported: 16.
  • The SCHEDULE variable determines backup frequency. See go-cron schedules documentation here. Omit to run the backup immediately and then exit.
  • If PASSPHRASE is provided, the backup will be encrypted using GPG.
  • Run docker exec <container name> sh backup.sh to trigger a backup ad-hoc.
  • If BACKUP_KEEP_DAYS is set, backups older than this many days will be deleted from S3.
  • Set S3_ENDPOINT if you're using a non-AWS S3-compatible storage provider.

Quick Setup

  1. Install Docker and Docker-Compose
  1. Edit environment variable
# Postgres
POSTGRES_DATABASE=database
POSTGRES_USER=user
POSTGRES_PASSWORD=password

# Backup
SCHEDULE=@weekly
BACKUP_KEEP_DAYS=7
PASSPHRASE=wxHw26GJZQBDenA8
S3_REGION=us-east-1
S3_BUCKET=my-s3-bucket
S3_PREFIX=prefix
S3_ACCESS_KEY_ID=AKIA3M3ZKBJPQUBT2UK6
S3_SECRET_ACCESS_KEY=BNcXdm18XMctzMH87PZLm8UoP6WlegcPvsQbF5TH
POSTGRES_HOST=postgres
  1. Example docker-compose.yml
version: "3"

services:
  postgres:
    container_name: postgres
    restart: unless-stopped
    image: postgres:16
    environment:
      - POSTGRES_DATABASE=${POSTGRES_DATABASE:-database}
      - POSTGRES_USER=${POSTGRES_USER:-user}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password}
    networks:
      - network-bridge
      
  backup:
    container_name: postgres-backup
    restart: unless-stopped
    image: walruship/postgres-backup-s3:16
    environment:
      - SCHEDULE=${SCHEDULE:-@weekly}
      - BACKUP_KEEP_DAYS=${BACKUP_KEEP_DAYS:-7}
      - PASSPHRASE=${PASSPHRASE:-Passphrase}
      - S3_REGION=${S3_REGION:-Region}
      - S3_BUCKET=${S3_BUCKET:-Bucket}
      - S3_PREFIX=${S3_PREFIX:-Prefix}
      - S3_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID:-Key}
      - S3_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY:-Secret}
      - POSTGRES_HOST=${POSTGRES_HOST:-Postgres}
    depends_on:
      - postgres
    networks:
      - network-bridge

networks:
  network-bridge:
    driver: bridge

Restore

WARNING: DATA LOSS! All database objects will be dropped and re-created.

... from latest backup
docker exec <container name> sh restore.sh

NOTE: If your bucket has more than 1000 files, the latest may not be restored -- only one S3 ls command is used

... from specific backup
docker exec <container name> sh restore.sh <timestamp>

Security

  • If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

Tag summary

Content type

Image

Digest

sha256:86b7c9369

Size

67.1 MB

Last updated

almost 3 years ago

docker pull walruship/postgres-backup-s3:16