SoFunction
Updated on 2025-04-07

PostgreSQL backup tool pgBackRest usage details

Preface

pgBackRest is an open source backup and restore tool designed to provide reliable and easy-to-use backups for backup and restore.

characteristic

Parallel backup and restore

Compression during backup operations is usually the bottleneck. pgBackRest solves the bottleneck problem of compression during backup through parallel processing.

Local remote operation

The custom protocol allows pgBackRest to minimize configurations to perform backups, restores, and archives locally or remotely via SSH. In addition, the program also provides a PostgreSQL query interface through the protocol layer, so that PostgreSQL must be accessed remotely, thus ensuring its security performance.

Full, incremental and differential backups

Supports full, incremental and differential backups. pgBackRest is not affected by asynchronous time, so differential and incremental backups are completely secure.

Backup retention policies and archive expirations

Support retention policy settings enable the creation of overlays for full and differential backups at any time.

Backup Integrity

The checksum of each file in the backup can be calculated and rechecked during restore. After the backup has finished copying the file, it will wait until the backup required for each WAL segment remains consistent and then stored in the backup repository.

Block Checksum Breakpoint Backup Stream Compression and Checksum Incremental Restore Parallel Asynchronous WAL Push and Get Tablespace Remapping and Link Support S3 Storage Support and Azure Compatible Object Storage Support Encryption

use

Install and decompression

[postgres@pgserver12 tools]$ tar -zxf pgbackrest-release-2.
[postgres@pgserver12 tools]$ ls

Create the necessary directory

ostgres@sungsasong ~]$ sudo mkdir -p -m 770 /var/log/pgbackrest
[postgres@sungsasong ~]$ sudo chown  /var/log/pgbackrest/
[postgres@sungsasong ~]$ sudo mkdir -p /etc/pgbackrest
[postgres@sungsasong ~]$ sudo mkdir -p /etc/pgbackrest/
[postgres@sungsasong ~]$ sudo touch /etc/pgbackrest/
[postgres@sungsasong ~]$ sudo chmod 640 /etc/pgbackrest/
[postgres@sungsasong ~]$ sudo chown  -R /etc/pgbackrest/
[postgres@sungsasong ~]$ sudo mkdir /usr/bin/pgbackrest
[postgres@sungsasong ~]$ sudo chown  /usr/bin/pgbackrest/
[postgres@sungsasong ~]$ sudo chmod 755 /usr/bin/pgbackrest/

Compile and install

[postgres@sungsasong src]$ ./configure --prefix=/usr/bin/pgbackrest/
[postgres@sungsasong src]$ make -j24
[postgres@sungsasong src]$ make install -j24
install -d /usr/bin/pgbackrest/bin
install -m 755 pgbackrest /usr/bin/pgbackrest/bin

Command Test

[postgres@sungsasong src]$ /usr/bin/pgbackrest/bin/pgbackrest
pgBackRest 2.31 - General help
 
Usage:
 pgbackrest [options] [command]
 
Commands:
 archive-get Get a WAL segment from the archive.
 archive-push Push a WAL segment to the archive.
 backup Backup a database cluster.
 check Check the configuration.
 expire Expire backups that exceed retention.
 help Get help.
 info Retrieve information about backups.
 restore Restore a database cluster.
 stanza-create Create the required stanza data.
 stanza-delete Delete a stanza.
 stanza-upgrade Upgrade a stanza.
 start Allow pgBackRest processes to run.
 stop Stop pgBackRest processes from running.
 version Get version.
 
Use 'pgbackrest help [command]' for more information.

Configure database listening and access and logging (optional)

[postgres@sungsasong pgbackrest-release-2.31]$ egrep "10.10" $PGDATA/pg_hba.conf
host all all 10.10.20.0/24 trust
[postgres@sungsasong pgbackrest-release-2.31]$ egrep -v "^#" $PGDATA/
logging_collector = 'on'
listen_addresses = '*'

Configure PostgreSQL Database Datastore Directory

[postgres@sungsasong src]$ cat >>/etc/pgbackrest/ <<EOF
> [demo]
> pgl-path=/data/pg10/pgdata
> EOF

Configure environment variables

[postgres@sungsasong ~]$ echo "export PATH=/usr/bin/pgbackrest/bin:\$PATH" >> .bashrc

Create backup and archive repositories

