Bootstrap

PG备份管理工具pg_probackup

一、pg_probackup概述

pg_probackup 是一款免费的postgres数据库集群备份工具,与其他备份工具相比,它主要有如下一些优势:

  • 提供增量备份,增量备份一定程度上可以节省磁盘空间的使用并且减少备份时间消耗
  • 可通过全量备份+增量备份进行增量恢复
  • 无需通过实际的数据恢复操作验证备份文件是否有效
  • Verification: on-demand verification of Postgres Pro instance with the checkdb command.
  • 可以通过设置恢复时间以及备份最大文件数来进行备份文件以及WAL归档日志的保留策略
  • 对于backup、restore、merge、delete、validate、checkdb操作都可开启并发线程执行
  • 提供压缩备份以节省磁盘空间
  • 可对远程实例进行备份恢复
  • 可从standby实例进行备份
  • 对于PGDATA外的目录数据(如:脚本、日志转储、sql dump 文件等),可使用参数额外指定进行备份
  • 可查看已备份数据备份以及归档的列表以及相关详细信息
  • 支持部分还原(还原部分数据库)

pg_probackup 下的几种备份方式:

  • 全量备份 : 全量备份会将数据库集下所有的数据文件进行备份

  • 增量备份 : 增量备份仅会备份上一次全量备份之后发生变更的数据,相对于全量备份其空间占用有了极大的缩减

    DELTA模式 : 在该模式下, pg_probackup 会扫描所有的数据目录文件,然后将上一次备份后发生改变的数据页进行拷贝备份。这种模式下增量备份的IO消耗基本等同于全量备份。

    PAGE模式 : 在该模式下, pg_probackup仅会扫描备份上一次备份结束时刻之后的所有WAL归档日志。这种模式下的增量备份必须保证wal日志有设置合理的归档(wal_level > minimal 、archive_mode = on/always、archive_command 使用 pg_probackup进行archive-push 归档)。

    PTRACK模式 : 在该模式下,pg_probackup 会实时跟踪源备份实例端数据页的变化,对于距上一次备份后发生更新的数据页,将其记录在 bitmap 中,以此来加快增量备份的时间。该模式下不需要关注WAL日志归档的设置,增量备份时间相对于DELTA更快,但是由于需要实时跟踪发生变化的数据页,所以对源端数据库服务器是有一定的资源消耗的。

pg_probackup 工具的一些局限性:

  • 仅支持Postgres Pro 9.5以上的版本
  • Windows系统不支持远程备份恢复
  • 在Unix系统在,如果数据库版本小于等于 Postgres Pro 10 ,只能通过与OS同账号的超级用户postgres进行备份。
  • 对于PostgreSQL 9.5版本数据库,进行备份的数据库账号必须具体superuser的角色,否则无法备份 pg_create_restore_point(text) 、 pg_switch_xlog()
  • 备份工具与数据库 block_size 、 wal_block_size 必须一致,否则无法备份(block_size、wal_block_size在安装包源码编译时设置)

二、安装部署

2.1 源码安装

1、下载安装包

# wget -c https://github.com/postgrespro/pg_probackup/archive/2.4.2.tar.gz
# tar xf 2.4.2.tar.gz

# cd pg_probackup-2.4.2/
# ll
总用量 176
drwxrwxr-x 2 root root   4096 7月   1 08:07 doc
-rw-rw-r-- 1 root root 128060 7月   1 08:07 Documentation.md
-rw-rw-r-- 1 root root   4976 7月   1 08:07 gen_probackup_project.pl
-rw-rw-r-- 1 root root   1200 7月   1 08:07 LICENSE
-rw-rw-r-- 1 root root   3962 7月   1 08:07 Makefile
-rw-rw-r-- 1 root root  13345 7月   1 08:07 README.md
drwxrwxr-x 3 root root   4096 7月   1 08:07 src
drwxrwxr-x 4 root root   4096 7月   1 08:07 tests
drwxrwxr-x 2 root root   4096 7月   1 08:07 travis

2、编译安装

# PG_CONFIG是我们pg_config程序所在路径,top_srcdir为postgres源码所在路径
# make USE_PGXS=1 PG_CONFIG=/usr/local/pgsql/bin/pg_config  top_srcdir=/usr/local/postgresql-12.2

