Sign inSign up

peetvandesande/pg-backup

By peetvandesande

•Updated 9 months ago

PostgreSQL Backup sidecar image

Image
Databases & storage
0

2.6K

peetvandesande/pg-backup repository overview

⁠🐘 pg-backup

Docker Pulls Image Size GitHub License

Lightweight PostgreSQL backup container that creates timestamped database dumps.
No rotation, pruning, encryption, or cloud upload — bring your own retention policy.


ā šŸš€ Quick Usage

⁠One-off backup
docker run --rm \
  --network app_default \       # Needed to connect to PostgreSQL server
  -v /var/backups:/backups \
  -e POSTGRES_HOST=db \
  -e POSTGRES_USER=myuser \
  -e POSTGRES_PASSWORD=mypass \
  -e POSTGRES_DB=mydb \
  -e BACKUP_NAME_PREFIX=mydb \
  -e RUN_ONCE=1 \
  peetvandesande/pg-backup:alpine
⁠Scheduled backup ( daily at 02:00 by default )
docker run -d --name pg-backup \
  --network app_default \
  -v /var/backups:/backups \
  -e POSTGRES_HOST=db \
  -e POSTGRES_USER=myuser \
  -e POSTGRES_PASSWORD=mypass \
  -e POSTGRES_DB=mydb \
  -e BACKUP_NAME_PREFIX=mydb \
  peetvandesande/pg-backup:alpine

Override schedule:

-v ./crontab:/etc/crontabs/root:ro

To run a backup before cron starts:

-e RUN_BACKUP_ON_START=1

⁠🧰 Restore

Restore the latest matching dump:

docker run --rm \
  --network app_default \
  -v /var/backups:/backups \
  -e POSTGRES_HOST=db \
  -e POSTGRES_USER=myuser \
  -e POSTGRES_PASSWORD=mypass \
  -e POSTGRES_DB=mydb \
  peetvandesande/pg-backup:alpine restore

Restore a specific dump file:

docker run --rm \
  --network app_default \
  -v /var/backups:/backups \
  -e POSTGRES_HOST=db \
  -e POSTGRES_USER=myuser \
  -e POSTGRES_PASSWORD=mypass \
  -e POSTGRES_DB=mydb \
  peetvandesande/pg-backup:alpine restore /backups/mydb-20251106.sql.gz

ā āš™ļø Environment Variables

VariableDefaultDescription
POSTGRES_USERrequiredPostgreSQL username
POSTGRES_PASSWORDrequiredPostgreSQL password
POSTGRES_DBrequiredDatabase name to dump
POSTGRES_HOSTdbHostname or container name of PostgreSQL server
POSTGRES_PORT5432PostgreSQL port
BACKUP_NAME_PREFIXpostgres-$POSTGRES_DBPrefix for dump filenames
BACKUPS_DIR/backupsDirectory for dump storage
COMPRESSgzCompression type: gz, bz2, zst, or none
COMPRESS_LEVEL(auto)Compression level for selected compressor
VERIFY_SHA25611 = write .sha256 checksum file
PRESERVE_TIMES1Preserve timestamps when creating archives
CHOWN_UID(unset)Apply user ownership (if set)
CHOWN_GID(unset)Apply group ownership (if set)
CHMOD_MODE(unset)Apply mode to dump and checksum files e.g. 0640
RUN_ONCE0Run backup once and exit
RUN_BACKUP_ON_START0Run backup before cron starts
DUMP_NO_OWNER0Omit db ownership account
DUMP_NO_PRIVILEGES0Omit db privileges
INCLUDE_GLOBALS1Include global privileges
RESTORE_STRIP_ACL0Strip ACLs during restore
⁠Ownership Logic Example
# Force UID=34, keep existing GID:
-e CHOWN_UID=34

# Force GID=34, keep existing UID:
-e CHOWN_GID=34

# Force both explicitly:
-e CHOWN_UID=34 -e CHOWN_GID=34

ā šŸ“¦ Output Format

/backups/
ā”œā”€ā”€ mydb-postgres-20251106.sql.gz
└── mydb-postgres-20251106.sql.gz.sha256

ā šŸ·ļø Tags

TagDescription
alpine (default)Smallest runtime image
devWork-in-progress branch images
<version>Tagged stable releases

⁠🐳 Docker Compose Example

services:
  pg-backup:
    image: peetvandesande/pg-backup:alpine
    environment:
      POSTGRES_HOST: db
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypass
      POSTGRES_DB: mydb
      BACKUP_NAME_PREFIX: mydb-postgres
      COMPRESS: gz
      VERIFY_SHA256: 1
    volumes:
      - ./backups:/backups
    depends_on:
      - db

  db:
    image: postgres:17-alpine
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypass
      POSTGRES_DB: mydb
    volumes:
      - ./data:/var/lib/postgresql/data

This setup runs a PostgreSQL container and a backup container side by side,
with backups stored under ./backups on the host.


Full docs and restore guide available here:
→ https://github.com/peetvandesande/pg-backup/tree/main/docs/⁠

Tag summary

Content type

Image

Digest

sha256:48621c5e6…

Size

8.1 MB

Last updated

9 months ago

docker pull peetvandesande/pg-backup