实战篇:PostgreSQL开发与备份管理pg_probackup(第五部分)
发表时间: 2024-03-06 17:07
作者:太阳
pg_probackup 是一款免费的postgres数据库集群备份工具,与其他备份工具相比,它主要有如下一些优势:
pg_probackup 下的几种备份方式:
pg_probackup 工具的一些局限性:
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总用量 176drwxrwxr-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.mddrwxrwxr-x 3 root root 4096 7月 1 08:07 srcdrwxrwxr-x 4 root root 4096 7月 1 08:07 testsdrwxrwxr-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总用量 612drwxrwxr-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.mddrwxrwxr-x 3 root root 4096 9月 22 21:55 srcdrwxrwxr-x 4 root root 4096 7月 1 08:07 testsdrwxrwxr-x 2 root root 4096 7月 1 08:07 travis
3、版本检查
# 查看版本# ./pg_probackup --versionpg_probackup 2.4.2 (PostgreSQL 12.2)
rpm -ivh https://repo.postgrespro.ru/pg_probackup/keys/pg_probackup-repo-centos.noarch.rpmyum install pg_probackup-13yum 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}## 不完整恢复,恢复部分databasepg_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}#Configurationbackup-mode = FULL //备份模式stream = false //是否启用streamcompress-alg = nonecompress-level = 1 //压缩等级from-replica = false#Compatibilityblock-size = 8192 //blocksizexlog-block-size = 8192checksum-version = 0program-version = 2.4.2server-version = 12 //PG Version#Result backup infotimelineid = 1start-lsn = 0/A000028stop-lsn = 0/B0000B8start-time = '2020-09-22 22:49:33+08'end-time = '2020-09-22 22:49:39+08'recovery-xid = 658recovery-time = '2020-09-22 22:49:34+08'data-bytes = 41423956wal-bytes = 16777216uncompressed-bytes = 41389959pgdata-bytes = 41389720status = OKprimary_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
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 informationpgdata = /data/pgsql12/datasystem-identifier = 6870373621203487994xlog-seg-size = 16777216# Connection parameterspgdatabase = postgres# Replica parametersreplica-timeout = 5min# Archive parametersarchive-timeout = 5min# Logging parameterslog-level-console = INFOlog-level-file = OFFlog-filename = pg_probackup.loglog-rotation-size = 0TBlog-rotation-age = 0d# Retention parametersretention-redundancy = 7retention-window = 7wal-depth = 0# Compression parameterscompress-algorithm = nonecompress-level = 1# Remote access parametersremote-proto = ssh
删除过期数据
pg_probackup delete -B ${backup_dir} --instance ${instance_name} --delete-expired--同时删除过期WALpg_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