PostgreSQL备份工具-pg_probackup

pg_probackup是由俄罗斯PG社区组织Postgres Professional发布的一个用于管理PostgreSQL数据库备份和恢复的工具。支持PostgreSQL9.5+的版本;是基于pg_rman扩展而来

2|0安装和设置

wget https://github.com/postgrespro/pg_probackup/archive/2.2.7.tar.gz tar -zxvf 2.2.7.tar.gz  cd pg_probackup-2.2.7/ make USE_PGXS=1 PG_CONFIG=/opt/pg12/bin/pg_config top_srcdir=/root/package/postgresql-12.2 make USE_PGXS=1 PG_CONFIG=/opt/pg12/bin/pg_config top_srcdir=/root/package/postgresql-12.2 install

一旦安装了pg_probackup,请完成以下设置:

2|1初始化备份目录

所谓初始化:就是创建一个备份目录;用于存放备份文件和归档日志文件

初始化备份目录,请运行以下命令:

pg_probackup init -B backup_dir -- 示例 pg_probackup init -B /data/postgres/probackup

pg_probackup创建备份目录,其中包含以下子目录:

  • wal/-wal文件目录。
  • backups/-备份文件的目录。

备份目录初始化了;我们可以添加备份实例

2|2向备份目录添加新的备份instance

pg_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 /data/postgres/data --instance local_6000
  • data_dir是要备份的群集的数据目录。要设置和使用pg_probackup,需要对此目录的写访问权限。
  • instance_name是将存储此群集的WAL和备份文件的子目录的名称。
  • remote_options是可选参数,仅当data_dir位于远程系统上时,才需要指定这些参数。后续在详细讲解,参考

pg_probackup在备份目录的 backups 和 wal 目录下创建 instance_name 子目录。backups/instance_name 目录包含 pg_probackup.conf 控制此备份实例的pg_probackup设置的配置文件。如果使用remote_选项运行此命令,则指定的参数将添加到pg_probackup.conf。

配置查看后面章节

2|3配置PostgreSQL集群

如果使用非postgres用户需要配置以下权限 For PostgreSQL 10 or higher: $ psql -p 5432 -U postgres BEGIN; CREATE ROLE backup WITH LOGIN REPLICATION; 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;

2|4postgresql.conf参数设置

  • 确保wal_level参数高于minimal。
  • 如果要在primary上配置存档,则存档模式必须设置为“on”或“always”。要在standby下执行存档,请将此参数设置为“always"
  • 设置archive_命令参数,如下所示:
archive_command = 'install_dir/pg_probackup archive-push -B backup_dir --instance instance_name --wal-file-name=%f [remote_options]' -- 示例 /opt/pg12/bin/pg_probackup archive-push -B /data/postgres/probackup --instance local_6000 --wal-file-path=%p --wal-file-name=%f

3|0

4|0创建全量备份

创建备份,命令

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

示例

pg_probackup backup -B /data/postgres/probackup --instance local_6000 -b full

5|0查看备份

pg_probackup show -B /data/postgres/probackup pg_probackup show -B /data/postgres/probackup --instance local_6000 -i QGQMKH pg_probackup show -B /data/postgres/probackup --instance local_6000 --archive

6|0小试牛刀案例

6|11、基于PITR备份与恢复

-- 备份 pg_probackup backup -B /data/postgres/probackup --instance local_6000 -b full --恢复之前需要先停库;在清理PGDATA目录以及外在的表空间目录 pg_ctl stop;rm -rf $PGDATA/* --恢复到新的PGDATA目录 pg_probackup restore -B /data/postgres/probackup --instance local_6000 -D /data/postgres/data6000 --recovery-target-time='2020-09-23 11:05:17' -- 恢复到原目录 pg_probackup restore -B /data/postgres/probackup --instance local_6000 --recovery-target-time='2020-09-23 11:05:17'

6|22、增量备份

增量备份有3中模式

-- 增量备份DELTA pg_probackup backup -B /data/postgres/probackup --instance local_6000 -b delta -- 增量备份page pg_probackup backup -B /data/postgres/probackup --instance local_6000 -b page -- https://postgrespro.com/docs/postgrespro/12/app-pgprobackup

6|33、远程备份

--set up ssh ssh-copy-id postgres@node2 --备份库 添加备份实例 pg_probackup add-instance -B /data/postgres/probackup -D /data/postgres/data --instance pg207_6000 --remote-proto=ssh --remote-host=192.168.1.207 --remote-port=22  --remote-user=postgres --remote-path=/opt/pg12/bin  --ssh-options='-o ServerAliveCountMax=5 -o ServerAliveInterval=60' --案例库 配置的参数 --备份需要用到postgres超级用户;所以在pg_hba.conf; host    all            postgres      192.168.1.221/24        trust  -- postgresql.conf wal_level = replica archive_mode = on archive_command = '/opt/pg12/bin/pg_probackup archive-push -B /data/postgres/probackup --instance pg207_6000 --wal-file-path=%p --wal-file-name=%f --remote-proto=ssh --remote-host=192.168.1.221 --remote-port=22  --remote-user=postgres --remote-path=/opt/pg12/bin' -- 备份库; 执行远程备份 pg_probackup backup -B /data/postgres/probackup --instance pg207_6000 -b full --remote-proto=ssh --remote-host=192.168.1.207 --remote-port=22  --remote-user=postgres --remote-path=/opt/pg12/bin  --ssh-options='-o ServerAliveCountMax=5 -o ServerAliveInterval=60' --Take a FULL backup: pg_probackup-11 backup -B /mnt/backups --instance 'pg-11' -b FULL --stream --remote-host=postgres_host --remote-user=postgres -U backup -d backupdb --Take an incremental backup in the DELTA mode: pg_probackup-11 backup -B /mnt/backups --instance 'pg-11' -b delta --stream --remote-host=postgres_host --remote-user=postgres -U backup -d backupdb -- pg_probackup-11 set-config -B /mnt/backups --instance 'pg-11' --remote-host=postgres_host --remote-user=postgres -U backup -d backupdb pg_probackup-11 backup -B /mnt/backups --instance 'pg-11' -b delta --stream --pg_probackup-11 show-config -B /mnt/backups --instance 'pg-11' --pg_probackup-11 show -B /mnt/backups --instance 'pg-11' -- 备份库;查看备份 pg_probackup show -B /data/postgres/probackup --instance pg207_6000

6|44、恢复示例

-- 恢复 pg_probackup restore -B /data/postgres/probackup --instance local_6000 --recovery-target-time='2020-09-23 14:54:55'
上一篇:openGauss和PostgreSQL的源码目录结构对比


下一篇:Postgresql从MD5密码验证改为SCRAM-SHA-256