当前位置:   article > 正文

PostgreSQL备份恢复管理器pg_probackup

PostgreSQL备份恢复管理器pg_probackup

参考:
https://github.com/postgrespro/pg_probackup
https://postgrespro.github.io/pg_probackup/

pg_probackup简介

pg_probackup是由俄罗斯PG社区组织Postgres Professional发布的一个用于管理PostgreSQL数据库备份和恢复的工具。
它用于执行PostgreSQL实例的定期备份,使你能够在失败时恢复服务器。
最新版2.2.7支持PostgreSQL 9.5, 9.6, 10, 11, 12

优点:

1.增量备份:支持三种不同模式
2.数据有效性自动检测
3.验证:基于checkdb命令
4.历史过期数据处理
5.并行化:支持backup、restore、merge、delete、validate、checkdb
6.压缩
7.Deduplication:重复数据不备份(比如_vm或者_fsm)
8.支持远程操作:例如SSH
9.支持从standby备份
10.数据存储在外部目录:与PGDATA保持独立
11.备份元数据管理:命令配置持久化到文本文件
12.归档元数据管理:命令配置持久化到文本文件
13.并行恢复

使用限制

1.支持9.5+
2.远程备份不支持windows
3.Unix系统,PG10+,备份用户必须是安装PG的操作系统用户
4.PG9.5,非超级用户角色备份可能比超级用户备份慢
5.参数block_size 和wal_block_size备份和恢复服务器要一致
6.增量备份要在同一个时间线(timeline)下
7.从standby备份
支持PG版本 9.6+
备库配置 hot_standby = on
主库配置 full_page_writes = on

增量备份三种模式

第一种:DELTA backup
此模式读取PGDATA所有数据文件,拷贝上次备份以来的所有PAGE,IO压力与全备类似。

第二种:PAGE backup
此模式扫描归档目录里上次扫过之后的WAL文件,只拷贝WAL的PAGE,
需要配置持续归档才能使用。

第三种:PTRACK backup
仅支持Postgres Pro Standard and Postgres Pro Enterprise

演示环境

remote_host 192.168.99.200
backup_host 192.168.99.223
pg_probackup 2.2.7
pg 12.1

安装pg_probackup

源码编译安装

  1. 下载文件
  2. $ wget https://github.com/postgrespro/pg_probackup/archive/2.2.7.tar.gz
  3. 解压源码
  4. $ tar zxvf 2.2.7.tar.gz
  5. 进入源码目录
  6. $ cd pg_probackup-2.2.7/
  7. 编译
  8. $ make USE_PGXS=1 PG_CONFIG=/opt/pgsql/bin/pg_config top_srcdir=/opt/postgresql-12.1
  9. 安装
  10. $ make USE_PGXS=1 PG_CONFIG=/opt/pgsql/bin/pg_config top_srcdir=/opt/postgresql-12.1 install
  11. 查看安装版本
  12. $ /opt/pgsql/bin/pg_probackup version
  13. pg_probackup 2.2.7 (PostgreSQL 12.1)

RPM安装

  1. #RPM Centos Packages
  2. rpm -ivh http://repo.postgrespro.ru/pg_probackup/keys/pg_probackup-repo-centos.noarch.rpm
  3. yum install pg_probackup-{12,11,10,9.6,9.5}
  4. yum install pg_probackup-{12,11,10,9.6,9.5}-debuginfo
  5. #RPM RHEL Packages
  6. rpm -ivh http://repo.postgrespro.ru/pg_probackup/keys/pg_probackup-repo-rhel.noarch.rpm
  7. yum install pg_probackup-{12,11,10,9.6,9.5}
  8. yum install pg_probackup-{12,11,10,9.6,9.5}-debuginfo

