其他分享
首页 > 其他分享> > PG-pg_probackup备份恢复工具

PG-pg_probackup备份恢复工具

作者:互联网

pg_probackup

pg_probackup是由Postgres Professional发布的一个用于管理PostgreSQL 9.5 及以上版本数据库备份和恢复的工具。pg_probackup 实用程序基于 pg_arman,它最初由 NTT 编写,然后由 Michael Paquier 开发和维护。

安装配置

软件安装

源码安装
wget https://codeload.github.com/postgrespro/pg_probackup/tar.gz/refs/tags/2.5.1

make USE_PGXS=1 PG_CONFIG=<path_to_pg_config> top_srcdir=<path_to_PostgreSQL_source_tree>
make USE_PGXS=1 PG_CONFIG=<path_to_pg_config> top_srcdir=<path_to_PostgreSQL_source_tree> install

# 不使用 PGXS
cd <path_to_PostgreSQL_source_tree> && git clone https://github.com/postgrespro/pg_probackup contrib/pg_probackup && cd contrib/pg_probackup && make

rpm包
# pg_probackup for PostgresPro Standard and Enterprise
#DEB Ubuntu|Debian Packages
sudo sh -c 'echo "deb [arch=amd64] https://repo.postgrespro.ru/pg_probackup-forks/deb/ $(lsb_release -cs) main-$(lsb_release -cs)" > /etc/apt/sources.list.d/pg_probackup-forks.list'
sudo wget -O - https://repo.postgrespro.ru/pg_probackup-forks/keys/GPG-KEY-PG_PROBACKUP | sudo apt-key add - && sudo apt-get update
sudo apt-get install pg-probackup-{std,ent}-{12,11,10,9.6}
sudo apt-get install pg-probackup-{std,ent}-{12,11,10,9.6}-dbg

#DEB Astra Linix Orel
sudo sh -c 'echo "deb [arch=amd64] https://repo.postgrespro.ru/pg_probackup-forks/deb/ stretch main-stretch" > /etc/apt/sources.list.d/pg_probackup.list'
sudo wget -O - https://repo.postgrespro.ru/pg_probackup-forks/keys/GPG-KEY-PG_PROBACKUP | sudo apt-key add - && sudo apt-get update
sudo apt-get install pg-probackup-{std,ent}-{12,11,10,9.6}{-dbg,}


#RPM Centos Packages
rpm -ivh https://repo.postgrespro.ru/pg_probackup-forks/keys/pg_probackup-repo-forks-centos.noarch.rpm
yum install pg_probackup-{std,ent}-{12,11,10,9.6}
yum install pg_probackup-{std,ent}-{12,11,10,9.6}-debuginfo

#RPM RHEL Packages
rpm -ivh https://repo.postgrespro.ru/pg_probackup-forks/keys/pg_probackup-repo-forks-rhel.noarch.rpm
yum install pg_probackup-{std,ent}-{12,11,10,9.6}
yum install pg_probackup-{std,ent}-{12,11,10,9.6}-debuginfo

#RPM Oracle Linux Packages
rpm -ivh https://repo.postgrespro.ru/pg_probackup-forks/keys/pg_probackup-repo-forks-oraclelinux.noarch.rpm
yum install pg_probackup-{std,ent}-{12,11,10,9.6}
yum install pg_probackup-{std,ent}-{12,11,10,9.6}-debuginfo

#RPM ALT Linux 7
sudo sh -c 'echo "rpm https://repo.postgrespro.ru/pg_probackup-forks/rpm/latest/altlinux-p7 x86_64 forks" > /etc/apt/sources.list.d/pg_probackup_forks.list'
sudo apt-get update
sudo apt-get install pg_probackup-{std,ent}-{12,11,10,9.6}
sudo apt-get install pg_probackup-{std,ent}-{12,11,10,9.6}-debuginfo

#RPM ALT Linux 8
sudo sh -c 'echo "rpm https://repo.postgrespro.ru/pg_probackup-forks/rpm/latest/altlinux-p8 x86_64 forks" > /etc/apt/sources.list.d/pg_probackup_forks.list'
sudo apt-get update
sudo apt-get install pg_probackup-{std,ent}-{12,11,10,9.6}
sudo apt-get install pg_probackup-{std,ent}-{12,11,10,9.6}-debuginfo

#RPM ALT Linux 9
sudo sh -c 'echo "rpm https://repo.postgrespro.ru/pg_probackup-forks/rpm/latest/altlinux-p9 x86_64 forks" > /etc/apt/sources.list.d/pg_probackup_forks.list' && sudo apt-get update
sudo apt-get install pg_probackup-{std,ent}-{12,11,10,9.6}
sudo apt-get install pg_probackup-{std,ent}-{12,11,10,9.6}-debuginfo

