pg-clusterbackup — Automated Multi-Cluster Backup for PostgreSQL

pg-clusterbackup is an open-source tool for automated, generation-based backups of every running PostgreSQL cluster on a host (as of November 2026, version 1.8.1). It discovers active instances automatically, dumps each database individually in custom format (.cus) alongside a separate globals.sql, and rotates old generations only after a successful run. All three implementations in PHP, Bash, and PowerShell follow the same specification.

pg-clusterbackup workflow: discover clusters, dump individually, rotate, and report 1. Discover Clusters & DBs 2. Dump .cus & globals.sql 3. Rotate After success only 4. Report Mail, JSON, Ping
Figure 1: Single run of pg-clusterbackup on a host with multiple PostgreSQL clusters.

Open Source on GitHub

Licensed under MIT, pg-clusterbackup discovers running clusters automatically, dumps in parallel, and sends run reports via email or webhook.

View on GitHub

Training: PostgreSQL Administration

Practice backup and recovery strategies, PITR, maintenance, and high availability hands-on with your own cluster.

View Training (German)

Why static backup scripts fall short

Traditional backup scripts assume a single database server and a hardcoded list of database names. As soon as multiple PostgreSQL versions run on the same host or new databases are created, parts of your data go unbacked. pg-clusterbackup instead queries the operating system on every run for all active clusters and their databases.

In our PostgreSQL Administration training (German) and consulting work, the same four pitfalls come up again and again:

  • Side-by-side major versions: Debian and Ubuntu hosts (via postgresql-common) as well as RHEL systems often run several PostgreSQL versions such as 14, 16, and 18 on different ports.
  • Dynamic databases: Developers or applications create new databases that are missing from static backup lists and silently skipped.
  • Missing global objects: A per-database pg_dump does not include roles, passwords, or tablespaces. Without pg_dumpall --globals-only, restoring onto a fresh server fails.
  • Monolithic all-in-one dumps: A single dump across all databases holds locks longer and makes it harder to restore just one database or table.
Comparison: static backup script vs. pg-clusterbackup A static script relies on manual lists, misses new databases, and often omits global roles. pg-clusterbackup discovers running clusters automatically, dumps each database individually, and stores globals separately. Static Script Manual database list New DBs get missed Global roles often missing With pg-clusterbackup Auto-discovers active clusters Per-DB dumps (.cus) + globals.sql Rotates only after success
Figure 2: Dynamic OS-level discovery instead of hand-maintained database lists.

What pg-clusterbackup does

