作者:太阳
一、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 包装置部署
rpm -ivh https://repo.postgrespro.ru/pg_probackup/keys/pg_probackup-repo-centos.noarch.rpm
yum install pg_probackup-13
yum install pg_probackup-13-debuginfo
三、常用命令
初始化备份目录
$ 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
更多技术信息请查看云掣官网 https://yunche.pro/?t=yrgw