配置

  1. /opt/pgsql/bin/initdb -D /home/postgres/data5432 \
  2. --allow-group-access \
  3. -Upostgres -W
  4. 1.Initialize the backup catalog
  5. 使用postgres用户执行pg_probackup创建backup catalog
  6. $ /opt/pgsql/bin/pg_probackup init \
  7. -B /home/postgres/pgdata_probackup
  8. 2.Add a new backup instance to the backup catalog.
  9. pg_probackup可以在一个backup catalog存储多个实例
  10. 添加backup_host本地备份实例
  11. $ /opt/pgsql/bin/pg_probackup add-instance \
  12. -B /home/postgres/pgdata_probackup \
  13. -D /home/postgres/data5432 \
  14. --instance local_5432
  15. 添加远程备份实例
  16. $ /opt/pgsql/bin/pg_probackup add-instance \
  17. -B /home/postgres/pgdata_probackup \
  18. -D /home/postgres/data5432 \
  19. --instance pg200_5432 \
  20. --remote-proto=ssh \
  21. --remote-host=192.168.99.200 \
  22. --remote-port=22 \
  23. --remote-user=postgres \
  24. --remote-path=/opt/pgsql/bin \
  25. --ssh-options='-o ServerAliveCountMax=5 -o ServerAliveInterval=60'
  26. 删除备份实例
  27. /opt/pgsql/bin/pg_probackup del-instance \
  28. -B /home/postgres/pgdata_probackup \
  29. --instance pg200_5432
  30. 上面操作需要配置SSH信任
  31. $ ssh-copy-id postgres@192.168.99.200
  32. 3.Configure the database cluster to enable pg_probackup backups.
  33. 如果使用非postgres用户需要配置以下权限,本文使用postgres用户操作
  34. $ psql -p5432 -Upostgres
  35. BEGIN;
  36. CREATE ROLE backup WITH LOGIN REPLICATION;
  37. GRANT USAGE ON SCHEMA pg_catalog TO backup;
  38. GRANT EXECUTE ON FUNCTION pg_catalog.current_setting(text) TO backup;
  39. GRANT EXECUTE ON FUNCTION pg_catalog.pg_is_in_recovery() TO backup;
  40. GRANT EXECUTE ON FUNCTION pg_catalog.pg_start_backup(text, boolean, boolean) TO backup;
  41. GRANT EXECUTE ON FUNCTION pg_catalog.pg_stop_backup(boolean, boolean) TO backup;
  42. GRANT EXECUTE ON FUNCTION pg_catalog.pg_create_restore_point(text) TO backup;
  43. GRANT EXECUTE ON FUNCTION pg_catalog.pg_switch_wal() TO backup;
  44. GRANT EXECUTE ON FUNCTION pg_catalog.pg_last_wal_replay_lsn() TO backup;
  45. GRANT EXECUTE ON FUNCTION pg_catalog.txid_current() TO backup;
  46. GRANT EXECUTE ON FUNCTION pg_catalog.txid_current_snapshot() TO backup;
  47. GRANT EXECUTE ON FUNCTION pg_catalog.txid_snapshot_xmax(txid_snapshot) TO backup;
  48. GRANT EXECUTE ON FUNCTION pg_catalog.pg_control_checkpoint() TO backup;
  49. COMMIT;
  50. 4.postgresql.conf配置
  51. $ vi data5432/postgresql.conf
  52. max_wal_senders设置合理值
  53. wal_level = 'replica'
  54. archive_mode = 'on'
  55. 本地实例
  56. archive_command = '/opt/pgsql/bin/pg_probackup archive-push
  57. -B /home/postgres/pgdata_probackup
  58. --instance local_5432
  59. --wal-file-path=%p
  60. --wal-file-name=%f'
  61. 200远程实例
  62. archive_command = '/opt/pgsql/bin/pg_probackup archive-push
  63. -B /home/postgres/pgdata_probackup
  64. --instance pg200_5432 --wal-file-path=%p --wal-file-name=%f [remote_options]'
  65. remote_options参考如下
  66. --remote-proto=ssh \
  67. --remote-host=192.168.99.223 \
  68. --remote-port=22 \
  69. --remote-user=postgres \
  70. --remote-path=/opt/pgsql/bin \
  71. --ssh-options='-o ServerAliveCountMax=5 -o ServerAliveInterval=60'
  72. $ ssh-copy-id postgres@192.168.99.223

全备(Full backups)

  1. backup_host备份本地实例
  2. $ /opt/pgsql/bin/pg_probackup backup \
  3. -B /home/postgres/pgdata_probackup \
  4. --instance local_5432 \
  5. -b full
  6. backup_host备份远程实例
  7. $ /opt/pgsql/bin/pg_probackup backup \
  8. -B /home/postgres/pgdata_probackup \
  9. --instance pg200_5432 \
  10. -b full
  11. 如果需包含外部目录
  12. --external-dirs=/etc/dir1:/etc/dir2

