PostgreSQL 数据库备份工具 pgBackRest 使用
最近项目中使用到了PostgreSQL 数据库,PostgreSQL 数据库官方备份工具pg_basebackup 使用起来比较繁琐,研究了一下开源的备份工具 pgBackRest。
pgBackRest是专为 PostgreSQL 数据库设计的高性能备份与恢复工具,支持全量、增量和差异备份,具备并行处理、压缩、加密及时间点恢复(PITR)等核心功能,官方网站为https://pgbackrest.org/。
网上关于 pgBackRest文档很多,但是大多写的太过繁琐、复杂,整理了一下安装、配置步骤:
1、官方下载地址为https://github.com/pgbackrest,只提供源码需要手动编译安装,PostgreSQL 官网提供二进制安装包,这里以redhat enterprise linux 8.10 rpm 包为例,下载地址如下https://download.postgresql.org/pub/repos/yum/common/redhat/rhel-8.10-x86_64/ ,

其他Linux 发行版本二进制安装包可在如下地址https://download.postgresql.org/pub/repos/ 自行查找。

2、pgBakRest rpm 安装包依赖于libssh2 包,下载地址为https://mirrors.tuna.tsinghua.edu.cn/epel/8/Everything/x86_64/Packages

3、PostgreSql 数据库服务器操作系统配置好本地安装镜像yum 源,通过yum 依次安装libssh2、pgbakrest 包,执行pgbakrest help 可以查看使用帮助:
pgbackrest help
pgBackRest 2.58.0 - General help
Usage:
pgbackrest [options] [command]
Commands:
annotate add or modify backup annotation
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
repo-get get a file from a repository
repo-ls list files in a repository
restore restore a database cluster
server pgBackRest server
server-ping ping pgBackRest server
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
verify verify contents of a repository
version get version
Use 'pgbackrest help [command]' for more information.
4、安装完成后,配置pgbakrest,配置文件位于/etc/pgbackrest.conf,配置文件内容如下:
cat /etc/pgbackrest.conf
[global] --全局配置
repo1-cipher-type=none --不加密备份文件
repo1-path=/backup/pgbackrest --备份文件存放路径
repo1-retention-full-type=time --备份文件保留策略按照时间
repo1-retention-full=7 --保留最近7天的完整备份
repo1-retention-archive-type=full --归档删除策略基于完整备份
repo1-retention-archive=7 --保留最近7天完整备份所需要的归档日志
log-level-console=info --输出详细日志
[pgdb] --数据库实例配置
pg1-path=/data/pgsql/pgdata --数据库数据存放路径
pg1-port=5432 --数据端口
pg1-user=postgres --数据库进程使用的操作系统用户
pg1-socket-path=/tmp --数据库socket 存放路径
[global:archive-push]
compress-level=3 --压缩归档日志
5、PostgreSql 数据库需配合启用归档模式,重启数据库服务
archive_mode=on
archive_command='pgbackrest --stanza=pgdb archive-push %p'

6、执行pgbakrest check,确认配置无误:
pgbackrest check
2026-04-19 11:55:59.522 P00 INFO: check command begin 2.58.0: --exec-id=3856469-3feabc7b --log-level-console=info --repo1-path=/backup/pgbackrest
2026-04-19 11:55:59.522 P00 INFO: check stanza 'pgdb'
2026-04-19 11:55:59.527 P00 INFO: check repo1 configuration (primary)
2026-04-19 11:55:59.538 P00 INFO: check repo1 archive for WAL (primary)
2026-04-19 11:56:00.147 P00 INFO: WAL segment 000000070000000F00000044 successfully archived to '/backup/pgbackrest/archive/pgdb/14-1/000000070000000F/000000070000000F00000044-b2b6d869b78c1e2e7ad8be9c959f1588c60c50ae.gz' on repo1
2026-04-19 11:56:00.147 P00 INFO: check command end: completed successfully (626ms)
7、执行pgbackrest --stanza=pgdb stanza-create创建备份仓库
8、配置cron 定时备份任务:
crontab -l
00 11 * * * /usr/bin/pgbackrest --stanza=pgdb backup --type=full
9、恢复数据库,恢复之前需先停止PostgreSql 数据库服务,数据目录需为空:
查看备份信息,执行pgbakrest info
pgbackrest info
stanza: pgdb
status: ok
cipher: none
db (current)
wal archive min/max (14): 000000070000000D0000006E/000000070000001600000001
full backup: 20260417-110001F
timestamp start/stop: 2026-04-17 23:00:01+08 / 2026-04-17 23:04:10+08
wal start/stop: 000000070000000D0000006E / 000000070000000D0000006E
database size: 3.1GB, database backup size: 3.1GB
repo1: backup set size: 564.6MB, backup size: 564.6MB
full backup: 20260418-110002F
timestamp start/stop: 2026-04-18 23:00:02+08 / 2026-04-18 23:04:33+08
wal start/stop: 000000070000000E00000055 / 000000070000000E00000055
database size: 3.2GB, database backup size: 3.2GB
repo1: backup set size: 574.3MB, backup size: 574.3MB
full backup: 20260419-110001F
timestamp start/stop: 2026-04-19 23:00:01+08 / 2026-04-19 23:04:01+08
wal start/stop: 000000070000000F0000003B / 000000070000000F0000003B
database size: 3.2GB, database backup size: 3.2GB
repo1: backup set size: 584MB, backup size: 584MB
恢复指定的备份
/usr/bin/pgbackrest --stanza=pgdb --set=20260419-110001F restore
本文来自博客园,作者:踏雪无痕2017,转载请注明原文链接:https://www.cnblogs.com/oradba/p/19890561

浙公网安备 33010602011771号