Skip to content

Latest commit

 

History

History
65 lines (57 loc) · 2.66 KB

README.md

File metadata and controls

65 lines (57 loc) · 2.66 KB

A (very) simple daily backup solution for PostgreSQL. Built around dcron and pg_dump
in a very small package - thanks to Alpine.

Usage

with docker-compose

version: "2.2"
services:
  db:
    image: postgres:11-alpine
    environment:
      POSTGRES_PASSWORD: "9p4O6ICIGf9hmdA1GJFwDN8"
  backup:
    image: 0xff/postgres-backup
    environment:
      PGPASSWORD: "9p4O6ICIGf9hmdA1GJFwDN8"
    volumes:
      - ./pg-backups:/pg-backups

And you will have a daly backup in ./pg-backups for 7 days.
After that, old backups will be overwritten.
This can be customized to some extent by environment variables.
See Dockerfile for defaults.
libpq environment variables will also be passed on.

Advanced usage

By default backups are made for 7 days as SQL dumps, wich is nice for readability but might not be what you want.
In that case you may want to use a different format, for example tar for a jear.
You can do so by changing the PGDUMPOPTIONS environment variable like:

  backup:
    environment:
      PGDUMPOPTIONS: "-F t -f /pg-backups/backup-$$(date +%j).tar"

The example above is for a docker-compose.yml file where you have to use $$ in order to write a single dollar sign.
Also note %j in the date format expression which means day of year.
So you will have backup files for a year until the first one is overwritten.

For a full example of using the custom format please have a look at example.yml.

Restore

docker exec into the container and use pg_restore or psql.

pg_restore with tar or custom format.

Inspect backup:

docker-compose exec backup pg_restore -l /pg-backups/backup-1.tar

Restore backup - and thereby destroy existing data - into postgres database:

docker-compose exec backup pg_restore -d postgres --clean --if-exists /pg-backups/backup-1.tar

psql for sql dumps

By default sql dumps are created with --clean --if-exists wich will ovrwrite existing data in this case.

docker-compose exec backup psql -f /pg-backups/backup-1.sql

Why

I really tried hard to find a simple postgres-backup solution but I did not find any.
If someone finds something similar please drop me a line - i'd appreciate it.