On every run, the tool discovers all active PostgreSQL instances on the host and dumps each database in isolation. Old generations are pruned per cluster only after the current backup completes without errors, and you can verify every setting ahead of time using --dry-run.

  1. Automatic cluster discovery: On Debian and Ubuntu, the script reads pg_lsclusters. On RHEL, CentOS, Rocky, and AlmaLinux, it scans data directories via configurable glob patterns (/var/lib/pgsql/*/data); on Windows, it detects active PostgreSQL services or portable installations.
  2. Per-database custom dumps: Each database is backed up individually with pg_dump -Fc, allowing targeted restores of single databases or tables. Use --include and --exclude glob filters to select databases and -Z to set compression (for example zstd:3).
  3. Separate globals: Roles, permissions, and tablespaces are dumped per cluster via pg_dumpall -g into globals.sql.
  4. Parallel execution (-j and -C): -j runs multiple pg_dump jobs concurrently within a cluster, while -C backs up multiple clusters at once, cutting backup windows on multi-core hosts.
  5. Safety, pre-flight checks, and reporting: A lock file prevents overlapping runs, and min_free_space_gb checks free disk space before starting. On Linux, umask 077 protects dump files, and safe cross-filesystem moves transfer completed directories atomically. Each cluster gets a manifest.json with timings and file sizes; run status is sent by email and optionally via --ping-url to monitoring services such as Healthchecks.io or Uptime Kuma.

For keeping your databases healthy alongside regular backups, see PostgreSQL Maintenance — VACUUM, REINDEX, CLUSTER (German) and The Operating System Beneath PostgreSQL (German). To audit your server configuration as well, pair it with pg-harden.


Backup directory layout

pg-clusterbackup organizes backups by date, PostgreSQL major version, and cluster name. That layout lets you locate any database dump immediately and restore it directly with pg_restore.

/data/backup/postgresql/
├── 20261130/
│   ├── 16/
│   │   └── main/
│   │       ├── globals.sql
│   │       ├── manifest.json
│   │       ├── app_production.cus
│   │       └── analytics.cus
│   └── 18/
│       └── testing/
│           ├── globals.sql
│           ├── manifest.json
│           └── staging_db.cus
└── 20261129/
    └── ...

Restoring a single database

Thanks to the custom format (.cus), you can restore a single database with one command:

pg_restore -h /var/run/postgresql -p 5432 -d app_production /data/backup/postgresql/20261130/16/main/app_production.cus

Quick start: installation and setup

Download the single script file that fits your environment: PHP, Bash, or PowerShell. All three ports follow the exact same specification (SPEC.md) and are tested in version 1.8.1 (as of November 2026).

Quick start with pg-clusterbackup in three steps 1 Download script 2 Write INI (--ini-write) 3 Schedule cron job
Figure 3: Setup in three steps — from downloading the script to a daily cron job.

1. Download the script (v1.8.1)

PHP:

curl -O https://raw.githubusercontent.com/glueck-it/pg-clusterbackup/v1.8.1/php/pg_clusterbackup.php
chmod +x pg_clusterbackup.php

Bash:

curl -O https://raw.githubusercontent.com/glueck-it/pg-clusterbackup/v1.8.1/bash/pg_clusterbackup.sh
chmod +x pg_clusterbackup.sh

PowerShell (Windows / Linux):

Invoke-WebRequest https://raw.githubusercontent.com/glueck-it/pg-clusterbackup/v1.8.1/powershell/pg_clusterbackup.ps1 -OutFile pg_clusterbackup.ps1

2. Save your configuration once

Pass --ini-write (PowerShell: -IniWrite) to persist your settings in pg_clusterbackup.ini. Before the first real run, verify cluster discovery and target paths with --dry-run:

./pg_clusterbackup.php -D /data/backup/postgresql --email admin@example.com -n 14 -j 4 -C 2 --ini-write
./pg_clusterbackup.php --dry-run

From then on, invoke the script without flags, for example in a daily cron job at 02:30:

30 2 * * * root /opt/pg-clusterbackup/pg_clusterbackup.php

Frequently Asked Questions (FAQ)

Which PostgreSQL versions are supported?

pg-clusterbackup supports all modern PostgreSQL releases from PostgreSQL 10 through PostgreSQL 18. Because it invokes the host's installed pg_dump, pg_dumpall, and pg_lsclusters binaries, it works across versions without server-side changes.

How does it compare to pgBackRest or Barman?

pgBackRest and Barman are built for physical base backups and continuous Point-in-Time Recovery (PITR) using WAL archives. pg-clusterbackup complements them with portable, logical per-database dumps so you can restore individual databases or schemas quickly across PostgreSQL versions on any machine.

Does it run on Windows?

Yes. Both the PowerShell script (pg_clusterbackup.ps1) and the PHP script run natively on Windows. Active PostgreSQL instances are discovered through the Windows Service Manager via their -D argument or via configurable data directory globs (C:\Program Files\PostgreSQL\*\data).

Where can I find the source code and documentation?

The full project with changelog, examples, and specification is hosted on GitHub: https://github.com/glueck-it/pg-clusterbackup.

A release archive is also available on the PostgreSQL Extension Network: pg-clusterbackup on PGXN. pg-clusterbackup is a command-line tool, not a server extension; there is no CREATE EXTENSION.


Related Training & Consulting

If you want to strengthen your backup and recovery strategy, I support teams in the Cologne/Leverkusen area on-site as well as remotely or in-house across Germany — see Database Consulting in the Rhineland (German):