增量备份(DELTA)

  1. $ /opt/pgsql/bin/pg_probackup backup \
  2. -B /home/postgres/pgdata_probackup \
  3. --instance local_5432 \
  4. -b delta
  5. $ /opt/pgsql/bin/pg_probackup backup \
  6. -B /home/postgres/pgdata_probackup \
  7. --instance pg200_5432 \
  8. -b delta

增量备份(PAGE)

  1. $ /opt/pgsql/bin/pg_probackup backup \
  2. -B /home/postgres/pgdata_probackup \
  3. --instance local_5432 \
  4. -b page
  5. $ /opt/pgsql/bin/pg_probackup backup \
  6. -B /home/postgres/pgdata_probackup \
  7. --instance pg200_5432 \
  8. -b page

查看可用备份

$ pg_probackup show -B /home/postgres/pgdata_probackup/

查看备份详细

  1. $ pg_probackup show \
  2. -B /home/postgres/pgdata_probackup/ \
  3. --instance pg200_5432 \
  4. -i Q5Q3O0

查看归档详细

  1. $ pg_probackup show \
  2. -B /home/postgres/pgdata_probackup/ \
  3. --instance pg200_5432 \
  4. --archive

配置 Retention Policy

  1. pg_probackup set-config \
  2. -B /home/postgres/pgdata_probackup/ \
  3. --instance pg200_5432 \
  4. --retention-redundancy=20
  5. pg_probackup set-config \
  6. -B /home/postgres/pgdata_probackup/ \
  7. --instance pg200_5432 \
  8. --retention-window=7

删除过期数据

  1. pg_probackup delete \
  2. -B /home/postgres/pgdata_probackup/ \
  3. --instance pg200_5432 \
  4. --delete-expired
  5. --同时删除过期WAL
  6. pg_probackup delete \
  7. -B /home/postgres/pgdata_probackup/ \
  8. --instance pg200_5432 \
  9. --delete-expired \
  10. --delete-wal
  11. --使用新策略覆盖当前策略删除
  12. pg_probackup delete \
  13. -B /home/postgres/pgdata_probackup/ \
  14. --instance pg200_5432 \
  15. --delete-expired --delete-wal \
  16. --retention-window=1 --retention-redundancy=1

检测PG实例checksum

  1. pg_probackup checkdb \
  2. --backup-path=/home/postgres/pgdata_probackup \
  3. --instance local_5432 \
  4. --pgdata=/opt/data5432 \
  5. --pguser=postgres \
  6. --pgdatabase=postgres \
  7. --pgport=5432

恢复

备份有效性检测

检测所有备份

  1. pg_probackup validate \
  2. --backup-path=/home/postgres/pgdata_probackup \
  3. --instance local_5432

检测单个备份

  1. pg_probackup validate \
  2. --backup-path=/home/postgres/pgdata_probackup \
  3. --instance local_5432 \
  4. --backup-id=QMZB1Q
  1. 223 back host操作
  2. pg_probackup restore \
  3. -B /home/postgres/pgdata_probackup/ \
  4. -D /home/postgres/data5432 \
  5. --instance pg200_5432 \
  6. --remote-user=postgres \
  7. --remote-host=192.168.99.200 \
  8. --remote-port=22 \
  9. --archive-host=192.168.99.223 \
  10. --archive-port=22 \
  11. --archive-user=postgres
  12. 恢复之后需要重做基础备份,后续才能继续做增量备份
  13. /opt/pgsql/bin/pg_probackup backup \
  14. -B /home/postgres/pgdata_probackup \
  15. --instance pg200_5432 \
  16. -b full
  17. 再次恢复
  18. pg_probackup restore \
  19. -B /home/postgres/pgdata_probackup/ \
  20. -D /home/postgres/data5432 \
  21. --instance pg200_5432 \
  22. --remote-user=postgres \
  23. --remote-host=192.168.99.200 \
  24. --remote-port=22 \
  25. --archive-host=192.168.99.223 \
  26. --archive-port=22 \
  27. --archive-user=postgres
保持联系

本人组建了一个技术交流群:PG乐知乐享交流群。欢迎关注文章的小伙伴随缘加入,进群请加本人微信skypkmoon并备注PG乐知乐享。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/525283
推荐阅读
相关标签
  

闽ICP备14008679号