[postgres@sungsasong ~]$ sudo mkdir -p /pgbackrest/repos
[postgres@sungsasong ~]$ sudo chmod 750 /pgbackrest/ -R
[postgres@sungsasong ~]$ sudo chown  /pgbackrest/ -R

Load the repository path in pgBackRest configuration file

[postgres@sungsasong ~]$ cat /etc/pgbackrest/
[demo]
pgl-path=/data/pg10/pgdata
 
[global]
repol-path=/pgbackrest/repos

Configure database archives

[postgres@sungsasong ~]$ egrep -v "^#" $PGDATA/
logging_collector = 'on'
archive_mode = 'on'
archive_command = 'pgbackrest --stanza=demo archive-push %p'
listen_addresses = '*'
log_filename = ''
log_line_prefix = ''
max_wal_senders = '3'
wal_level = 'replica'

Restart the database

[postgres@sungsasong ~]$ pg_ctl restart -D $PGDATA -l /tmp/logfile
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started

Optional configuration archive compression

[postgres@sungsasong ~]$ cat /etc/pgbackrest/
[demo]
pgl-path=/data/pg10/pgdata
 
[global]
repol-path=/pgbackrest/repos
 
[global:archive_push]
compress-level=3

Configure archives based on retention policies

[postgres@sungsasong ~]$ cat /etc/pgbackrest/
[demo]
pg1-path=/data/pg10/pgdata
pg1-host-config-path=/data/pg10/pgbackrest
pg1-host-port=22
pg1-host-user=postgres
pg1-host=sungsasong
pg1-port=10001
pg1-user=postgres
 
[global]
repo1-path=/pgbackrest/repos
repo1-retention-full=2
 
[global:archive_push]
compress-level=3

Create storage space and check configuration

[postgres@sungsasong ~]$ pgbackrest --stanza=demo --log-level-console=info stanza-create
2021-01-08 20:42:58.887 P00 INFO: stanza-create command begin 2.31: --exec-id=9283-e9744c3e --log-level-console=info --pg1-host=sungsasong --pg1-host-config-path=/data/pg10/pgbackrest --pg1-host-port=22 --pg1-host-user=postgres --pg1-path=/data/pg10/pgdata --pg1-port=10001 --pg1-user=postgres --repo1-path=/pgbackrest/repos --stanza=demo
2021-01-08 20:42:59.835 P00 INFO: stanza-create command end: completed successfully (948ms)

Check configuration

[postgres@sungsasong ~]$ pgbackrest --stanza=demo --log-level-console=info check
2021-01-08 21:41:29.851 P00 INFO: check command begin 2.31: --exec-id=21648-1862ac0d --log-level-console=info --pg1-path=/data/pg10/pgdata --pg1-port=10001 --pg1-user=postgres --repo1-path=/pgbackrest/repos --stanza=demo
2021-01-08 21:41:32.826 P00 INFO: WAL segment 000000010000000000000006 successfully archived to '/pgbackrest/repos/archive/demo/10-1/0000000100000000/'
2021-01-08 21:41:32.826 P00 INFO: check command end: completed successfully (2975ms)

Perform a backup

The default is incremental backup. Incremental backup will request a basic complete backup. If there is no basic complete backup, the incremental backup will be changed to full backup.

pgbackrest --stanza=demo --log-level-console=info backup
2021-01-08 21:43:55.014 P00 INFO: backup command begin 2.31: --exec-id=21661-13c54272 --log-level-console=info --pg1-path=/data/pg10/pgdata --pg1-port=10001 --pg1-user=postgres --repo1-path=/pgbackrest/repos --repo1-retention-full=2 --stanza=demo
WARN: no prior backup exists, incr backup has been changed to full
...
Many outputs are omitted here
...
2021-01-08 21:44:00.951 P00 INFO: full backup size = 23.3MB
2021-01-08 21:44:00.951 P00 INFO: execute non-exclusive pg_stop_backup() and wait for all WAL segments to archive
2021-01-08 21:44:01.153 P00 INFO: backup stop archive = 000000010000000000000008, lsn = 0/8000130
2021-01-08 21:44:01.154 P00 INFO: check archive for segment(s) 000000010000000000000008:000000010000000000000008
2021-01-08 21:44:01.273 P00 INFO: new backup label = 20210108-214355F
2021-01-08 21:44:01.301 P00 INFO: backup command end: completed successfully (6288ms)
2021-01-08 21:44:01.301 P00 INFO: expire command begin 2.31: --exec-id=21661-13c54272 --log-level-console=info --repo1-path=/pgbackrest/repos --repo1-retention-full=2 --stanza=demo
2021-01-08 21:44:01.304 P00 INFO: expire command end: completed successfully (3ms)