配置

用法
pg_probackup version

pg_probackup help [command]

pg_probackup init -B backup_dir

pg_probackup add-instance -B backup_dir -D data_dir --instance instance_name

pg_probackup del-instance -B backup_dir --instance instance_name

pg_probackup set-config -B backup_dir --instance instance_name [option...]

pg_probackup set-backup -B backup_dir --instance instance_name -i backup_id [option...]

pg_probackup show-config -B backup_dir --instance instance_name [--format=format]

pg_probackup show -B backup_dir [option...]

pg_probackup backup -B backup_dir --instance instance_name -b backup_mode [option...]

pg_probackup restore -B backup_dir --instance instance_name [option...]

pg_probackup checkdb -B backup_dir --instance instance_name -D data_dir [option...]

pg_probackup validate -B backup_dir [option...]

pg_probackup merge -B backup_dir --instance instance_name -i backup_id [option...]

pg_probackup delete -B backup_dir --instance instance_name { -i backup_id | --delete-wal | --delete-expired | --merge-expired } [option...]

pg_probackup archive-push -B backup_dir --instance instance_name --wal-file-name wal_file_name [option...]

pg_probackup archive-get -B backup_dir --instance instance_name --wal-file-path wal_file_path --wal-file-name wal_file_name [option...]

pg_probackup catchup -b catchup_mode --source-pgdata=path_to_pgdata_on_remote_server --destination-pgdata=path_to_local_dir [option...]
初始化catalog

用于存放备份文件和归档日志文件

pg_probackup init -B /data/postgres/probackup

完成上面命令后,它会创建下面的子目录

向备份目录添加新的备份实例
pg_probackup add-instance -B backup_dir -D data_dir --instance instance_name [remote_options]

#-- 本地添加备份instance 示例
pg_probackup add-instance -B /data/postgres/probackup -D $PGDATA --instance local5432
配置PostgreSQL集群
创建用户

建议创建一个具有所选备份策略所需的最低权限的单独角色运行pg_probackup使用程序

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;
服务端参数配置

确保有连续 WAL 归档配置

设置集群验证
CREATE EXTENSION amcheck;
使用Partial Restore(部分还原)配置

pg_catalog.pg_database的只读权限给backup用户角色

GRANT SELECT ON TABLE pg_catalog.pg_database TO backup;
使用远程备份模式
使用PTRACK 备份配置

如果要使用 PTRACK 备份,将执行 PTRACK 备份的角色必须有权访问集群的所有数据库。

使用

创建一个备份

pg_probackup backup -B backup_dir --instance instance_name -b backup_mode

backup_mode 取值:

查看备份

pg_probackup show -B /data/postgres/probackup

恢复备份

全量恢复
pg_probackup restore -B backup_dir --instance instance_name -i backup_id
Partial Restore
仅恢复指定的数据库
pg_probackup restore -B backup_dir --instance instance_name --db-include=database_name

pg_probackup restore -B backup_dir --instance instance_name --db-include=db1 --db-include=db2

从还原中排除一个或多个数据库
pg_probackup restore -B backup_dir --instance instance_name --db-exclude=database_name

pg_probackup restore -B backup_dir --instance instance_name --db-exclude=db1 --db-exclude=db2

基于时间点 (PITR) 恢复
要在准确时间恢复集群
pg_probackup restore -B backup_dir --instance instance_name --recovery-target-time="2021-05-18 14:18:11+03"
将集群恢复到特定事务 ID
pg_probackup restore -B backup_dir --instance instance_name --recovery-target-xid=687
恢复到特定 LSN
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"
将备份恢复到 WAL 存档中可用的最新状态
pg_probackup restore -B backup_dir --instance instance_name --recovery-target="latest"
将集群恢复到最早的一致性点
pg_probackup restore -B backup_dir --instance instance_name --recovery-target='immediate'

使用远程备份恢复模式

远程备份
# postgres 用户通过端口 2302 的 SSH 连接创建位于远程系统上的 Postgres Pro 集群的归档完整备份,主机地址为 192.168.0.2
pg_probackup backup -B backup_dir --instance instance_name -b FULL --remote-user=postgres --remote-host=192.168.0.2 --remote-port=2302

使用远程恢复
# postgres 用户通过端口 2302 的 SSH 连接在主机地址为 192.168.0.2 的远程系统上恢复最新的可用备份
pg_probackup restore -B backup_dir --instance instance_name --remote-user=postgres --remote-host=192.168.0.2 --remote-port=2302

标签:--,instance,catalog,PG,probackup,backup,pg
来源: https://www.cnblogs.com/binliubiao/p/15389151.html