# ll
总用量 612
drwxrwxr-x 2 root root   4096 7月   1 08:07 doc
-rw-rw-r-- 1 root root 128060 7月   1 08:07 Documentation.md
-rw-rw-r-- 1 root root   4976 7月   1 08:07 gen_probackup_project.pl
-rw-rw-r-- 1 root root   1200 7月   1 08:07 LICENSE
-rw-rw-r-- 1 root root   3962 7月   1 08:07 Makefile
-rwxr-xr-x 1 root root 445832 9月  22 21:55 pg_probackup            //编译后
-rw-rw-r-- 1 root root  13345 7月   1 08:07 README.md
drwxrwxr-x 3 root root   4096 9月  22 21:55 src
drwxrwxr-x 4 root root   4096 7月   1 08:07 tests
drwxrwxr-x 2 root root   4096 7月   1 08:07 travis

3、版本检查

# 查看版本
# ./pg_probackup --version
pg_probackup 2.4.2 (PostgreSQL 12.2)

2.2 rpm包安装部署

三、常用命令

  • 初始化备份目录
$ pg_probackup init -B ${backup_dir}
  • 添加实例
## 本地实例
pg_probackup  add-instance -B ${backup_dir} -D ${PGDATA} --instance ${instance_name}


## 添加远程实例
pg_probackup  add-instance -B ${backup_dir} -D ${PGDATA} --instance ${instance_name} --remote-prot=ssh --remote-host=${remote_ip} --remote-port=${remote_ssh_port} --remote-user=${remote_ssh_user} --remote-path=${pg_probackup_dir}
  • 备份
## 本地实例全量备份
pg_probackup backup -B ${backup_dir} --instance ${instance_name} -b full

## 远程实例全量备份
pg_probackup backup -B ${backup_dir} --instance ${instance_name} --remote-user=${remote_ssh_user} --remote-host=${remote_ip} --remote-port=${remote_ssh_port} -b full

## 增量备份

pg_probackup backup -B -B ${backup_dir} --instance ${instance_name} -b page|detla|ptrack
  • 恢复
## 根据备份集恢复
pg_probackup restore -B ${backup_dir} --instance ${instance_name} -i ${backup_id}

## 不完整恢复,恢复部分database
pg_probackup restore -B ${backup_dir} --instance ${instance_name} --db-include=${database_name1} --db-include=${database_name2}

## 按时间点恢复
pg_probackup restore -B ${backup_dir} --instance ${instance_name} --recovery-target-time='2020-09-22 22:49:34'
pg_probackup restore -B ${backup_dir} --instance ${instance_name} --recovery-target-xid='687'
pg_probackup restore -B ${backup_dir} --instance ${instance_name} --recovery-target-lsn='16/B374D848'
pg_probackup restore -B ${backup_dir} --instance ${instance_name} --recovery-target-name='before_app_upgrade'
pg_probackup restore -B ${backup_dir} --instance ${instance_name} --recovery-target='latest'
pg_probackup restore -B ${backup_dir} --instance ${instance_name} -recovery-target='immediate'
  • 查看备份文件可用性
pg_probackup show -B ${backup_dir} --instance ${instance_name} -i ${backup_id}
  • 查看备份详情
pg_probackup show -B ${backup_dir} --instance ${instance_name}  -i ${backup_id}
#Configuration
backup-mode = FULL                  //备份模式
stream = false                      //是否启用stream
compress-alg = none
compress-level = 1                  //压缩等级
from-replica = false

#Compatibility
block-size = 8192                   //blocksize
xlog-block-size = 8192
checksum-version = 0
program-version = 2.4.2
server-version = 12                 //PG Version

#Result backup info
timelineid = 1
start-lsn = 0/A000028
stop-lsn = 0/B0000B8
start-time = '2020-09-22 22:49:33+08'
end-time = '2020-09-22 22:49:39+08'
recovery-xid = 658
recovery-time = '2020-09-22 22:49:34+08'
data-bytes = 41423956
wal-bytes = 16777216
uncompressed-bytes = 41389959
pgdata-bytes = 41389720
status = OK
primary_conninfo = 'user=postgres port=5432 sslmode=disable sslcompression=0 gssencmode=disable krbsrvname=postgres target_session_attrs=any'
content-crc = 1486842437

  • 查看归档详情