Can you define the type of backup as incremental or differential backup

Specify using the --type parameter

[postgres@sungsasong ~]$ pgbackrest --stanza=demo --log-level-console=info --type=diff backup
2021-01-08 21:47:00.964 P00 INFO: backup command begin 2.31: --exec-id=21671-d3f8f8bf --log-level-console=info --pg1-path=/data/pg10/pgdata --pg1-port=10001 --pg1-user=postgres --repo1-path=/pgbackrest/repos --repo1-retention-full=2 --stanza=demo --type=diff
2021-01-08 21:47:01.770 P00 INFO: last backup label = 20210108-214355F, version = 2.31
2021-01-08 21:47:01.770 P00 INFO: execute non-exclusive pg_start_backup(): backup begins after the next regular checkpoint completes
2021-01-08 21:47:02.309 P00 INFO: backup start archive = 00000001000000000000000A, lsn = 0/A000028
2021-01-08 21:47:03.617 P01 INFO: backup file /data/pg10/pgdata/global/pg_control (8KB, 99%) checksum 5f020e7df484269ea245041be3228673560184ef
2021-01-08 21:47:03.721 P01 INFO: backup file /data/pg10/pgdata/pg_logical/replorigin_checkpoint (8B, 100%) checksum 347fc8f2df71bd4436e38bd1516ccd7ea0d46532
2021-01-08 21:47:03.722 P00 INFO: diff backup size = 8KB
2021-01-08 21:47:03.722 P00 INFO: execute non-exclusive pg_stop_backup() and wait for all WAL segments to archive
2021-01-08 21:47:03.942 P00 INFO: backup stop archive = 00000001000000000000000A, lsn = 0/A0000F8
2021-01-08 21:47:03.943 P00 INFO: check archive for segment(s) 00000001000000000000000A:00000001000000000000000A
2021-01-08 21:47:04.062 P00 INFO: new backup label = 20210108-214355F_20210108-214701D
2021-01-08 21:47:04.092 P00 INFO: backup command end: completed successfully (3129ms)
2021-01-08 21:47:04.092 P00 INFO: expire command begin 2.31: --exec-id=21671-d3f8f8bf --log-level-console=info --repo1-path=/pgbackrest/repos --repo1-retention-full=2 --stanza=demo
2021-01-08 21:47:04.095 P00 INFO: expire command end: completed successfully (3ms)

View backup information

[postgres@sungsasong ~]$ pgbackrest info
stanza: demo
 status: ok
 cipher: none
 
 db (current)
 wal archive min/max (10-1): 000000010000000000000001/00000001000000000000000A
 
 full backup: 20210108-214355F
 timestamp start/stop: 2021-01-08 21:43:55 / 2021-01-08 21:44:01
 wal start/stop: 000000010000000000000008 / 000000010000000000000008
 database size: 23.3MB, backup size: 23.3MB
 repository size: 2.7MB, repository backup size: 2.7MB
 
 diff backup: 20210108-214355F_20210108-214701D
 timestamp start/stop: 2021-01-08 21:47:01 / 2021-01-08 21:47:03
 wal start/stop: 00000001000000000000000A / 00000001000000000000000A
 database size: 23.3MB, backup size: 8.2KB
 repository size: 2.7MB, repository backup size: 425B
 backup reference list: 20210108-214355F

Restore a backup

Simulate database corruption

[postgres@sungsasong ~]$ cd $PGDATA/
[postgres@sungsasong pgdata]$ rm -rf *
#100Warning:Never execute with the production library

Perform a restore

[postgres@sungsasong pgdata]$ pgbackrest --stanza=demo restore

Restart the database

[postgres@sungsasong pgdata]$ pg_ctl start -D $PGDATA -l /tmp/logfile
waiting for server to start.... done
server started
[postgres@sungsasong pgdata]$ psql
psql (10.13)
Type "help" for help.

The above is the introduction and use of pgbackrest. The latest version currently used can already support PostgreSQL13 version, but the document labeling supports the latest PostgreSQL11 version. Interested students can go down and try it.

I hope I can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.