Automated, centralized, and reliable backups for PostgreSQL clusters managed by Patroni.
This project showcases a production-grade architecture where a dedicated pgBackRest server performs remote pull backups — both full and incremental — from a high-availability PostgreSQL cluster managed by Patroni.
- ✅ Set up a highly available PostgreSQL cluster using Patroni.
- ✅ Use a centralized pgBackRest repository host for backups.
- ✅ Support remote pull backups (not push), including full and incremental.
- ✅ Store all WALs and backup sets in one place for easy disaster recovery.
- ✅ Enable replica rebuilds from pgBackRest backups instead of pg_basebackup.
- ✅ Automate backup scheduling and retention.
- PostgreSQL 14+
- Patroni for high availability
- pgBackRest for backup & restore
- etcd (as Patroni DCS)
- Systemd/Cron for backup scheduling
- (Optional) HAProxy / PgBouncer / Grafana
patroni-pgbackrest-ha-backup/
├── docker-compose.yml # For local cluster setup (optional)
├── patroni/ # Patroni config files
│ ├── install_pgbackrest.sh
│ ├── patroni.yml
│ ├── pgbackrest.conf
│ └── postgresql.conf
├── pgbackrest/
│ ├── pgbackrest.conf # Centralized backup config
│ └── install.sh # setup script
├── scripts/
│ ├── backup-full.sh
│ ├── backup-incr.sh
│ └── restore.sh
└── README.md
-
The pgBackRest server connects to Patroni nodes via SSH.
-
It pulls backups from the current leader (or a replica).
-
WALs are archived continuously using
archive_command
. -
Backups are automated using cron/systemd timers:
02:00 AM
: full backupEvery hour
: incremental backups
- Replicas can be rebuilt using
pgbackrest restore
during Patroni bootstrap. - Supports Point-In-Time Recovery (PITR) using WALs.
- Easy failover and rejoin after node failure.
[global]
repo1-path=/var/lib/pgbackrest
repo1-retention-full=2
repo1-retention-diff=7
compress-type=zst
start-fast=y
process-max=2
log-level-console=info
[patronicluster]
pg1-host=patroni-node1
pg1-path=/var/lib/postgresql/data
pg2-host=patroni-node2
pg2-path=/var/lib/postgresql/data
pg3-host=patroni-node3
pg3-path=/var/lib/postgresql/data
-
Clone the repository
git clone https://github.com/your-username/patroni-pgbackrest-ha-backup.git cd patroni-pgbackrest-ha-backup
-
Set up SSH access
-
Copy SSH key from backup server to all Patroni nodes:
ssh-copy-id postgres@patroni-node1
-
-
Install dependencies
- Install pgBackRest (same version) on all nodes and backup server.
- Configure Patroni nodes to archive WALs using
archive_command
.
-
Create stanza
pgbackrest --stanza=patronicluster stanza-create
-
Run backups
pgbackrest --stanza=patronicluster --type=full backup pgbackrest --stanza=patronicluster --type=incr backup
- ✔️ Restore to any point in time using WALs
- ✔️ Rebuild a failed replica using
pgbackrest restore
- ✔️ Validate backup integrity with
check
command - ✔️ Automated failover with Patroni
- Using a pull backup model centralizes control and scales better.
- Properly synchronizing versions of pgBackRest is crucial.
- Automating replica rebuilds helps reduce downtime and human error.
- Separation of storage (WALs, backups) from database servers simplifies DR.
- Integrate with Prometheus + Grafana to monitor backup success/failure.
- Store backups in object storage (e.g. S3, MinIO).
- Use Ansible to automate deployment.
- Enable TLS encryption for secure SSH/WAL transmission.
This project is licensed under the MIT License.
PRs and ideas welcome! Please open issues for enhancements, bugs, or improvements.