pg_probackup show -B ${backup_dir} --instance ${instance_name} --archive
  • 配置 Retention Policy

–retention-redundancy=redundancy : 保留备份多少天 FULL

–retention-window=window : 可恢复多少天之前备份

pg_probackup set-config -B ${backup_dir} --instance ${instance_name} --retention-redundancy 7 --retention-window 7


$ /usr/local/pg_probackup-2.4.2/pg_probackup show-config -B /data/pgdata_probackup/ --instance local_5432
# Backup instance information
pgdata = /data/pgsql12/data
system-identifier = 6870373621203487994
xlog-seg-size = 16777216
# Connection parameters
pgdatabase = postgres
# Replica parameters
replica-timeout = 5min
# Archive parameters
archive-timeout = 5min
# Logging parameters
log-level-console = INFO
log-level-file = OFF
log-filename = pg_probackup.log
log-rotation-size = 0TB
log-rotation-age = 0d
# Retention parameters
retention-redundancy = 7
retention-window = 7
wal-depth = 0
# Compression parameters
compress-algorithm = none
compress-level = 1
# Remote access parameters
remote-proto = ssh

  • 删除过期数据
pg_probackup delete -B ${backup_dir} --instance ${instance_name} --delete-expired

--同时删除过期WAL
pg_probackup delete -B ${backup_dir} --instance ${instance_name} --delete-expired --delete-wal

--使用新策略覆盖当前策略删除
pg_probackup delete -B ${backup_dir} --instance ${instance_name} --delete-expired --delete-wal --retention-window=1 --retention-redundancy=1

四、工具使用

4.1 初始化配置

1、初始化备份目录

## 初始化备份目录
$ /usr/local/pg_probackup-2.4.2/pg_probackup init -B /data/pgdata_probackup
INFO: Backup catalog '/data/pgdata_probackup' successfully inited

## 初始化其实就是在该目录下创建一个backups目录一个wal目录
$ pwd
/data/pgdata_probackup
$ tree
.
├── backups
└── wal

2 directories, 0 files

2、一些必要的检查

1)本地备份

数据库 pg_hba.conf 配置文件中对 connection 和 replication 的放通

local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

2)远程备份

互信配置

## 在远程备份实例主机上
# su - postgres
$ ssh-keygen            
$ ssh-copy-id postgres@${备份机_ip}

## 在备份机上
# su - postgres
$ ssh-keygen            
$ ssh-copy-id postgres@${备份实例主机_ip}

## 测试互信
$ ssh postgres@${对方IP}

数据库 pg_hba.conf 配置文件中对 connection 和 replication 的放通

local   all             all                                     md5
host    all             all             xxx(备份机)             trust

3)备份数据库账号准备

若使用的数据库账号不是postgres超级账号,如果使用数据库账号backup进行备份工作,那么需要授予以下权限:

## For PostgreSQL 9.5:
BEGIN;
CREATE ROLE backup WITH LOGIN;
GRANT USAGE ON SCHEMA pg_catalog TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.current_setting(text) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_is_in_recovery() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_start_backup(text, boolean) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_stop_backup() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_create_restore_point(text) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_switch_xlog() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.txid_current() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.txid_current_snapshot() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.txid_snapshot_xmax(txid_snapshot) TO backup;
COMMIT;

## For Postgres Pro 9.6:
BEGIN;
CREATE ROLE backup WITH LOGIN;
GRANT USAGE ON SCHEMA pg_catalog TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.current_setting(text) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_is_in_recovery() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_start_backup(text, boolean, boolean) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_stop_backup(boolean) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_create_restore_point(text) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_switch_xlog() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_last_xlog_replay_location() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.txid_current() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.txid_current_snapshot() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.txid_snapshot_xmax(txid_snapshot) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_checkpoint() TO backup;
COMMIT;

## For Postgres Pro 10 or higher:
BEGIN;
CREATE ROLE backup WITH LOGIN;
GRANT USAGE ON SCHEMA pg_catalog TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.current_setting(text) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_is_in_recovery() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_start_backup(text, boolean, boolean) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_stop_backup(boolean, boolean) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_create_restore_point(text) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_switch_wal() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_last_wal_replay_lsn() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.txid_current() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.txid_current_snapshot() TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.txid_snapshot_xmax(txid_snapshot) TO backup;
GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_checkpoint() TO backup;
COMMIT;

