作者简介
Granthana Biswas cybertec公司工程师。
译者简介
王志斌,从事数据库产品相关工作,主要致力于postgresql数据库高可用解决方案及云端产品化工作。
校对者简介
李伟,任职于海能达通信股份有限公司,数据库开发高级工程师,致力于postgresql数据库在专网通信领域、公共安全领域的应用与推广。
pgBackRest是PostgreSQL的一个开源备份工具,它提供了简单的配置和可靠的备份。因此,如果您想保护您的数据库并轻松创建备份,pgBackRest是一个很好的解决方案。在本博客中,我们将介绍使用pgbackback,通过基本操作步骤,进行PostgreSQL的完全备份和差异备份。
pgBackRest的一些关键特性:
1. 并行备份与还原
2. 本地和远端操作
3. 全量、增量、差异备份
4. 备份转换和归档过期
5. 备份完整性
6. 页面校验和
7. 恢复备份
8. 流式压缩和校验和
想要阅读关于更多细节,请访问pgbackrest.org。(https://pgbackrest.org/)
现在我们将要从源码构建pgBackRest并安装到正在运行中的测试主机上。

从Debian Ubuntu包中进行安装:
sudo apt-get install pgbackrest
对于手动安装,请在构建的主机上下载源代码。请避免在生产服务器上构建源代码,因为构建所需的工具不应安装在生产计算机上:
下载pgBackRest(V2.14):
sudo wget -q -O - \ https://github.com/pgbackrest/pgbackrest/archive/release/2.14.tar.gz | \sudo tar zx -C root
安装和检查64系统下的依赖:
sudo apt-get install build-essential libssl-dev \libxml2-dev libperl-dev zlib1g-devperl -V | grep USE_64_BIT_INT
构建pgbackrest包:
(cd root/pgbackrest-release-2.14/src && ./configure)make -s -C root/pgbackrest-release-2.14/src
从构建主机上拷贝到数据库主机:
sudo scp BUILD_HOST:/root/pgbackrest-release-2.14/src/pgbackrest usr/bin/sudo chmod 755 usr/bin/pgbackrest
安装Perl包:
sudo apt-get install libdbd-pg-perl
在测试数据库主机上初始化pgBackRest配置文件和路径:
sudo mkdir -p -m 770 var/log/pgbackrestsudo chown postgres:postgres var/log/pgbackrestsudo mkdir -p etc/pgbackrestsudo mkdir -p etc/pgbackrest/conf.dsudo touch etc/pgbackrest/pgbackrest.confsudo chmod 640 etc/pgbackrest/pgbackrest.confsudo chown postgres:postgres etc/pgbackrest/pgbackrest.conf
创建pgBackRest仓库
sudo mkdir -p /var/lib/pgbackrestsudo chmod 750 /var/lib/pgbackrestsudo chown postgres:postgres /var/lib/pgbackrest
在pgbackrest 配置文件中,设置仓库路径和PostgreSQL数据目录:
cat /etc/pgbackrest/pgbackrest.conf[demo]pg1-path=/data/postgres/pgdata/data1[global]repo1-path=/var/lib/pgbackrest
配置PostgreSQL集群归档:
在postgresql.conf文件中修改如下参数:
archive_command = 'pgbackrest --stanza=demo archive-push %p'archive_mode = onlisten_addresses = '*'log_line_prefix = ''max_wal_senders = 3wal_level = replica
使用postgres创建节:
必须在存储库所在的主机上运行 stanza-create 命令才能初始化节。建议在创建节之后运行check命令,以确保正确配置了归档和备份。
$ pgbackrest --stanza=demo --log-level-console=info stanza-create2019-07-03 12:26:40.060 P00 INFO: stanza-create command begin 2.14: --log-level-console=info --pg1-path=/data/postgres/pgdata/data1 --repo1-path=/var/lib/pgbackrest --stanza=demo2019-07-03 12:26:40.494 P00 INFO: stanza-create command end: completed successfully (435ms)$ pgbackrest --stanza=demo --log-level-console=info check2019-07-03 12:27:11.996 P00 INFO: check command begin 2.14: --log-level-console=info --pg1-path=/data/postgres/pgdata/data1 --repo1-path=/var/lib/pgbackrest --stanza=demo2019-07-03 12:27:13.386 P00 INFO: WAL segment 000000010000000000000003 successfully stored in the archive at '/var/lib/pgbackrest/archive/demo/10-1/0000000100000000/000000010000000000000003-b346d07d4b31e54e31d9204204816cde3cfcca3a.gz'2019-07-03 12:27:13.387 P00 INFO: check command end: completed successfully (1392ms)
使用info命令获取备份信息
由于我们尚未进行任何备份,因此将得到以下结果:
$ pgbackrest infostanza: demostatus: error (no valid backups)cipher: nonedb (current)wal archive min/max (10-1): 000000010000000000000001/000000010000000000000003
备份:
让我们先创建第一个备份,默认情况下,即使我们将类型指定为差异,也将是full:
$ pgbackrest --stanza=demo --log-level-console=info backup2019-07-03 12:37:38.366 P00 INFO: backup command begin 2.14: --log-level-console=info --pg1-path=/data/postgres/pgdata/data1 --repo1-path=/var/lib/pgbackrest --repo1-retention-full=2 --stanza=demoWARN: no prior backup exists, incr backup has been changed to full2019-07-03 12:37:39.200 P00 INFO: execute non-exclusive pg_start_backup() with label "pgBackRest backup started at 2019-07-03 12:37:38": backup begins after the next regular checkpoint completes2019-07-03 12:37:39.500 P00 INFO: backup start archive = 000000010000000000000005, lsn = 0/50000282019-07-03 12:37:45.212 P00 INFO: full backup size = 22.5MB2019-07-03 12:37:45.212 P00 INFO: execute non-exclusive pg_stop_backup() and wait for all WAL segments to archive2019-07-03 12:37:45.313 P00 INFO: backup stop archive = 000000010000000000000005, lsn = 0/50001302019-07-03 12:37:45.558 P00 INFO: new backup label = 20190703-123738F2019-07-03 12:37:45.586 P00 INFO: backup command end: completed successfully (7221ms)2019-07-03 12:37:45.586 P00 INFO: expire command begin2019-07-03 12:37:45.594 P00 INFO: full backup total < 2 - using oldest full backup for 10-1 archive retention2019-07-03 12:37:45.596 P00 INFO: expire command end: completed successfully (10ms)
现在当再次运行info命令的时候:
$ pgbackrest infostanza: demostatus: okcipher: nonedb (current)wal archive min/max (10-1): 000000010000000000000005/000000010000000000000005full backup: 20190703-123738Ftimestamp start/stop: 2019-07-03 12:37:38 / 2019-07-03 12:37:45wal start/stop: 000000010000000000000005 / 000000010000000000000005database size: 22.6MB, backup size: 22.6MBrepository size: 2.7MB, repository backup size: 2.7MB
在集群上执行差异备份
$ pgbackrest --stanza=demo --log-level-console=info --type=diff backup2019-07-03 12:40:05.749 P00 INFO: backup command begin 2.14: --log-level-console=info --pg1-path=/data/postgres/pgdata/data1 --repo1-path=/var/lib/pgbackrest --repo1-retention-full=2 --stanza=demo --type=diff2019-07-03 12:40:05.951 P00 INFO: last backup label = 20190703-123738F, version = 2.142019-07-03 12:40:06.657 P00 INFO: execute non-exclusive pg_start_backup() with label "pgBackRest backup started at 2019-07-03 12:40:05": backup begins after the next regular checkpoint completes2019-07-03 12:40:06.958 P00 INFO: backup start archive = 000000010000000000000007, lsn = 0/70000282019-07-03 12:40:08.414 P01 INFO: backup file /data/postgres/pgdata/data1/global/pg_control (8KB, 99%) checksum c8b3635ef4701b19bff56fcd5ca33d41eaf3ce5b2019-07-03 12:40:08.421 P01 INFO: backup file /data/postgres/pgdata/data1/pg_logical/replorigin_checkpoint (8B, 100%) checksum 347fc8f2df71bd4436e38bd1516ccd7ea0d465322019-07-03 12:40:08.439 P00 INFO: diff backup size = 8KB2019-07-03 12:40:08.439 P00 INFO: execute non-exclusive pg_stop_backup() and wait for all WAL segments to archive2019-07-03 12:40:08.540 P00 INFO: backup stop archive = 000000010000000000000007, lsn = 0/70000F82019-07-03 12:40:08.843 P00 INFO: new backup label = 20190703-123738F_20190703-124005D2019-07-03 12:40:08.938 P00 INFO: backup command end: completed successfully (3189ms)2019-07-03 12:40:08.938 P00 INFO: expire command begin2019-07-03 12:40:08.949 P00 INFO: full backup total < 2 - using oldest full backup for 10-1 archive retention2019-07-03 12:40:08.951 P00 INFO: expire command end: completed successfully (13ms)
使用info命令,查看变化:
$ pgbackrest infostanza: demostatus: okcipher: nonedb (current)wal archive min/max (10-1): 000000010000000000000005/000000010000000000000007full backup: 20190703-123738Ftimestamp start/stop: 2019-07-03 12:37:38 / 2019-07-03 12:37:45wal start/stop: 000000010000000000000005 / 000000010000000000000005database size: 22.6MB, backup size: 22.6MBrepository size: 2.7MB, repository backup size: 2.7MBdiff backup: 20190703-123738F_20190703-124005Dtimestamp start/stop: 2019-07-03 12:40:05 / 2019-07-03 12:40:08wal start/stop: 000000010000000000000007 / 000000010000000000000007database size: 22.6MB, backup size: 8.2KBrepository size: 2.7MB, repository backup size: 468Bbackup reference list: 20190703-123738F
请点击文章底部“阅读原文”查看原文内容。
PostgreSQL中文社区欢迎广大技术人员投稿
投稿邮箱:press@postgres.cn




