Backup Mechanism
Pigsty’s backup and restore operations ultimately execute pgBackRest commands. Using them safely requires both pgBackRest’s model and the mapping from Pigsty’s orchestration layers to native options.
Core pgBackRest Concepts
Stanza: the Cluster’s Backup Identity
A stanza names one PostgreSQL backup configuration and isolates that cluster inside a repository. Pigsty maps it directly from pg_cluster: the pg-meta stanza stores data under backup/pg-meta/ and archive/pg-meta/, so several clusters can share one repository.
The stanza records the source system identifier and major version and checks them before a backup. That identity check is why a cross-cluster clone needs stanza-upgrade afterward. Pigsty creates the stanza during cluster initialization; stanza-upgrade updates it after a major-version change or clone.
Repository: Where Backups Live
A repository stores backup files and WAL archives. repo1-type selects POSIX, S3, Azure, GCS, or SFTP; repo1-path, repo1-cipher-*, and repo1-retention-* define location, encryption, and retention. Pigsty renders these from pgbackrest_repo; see Backup Repository.
Backup Chains and Labels
| Type | Contents | Label suffix |
|---|---|---|
| Full | Complete database-cluster copy | F |
| Differential | Changes since the latest full | D |
| Incremental | Changes since the latest backup of any type | I |
Labels encode the chain. 20250715-013657F is a full backup; 20250715-013657F_20250715-013724D and ..._20250715-013730I depend on the full identified before the underscore. --set chooses the starting backup explicitly; otherwise pgBackRest selects the newest usable set before the target.
Retention: Keeping the Repository Bounded
repo1-retention-full and repo1-retention-full-type (count or time) decide when full chains expire. Dependent differential/incremental backups and WAL needed only by that chain expire with the full. Pigsty enables expire-auto, and pig pb expire --plan previews a manual run.
Time retention is a minimum window, not “keep only fulls newer than N days.” An old full expires only when another retained full has reached that age. A 14-day setting with weekly fulls therefore commonly retains three full chains and roughly 14–21 days of history.
WAL Archiving
PostgreSQL invokes archive-push when a WAL segment fills or archive_timeout elapses. During recovery, restore_command calls archive-get. Pigsty enables asynchronous archiving through /pg/spool so a temporary repository delay does not block the primary directly.
Timelines
Each promotion after recovery or failover creates a new timeline. Older timeline history remains in the repository, and --target-timeline chooses the recovery branch (latest by default). See the conceptual PITR mechanism.
What restore Actually Does
restore first reconstructs the data directory. Pigsty enables --delta, so pgBackRest validates existing files and rewrites only mismatches. It then writes recovery state (recovery.signal, restore_command, and recovery_target_*). Actual WAL replay happens after PostgreSQL starts.
Consequently, a successful restore command is only half of PITR. --target-action controls what happens when replay reaches the target: pause, promote, or shutdown.
Observe a Backup Chain
Run read-only info after full, differential, and incremental backups to inspect labels, size, WAL bounds, and references:
A representative sequence looks like:
Pigsty’s Wrapper Layers
| Layer | Interface | What it does |
|---|---|---|
| Cluster orchestration | pg_pitr + pgsql-pitr.yml | Pause HA, stop nodes, render configuration, restore/replay, inspect control data, clean etcd, and rebuild HA |
| Instance orchestration | pig pitr | Preflight, keep one target offline, restore, optionally start PostgreSQL, and leave Patroni stopped for inspection |
| Command primitive | pig pb, pb, pg-backup | Supply stanza/DBSU context and call the corresponding pgBackRest command |
| Engine | pgbackrest | Read /etc/pgbackrest/pgbackrest.conf and perform backup, archive, and restore operations |
Command Primitives
pb is a login-shell function that reads the first stanza from the local configuration and forwards arguments:
pg-backup adds a primary-role check for scheduled use:
pig pb adds stanza detection, DBSU privilege handling, primary checks for backup, and plan/confirmation guards for destructive primitives. See Admin Commands.
Parameter Mapping
pg_pitr field | pig pitr option | pgBackRest option | Meaning |
|---|---|---|---|
cluster | --stanza | --stanza | Source cluster/stanza |
type plus time/xid/lsn/name | corresponding target option | --type + --target | Recovery target |
default | --default | no --type/--target | Replay to archive end |
immediate | --immediate | --type=immediate | Stop at the first consistent point |
exclusive | --exclusive / -X | --target-exclusive | Stop before the target |
action | --target-action | --target-action | pause, promote, or shutdown |
timeline | --target-timeline / -T | --target-timeline | Target timeline |
set | --set / -b | --set | Starting backup set |
db_include / db_exclude | — | --db-include / --db-exclude | Select databases in a physical restore |
link_map | — | --link-map | Remap directory or tablespace links |
process | — | process-max | Parallel restore processes |
data | --data / -D | --pg1-path | Target data directory |
repo | repository number only in pig pitr | rendered repo1-* | Override repository definition in the playbook |
A selective restore is still physical. Excluded databases receive sparse zeroed files so PostgreSQL can complete recovery, but those databases are inaccessible and must be removed explicitly afterward; this is not a logical subset like pg_dump.
How Configuration Is Rendered
The entry selected by pgbackrest_method is rendered to /etc/pgbackrest/pgbackrest.conf: underscores become hyphens and keys receive the repo1- prefix.
pgsql-pitr.yml renders a separate temporary /pg/conf/pitr.conf; PostgreSQL recovery output goes to /pg/tmp/recovery.log.
Scheduled Backups
pg_crontab entries are installed for the postgres OS user on every cluster node. Because pg-backup checks the current role, only the primary backs up, and a promoted primary takes over future schedules.
See Backup Policy for frequency and retention design.
Deployment Details
The pg_backup subtask installs/configures pgBackRest, creates the stanza, and—when pgbackrest_init_backup is enabled—attempts an initial full backup. /etc/pgbackrest/initial.done is written only after that backup succeeds.
| Path | Purpose |
|---|---|
/usr/bin/pgbackrest | pgBackRest binary |
/etc/pgbackrest/pgbackrest.conf | Main stanza and repository configuration |
/pg/backup | Local repository path |
/pg/spool | Asynchronous archive spool |
/pg/log/pgbackrest/ | Backup, archive, and restore logs |
/pg/conf/pitr.conf | Temporary PITR configuration |
/pg/tmp/recovery.log | PostgreSQL recovery log |
pgbackrest_exporter listens on pgbackrest_exporter_port, 9854 by default, and exports backup metrics. Disable it with pgbackrest_exporter_enabled or customize it with pgbackrest_exporter_options.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)