3、添加需要备份实例信息

一个备份目录下可添加多个实例,可以是本地实例也可以是远程实例

–instance : 指定instance的名称
–remote-host : 指定远程备份实例IP
–remote-port : 指定远程备份实例SSH端口
–remote-user : 指定远程SSH用户
–remote-path : 指定远程备份实例pg_probackup工具所在路径

1)添加本地备份实例

## 添加本地实例
$ /usr/local/pg_probackup-2.4.2/pg_probackup  add-instance -B /data/pgdata_probackup -D /data/pg5433/ --instance local_5433
INFO: Instance 'local_5433' successfully inited

$ /usr/local/pg_probackup-2.4.2/pg_probackup  add-instance -B /data/pgdata_probackup -D /data/pgsql12/data/ --instance local_5432
INFO: Instance 'local_5432' successfully inited

2)添加远程备份实例

## 添加远程实例
$ /usr/local/pg_probackup-2.4.2/pg_probackup  add-instance -B /data/pgdata_probackup/ -D /data/pgsql/data --instance zijie_5432 --remote-proto=ssh --remote-host=120.27.250.75 --remote-port=22 --remote-user=postgres --remote-path=/usr/local/pg_probackup-2.4.2/
INFO: Instance 'zijie_5432' successfully inited

4、修改需要备份实例的配置文件

1)本地备份实例

## 本地备份实例
$ vi postgres.conf
max_wal_senders     设置合理值
wal_level = 'replica'
archive_mode = 'on'
archive_command = '/usr/local/pg_probackup-2.4.2/pg_probackup archive-push -B /data/pgdata_probackup --instance local_5432 --wal-file-path=%p --wal-file-name=%f'

2)远程备份实例

## 远程备份实例
max_wal_senders     设置合理值
wal_level = 'replica'
archive_mode = 'on'
archive_command = '/usr/local/pg_probackup-2.4.2/pg_probackup archive-push -B /data/pgdata_probackup --instance zijie_5432 --wal-file-path=%p --wal-file-name=%f --remote-proto=ssh --remote-host=124.70.209.222 --remote-port=22 --remote-user=postgres --remote-path=/usr/local/pg_probackup-2.4.2/'

4.2 数据备份(全备+增备)

1)本机实例数据备份

## 记录t1表中原本的数据量
db1=# select count(*) from t1;
 count
-------
     8
(1 row)

## 进行第一次全备
$ /usr/local/pg_probackup-2.4.2/pg_probackup backup -B /data/pgdata_probackup/ --instance local_5432 -b full
INFO: Backup start, pg_probackup version: 2.4.2, instance: local_5432, backup ID: QH2EIL, backup mode: FULL, wal mode: ARCHIVE, remote: false, compress-algorithm: none, compress-level: 1
WARNING: This PostgreSQL instance was initialized without data block checksums. pg_probackup have no way to detect data block corruption without them. Reinitialize PGDATA with option '--data-checksums'.
WARNING: Current PostgreSQL role is superuser. It is not recommended to run backup or checkdb as superuser.
INFO: Wait for WAL segment /data/pgdata_probackup/wal/local_5432/00000001000000000000000A to be archived
INFO: PGDATA size: 39MB
INFO: Start transferring data files
INFO: Data files are transferred, time elapsed: 0
INFO: wait for pg_stop_backup()
INFO: pg_stop backup() successfully executed
INFO: Syncing backup files to disk
INFO: Backup files are synced, time elapsed: 4s
INFO: Validating backup QH2EIL
INFO: Backup QH2EIL data files are valid
INFO: Backup QH2EIL resident size: 40MB
INFO: Backup QH2EIL completed


## 查看备份信息
$ /usr/local/pg_probackup-2.4.2/pg_probackup show -B /data/pgdata_probackup/

BACKUP INSTANCE 'local_5432'
===================================================================================================================================
 Instance    Version  ID      Recovery Time           Mode  WAL Mode  TLI   Time  Data   WAL  Zratio  Start LSN  Stop LSN   Status
===================================================================================================================================
 local_5432  12       QH2EIL  2020-09-22 22:49:34+08  FULL  ARCHIVE   1/0     6s  40MB  16MB    1.00  0/A000028  0/B0000B8  OK
 local_5432  ----     QH2EI6  ----                    FULL  ARCHIVE   0/0      0     0     0    1.00  0/0        0/0        ERROR
 local_5432  ----     QH2EHB  ----                    FULL  ARCHIVE   0/0      0     0     0    1.00  0/0        0/0        ERROR
 local_5432  ----     QH2EGB  ----                    FULL  ARCHIVE   0/0      0     0     0    1.00  0/0        0/0        ERROR
 local_5432  ----     QH2EFM  ----                    FULL  ARCHIVE   0/0      0     0     0    1.00  0/0        0/0        ERROR
 local_5432  12       QH2E90  ----                    FULL  ARCHIVE   1/0  3m:5s     0     0    1.00  0/5000028  0/0        ERROR

BACKUP INSTANCE 'local_5433'
=================================================================================================================
 Instance  Version  ID  Recovery Time  Mode  WAL Mode  TLI  Time  Data  WAL  Zratio  Start LSN  Stop LSN  Status
=================================================================================================================


## 手动插入部分数据,模拟增量数据
db1=# select count(*) from t1;
 count
-------
    16
(1 row)

## 在全备的基础上进行第一次增量备份
$ /usr/local/pg_probackup-2.4.2/pg_probackup backup -B /data/pgdata_probackup/ --instance local_5432 -b page
INFO: Backup start, pg_probackup version: 2.4.2, instance: local_5432, backup ID: QH2F68, backup mode: PAGE, wal mode: ARCHIVE, remote: false, compress-algorithm: none, compress-level: 1
WARNING: This PostgreSQL instance was initialized without data block checksums. pg_probackup have no way to detect data block corruption without them. Reinitialize PGDATA with option '--data-checksums'.
WARNING: Current PostgreSQL role is superuser. It is not recommended to run backup or checkdb as superuser.
INFO: Wait for WAL segment /data/pgdata_probackup/wal/local_5432/00000001000000000000000D to be archived
INFO: Parent backup: QH2EIL
INFO: PGDATA size: 39MB
INFO: Extracting pagemap of changed blocks
INFO: Pagemap successfully extracted, time elapsed: 0 sec
INFO: Start transferring data files
INFO: Data files are transferred, time elapsed: 0
INFO: wait for pg_stop_backup()
INFO: pg_stop backup() successfully executed
INFO: Syncing backup files to disk
INFO: Backup files are synced, time elapsed: 0
INFO: Validating backup QH2F68
INFO: Backup QH2F68 data files are valid
INFO: Backup QH2F68 resident size: 152kB
INFO: Backup QH2F68 completed



## 查看备份信息
$ /usr/local/pg_probackup-2.4.2/pg_probackup show -B /data/pgdata_probackup/

BACKUP INSTANCE 'local_5432'
====================================================================================================================================
 Instance    Version  ID      Recovery Time           Mode  WAL Mode  TLI   Time   Data   WAL  Zratio  Start LSN  Stop LSN   Status
====================================================================================================================================
 local_5432  12       QH2F68  2020-09-22 23:03:45+08  PAGE  ARCHIVE   1/1     2s  152kB  16MB    1.00  0/D000028  0/E0000F0  OK         //第一次增备
 local_5432  12       QH2EIL  2020-09-22 22:49:34+08  FULL  ARCHIVE   1/0     6s   40MB  16MB    1.00  0/A000028  0/B0000B8  OK         //全备
 local_5432  ----     QH2EI6  ----                    FULL  ARCHIVE   0/0      0      0     0    1.00  0/0        0/0        ERROR
 local_5432  ----     QH2EHB  ----                    FULL  ARCHIVE   0/0      0      0     0    1.00  0/0        0/0        ERROR
 local_5432  ----     QH2EGB  ----                    FULL  ARCHIVE   0/0      0      0     0    1.00  0/0        0/0        ERROR
 local_5432  ----     QH2EFM  ----                    FULL  ARCHIVE   0/0      0      0     0    1.00  0/0        0/0        ERROR
 local_5432  12       QH2E90  ----                    FULL  ARCHIVE   1/0  3m:5s      0     0    1.00  0/5000028  0/0        ERROR

BACKUP INSTANCE 'local_5433'
=================================================================================================================
 Instance  Version  ID  Recovery Time  Mode  WAL Mode  TLI  Time  Data  WAL  Zratio  Start LSN  Stop LSN  Status
=================================================================================================================

2)远程实例数据备份

## 远程实例的全量备份
$ /usr/local/pg_probackup-2.4.2/pg_probackup backup -B /data/pgdata_probackup/ --instance zijie_5432 --remote-user=postgres --remote-host=120.27.250.75 --remote-port=22 -b full
INFO: Backup start, pg_probackup version: 2.4.2, instance: zijie_5432, backup ID: QH3V3Y, backup mode: FULL, wal mode: ARCHIVE, remote: true, compress-algorithm: none, compress-level: 1
Password:
WARNING: This PostgreSQL instance was initialized without data block checksums. pg_probackup have no way to detect data block corruption without them. Reinitialize PGDATA with option '--data-checksums'.
WARNING: Current PostgreSQL role is superuser. It is not recommended to run backup or checkdb as superuser.
INFO: Wait for WAL segment /data/pgdata_probackup/wal/zijie_5432/0000000100000000000000E7 to be archived
WARNING: By default pg_probackup assume WAL delivery method to be ARCHIVE. If continuous archiving is not set up, use '--stream' option to make autonomous backup. Otherwise check that continuous archiving works correctly.
INFO: PGDATA size: 59MB
INFO: Start transferring data files
INFO: Data files are transferred, time elapsed: 7m:41s
INFO: wait for pg_stop_backup()
INFO: pg_stop backup() successfully executed
INFO: Wait for LSN 0/E80001D8 in archived WAL segment /data/pgdata_probackup/wal/zijie_5432/0000000100000000000000E8
INFO: Syncing backup files to disk
INFO: Backup files are synced, time elapsed: 0
INFO: Validating backup QH3V3Y
INFO: Backup QH3V3Y data files are valid
INFO: Backup QH3V3Y resident size: 43MB
INFO: Backup QH3V3Y completed

## 查看备份信息
$ /usr/local/pg_probackup-2.4.2/pg_probackup show -B /data/pgdata_probackup/ --instance zijie_5432
=======================================================================================================================================
 Instance    Version  ID      Recovery Time           Mode  WAL Mode  TLI     Time  Data   WAL  Zratio  Start LSN   Stop LSN    Status
=======================================================================================================================================
 zijie_5432  12       QH3V3Y  2020-09-23 17:57:28+08  FULL  ARCHIVE   1/0  13m:59s  43MB  16MB    1.00  0/E7000028  0/E80001D8  OK
 zijie_5432  12       QH3U2S  ----                    FULL  ARCHIVE   1/0  17m:12s  54MB     0    1.00  0/E4000028  0/0         ERROR
 zijie_5432  ----     QH3U2J  ----                    FULL  ARCHIVE   0/0       4s     0     0    1.00  0/0         0/0         ERROR
 zijie_5432  12       QH3U20  ----                    FULL  ARCHIVE   0/0       5s     0     0    1.00  0/0         0/0         ERROR

3)备份文件可用性校验

## 备份文件校验
$ /usr/local/pg_probackup-2.4.2/pg_probackup validate -B /data/pgdata_probackup/ --instance local_5432
INFO: Validate backups of the instance 'local_5432'
INFO: Validating backup QH2F68
INFO: Backup QH2F68 data files are valid
INFO: Backup QH2F68 WAL segments are valid
INFO: Validating backup QH2EIL
INFO: Backup QH2EIL data files are valid
INFO: Backup QH2EIL WAL segments are valid
ERROR: Backup QH2EI6 has server version , but current pg_probackup binary compiled with server version 12

$ /usr/local/pg_probackup-2.4.2/pg_probackup validate -B /data/pgdata_probackup/ --instance zijie_5432
INFO: Validate backups of the instance 'zijie_5432'
INFO: Validating backup QH3V3Y
INFO: Backup QH3V3Y data files are valid
INFO: Backup QH3V3Y WAL segments are valid
WARNING: Backup QH3U2S has status ERROR. Skip validation.
ERROR: Backup QH3U2J has server version , but current pg_probackup binary compiled with server version 12

4.3 数据恢复

1、全量备份恢复

# 先关闭源数据库并备份原先的数据目录
$ pg_ctl -D /data/pgsql12/data/ stop
waiting for server to shut down.... done
server stopped
$ mv /data/pgsql12/data/ /data/pgsql12/data_bak/

## 通过全备恢复
$ /usr/local/pg_probackup-2.4.2/pg_probackup restore -B /data/pgdata_probackup/ --instance local_5432 --recovery-target-time='2020-09-22 22:49:34'
INFO: Validating backup QH2EIL
INFO: Backup QH2EIL data files are valid
INFO: Backup validation completed successfully on time 2020-09-22 22:49:34+08, xid 658 and LSN 0/B0000B8
INFO: Backup QH2EIL is valid.
INFO: Restoring the database from backup at 2020-09-22 22:49:33+08
INFO: Start restoring backup files. PGDATA size: 39MB
INFO: Backup files are restored. Transfered bytes: 39MB, time elapsed: 0
INFO: Restore incremental ratio (less is better): 100% (39MB/39MB)
INFO: Syncing restored files to disk
INFO: Restored backup files are synced, time elapsed: 4s
INFO: Restore of backup QH2EIL completed.

## 启动数据库并检查t1表数据恢复情况
$ pg_ctl -D /data/pgsql12/data/ start
waiting for server to start....2020-09-22 23:21:23.529 CST [32368] LOG:  starting PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2020-09-22 23:21:23.530 CST [32368] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-09-22 23:21:23.530 CST [32368] LOG:  listening on IPv6 address "::", port 5432
2020-09-22 23:21:23.537 CST [32368] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-09-22 23:21:23.552 CST [32368] LOG:  redirecting log output to logging collector process
2020-09-22 23:21:23.552 CST [32368] HINT:  Future log output will appear in directory "/data/pgsql12/logs".
 done
server started

db1=# select count(*) from t1;
 count
-------
     8              //可以看到数据恢复成功
(1 row)

2、增量数据恢复

## 再次关闭数据库并删除已有的数据目录
$ pg_ctl -D /data/pgsql12/data/ stop
waiting for server to shut down.... done
server stopped
$ rm -rf /data/pgsql12/data

## 将数据恢复到第一次增备时间点,并检查t1表的数据恢复情况
$ /usr/local/pg_probackup-2.4.2/pg_probackup restore -B /data/pgdata_probackup/ --instance local_5432 --recovery-target-time='2020-09-22 23:03:45'
INFO: Validating parents for backup QH2F68
INFO: Validating backup QH2EIL
INFO: Backup QH2EIL data files are valid
INFO: Validating backup QH2F68
INFO: Backup QH2F68 data files are valid
INFO: Backup validation completed successfully on time 2020-09-22 23:03:45+08, xid 659 and LSN 0/E0000F0
INFO: Backup QH2F68 is valid.
INFO: Restoring the database from backup at 2020-09-22 23:03:44+08
INFO: Start restoring backup files. PGDATA size: 39MB
INFO: Backup files are restored. Transfered bytes: 39MB, time elapsed: 0
INFO: Restore incremental ratio (less is better): 100% (39MB/39MB)
INFO: Syncing restored files to disk
INFO: Restored backup files are synced, time elapsed: 4s
INFO: Restore of backup QH2F68 completed.
[postgres@sansi_test pgsql12]$ pg_ctl -D /data/pgsql12/data/ start
waiting for server to start....2020-09-22 23:22:42.746 CST [32391] LOG:  starting PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2020-09-22 23:22:42.746 CST [32391] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-09-22 23:22:42.746 CST [32391] LOG:  listening on IPv6 address "::", port 5432
2020-09-22 23:22:42.750 CST [32391] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-09-22 23:22:42.761 CST [32391] LOG:  redirecting log output to logging collector process
2020-09-22 23:22:42.761 CST [32391] HINT:  Future log output will appear in directory "/data/pgsql12/logs".
 done
server started

db1=# select count(*) from t1;
 count
-------
    16          //可以看到数据恢复成功
(1 row)

```

参考连接

https://www.modb.pro/db/21780

https://cloud.tencent.com/developer/article/1526102

https://postgrespro.com/docs/postgrespro/12/app-pgprobackup <官方文档>

;