您现在的位置是:首页 > 博客网站制作 > 心得笔记心得笔记

PostgreSQL备份工具 pgBackRest使用详解

杨青青2022-02-20【心得笔记】人已围观

简介这篇文章主要介绍了PostgreSQL备份工具pgBackRest使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。前言pgBackRest是一款开源的备份还原工具,目标旨

这篇文章主要介绍了PostgreSQL备份工具pgBackRest使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。

前言

pgBackRest是一款开源的备份还原工具,目标旨在为备份和还原提供可靠易用的备份。

特性

并行备份和还原

备份操作期间压缩通常是其瓶颈所在。pgBackRest通过并行处理解决了备份期间压缩出现的瓶颈问题。

本地远程操作

自定义协议允许pgBackRest以最小化配置通过SSH在本地或者远程执行备份、还原和归档。并且该程序也通过协议层提供了PostgreSQL查询接口,以便于必须要再远程访问PostgreSQL,从而保证了其安全性能。

全量,增量和差异备份

支持全量,增量和差异备份。pgBackRest不受异步时间影响,因此差异和增量备份完全安全。

备份保留策略和和归档过期

支持保留策略设置可以在任意时间创建全备和差异备份的覆盖。

备份完整性

可以计算备份中每个文件的校验和,并在还原期间重新检查。备份完成文件复制后,将等待直到每个WAL段所需的备份保持一致然后存储到备份仓库中。

块校验和断点备份流压缩和校验和增量还原并行异步WALPush和Get表空间重新映射和Link支持S3存储支持和Azure兼容对象存储支持加密

使用

安装解压

[postgres@pgserver12tools]$tar-zxfpgbackrest-release-2.31.tar.gz[postgres@pgserver12tools]$ls

创建必要目录

ostgres@sungsasong~]$sudomkdir-p-m770/var/log/pgbackrest
[postgres@sungsasong~]$sudochownpostgres.postgres/var/log/pgbackrest/
[postgres@sungsasong~]$sudomkdir-p/etc/pgbackrest
[postgres@sungsasong~]$sudomkdir-p/etc/pgbackrest/conf.d
[postgres@sungsasong~]$sudotouch/etc/pgbackrest/pgbackrest.conf
[postgres@sungsasong~]$sudochmod640/etc/pgbackrest/pgbackrest.conf
[postgres@sungsasong~]$sudochownpostgres.postgres-R/etc/pgbackrest/
[postgres@sungsasong~]$sudomkdir/usr/bin/pgbackrest
[postgres@sungsasong~]$sudochownpostgres.postgres/usr/bin/pgbackrest/
[postgres@sungsasong~]$sudochmod755/usr/bin/pgbackrest/

编译安装

[postgres@sungsasongsrc]$./configure--prefix=/usr/bin/pgbackrest/
[postgres@sungsasongsrc]$make-j24
[postgres@sungsasongsrc]$makeinstall-j24
install-d/usr/bin/pgbackrest/bin
install-m755pgbackrest/usr/bin/pgbackrest/bin

命令测试

[postgres@sungsasongsrc]$/usr/bin/pgbackrest/bin/pgbackrest
pgBackRest2.31-Generalhelp

Usage:
pgbackrest[options][command]

Commands:
archive-getGetaWALsegmentfromthearchive.
archive-pushPushaWALsegmenttothearchive.
backupBackupadatabasecluster.
checkChecktheconfiguration.
expireExpirebackupsthatexceedretention.
helpGethelp.
infoRetrieveinformationaboutbackups.
restoreRestoreadatabasecluster.
stanza-createCreatetherequiredstanzadata.
stanza-deleteDeleteastanza.
stanza-upgradeUpgradeastanza.
startAllowpgBackRestprocessestorun.
stopStoppgBackRestprocessesfromrunning.
versionGetversion.

Use'pgbackresthelp[command]'formoreinformation.

配置数据库监听和访问及日志(可选)

[postgres@sungsasongpgbackrest-release-2.31]$egrep"10.10"$PGDATA/pg_hba.conf
hostallall10.10.20.0/24trust
[postgres@sungsasongpgbackrest-release-2.31]$egrep-v"^#"$PGDATA/postgresql.auto.conf
logging_collector='on'
listen_addresses='*'

配置PostgreSQL数据库数据存储目录

[postgres@sungsasongsrc]$cat>>/etc/pgbackrest/pgbackrest.conf<>[demo]
>pgl-path=/data/pg10/pgdata
>EOF

配置环境变量

1[postgres@sungsasong~]$echo"exportPATH=/usr/bin/pgbackrest/bin:\$PATH">>.bashrc

创建备份和归档仓库

[postgres@sungsasong~]$sudomkdir-p/pgbackrest/repos
[postgres@sungsasong~]$sudochmod750/pgbackrest/-R
[postgres@sungsasong~]$sudochownpostgres.postgres/pgbackrest/-R

将仓库路径加载在pgBackRest配置文件中

[postgres@sungsasong~]$cat/etc/pgbackrest/pgbackrest.conf
[demo]
pgl-path=/data/pg10/pgdata

[global]
repol-path=/pgbackrest/repos

配置数据库归档

[postgres@sungsasong~]$egrep-v"^#"$PGDATA/postgresql.auto.conf
logging_collector='on'
archive_mode='on'
archive_command='pgbackrest--stanza=demoarchive-push%p'
listen_addresses='*'
log_filename='postgresql.log'
log_line_prefix=''
max_wal_senders='3'
wal_level='replica'

重新启动数据库

[postgres@sungsasong~]$pg_ctlrestart-D$PGDATA-l/tmp/logfile
waitingforservertoshutdown....done
serverstopped
waitingforservertostart....done
serverstarted

可选配置归档压缩

[postgres@sungsasong~]$cat/etc/pgbackrest/pgbackrest.conf
[demo]
pgl-path=/data/pg10/pgdata

[global]
repol-path=/pgbackrest/repos

[global:archive_push]
compress-level=3

配置基于保留策略的归档

[postgres@sungsasong~]$cat/etc/pgbackrest/pgbackrest.conf
[demo]
pg1-path=/data/pg10/pgdata
pg1-host-config-path=/data/pg10/pgbackrest
pg1-host-port=22
pg1-host-user=postgres
pg1-host=sungsasong
pg1-port=10001
pg1-user=postgres

[global]
repo1-path=/pgbackrest/repos
repo1-retention-full=2

[global:archive_push]
compress-level=3

创建存储空间并检查配置

[postgres@sungsasong~]$pgbackrest--stanza=demo--log-level-console=infostanza-create
2021-01-0820:42:58.887P00INFO:stanza-createcommandbegin2.31:--exec-id=9283-e9744c3e--log-level-console=info--pg1-host=sungsasong--pg1-host-config-path=/data/pg10/pgbackrest--pg1-host-port=22--pg1-host-user=postgres--pg1-path=/data/pg10/pgdata--pg1-port=10001--pg1-user=postgres--repo1-path=/pgbackrest/repos--stanza=demo
2021-01-0820:42:59.835P00INFO:stanza-createcommandend:completedsuccessfully(948ms)

检查配置

[postgres@sungsasong~]$pgbackrest--stanza=demo--log-level-console=infocheck
2021-01-0821:41:29.851P00INFO:checkcommandbegin2.31:--exec-id=21648-1862ac0d--log-level-console=info--pg1-path=/data/pg10/pgdata--pg1-port=10001--pg1-user=postgres--repo1-path=/pgbackrest/repos--stanza=demo
2021-01-0821:41:32.826P00INFO:WALsegment000000010000000000000006successfullyarchivedto'/pgbackrest/repos/archive/demo/10-1/0000000100000000/000000010000000000000006-2f027934f4f35cd3983ca4b1b7b43c32ab089448.gz'
2021-01-0821:41:32.826P00INFO:checkcommandend:completedsuccessfully(2975ms)

执行备份

默认为增量备份,增量备份将会请求一个基础全备,如果没有基础全备,增量备份将会变更到全备。

pgbackrest--stanza=demo--log-level-console=infobackup
2021-01-0821:43:55.014P00INFO:backupcommandbegin2.31:--exec-id=21661-13c54272--log-level-console=info--pg1-path=/data/pg10/pgdata--pg1-port=10001--pg1-user=postgres--repo1-path=/pgbackrest/repos--repo1-retention-full=2--stanza=demo
WARN:nopriorbackupexists,incrbackuphasbeenchangedtofull
...
此处省略很多输出
...
2021-01-0821:44:00.951P00INFO:fullbackupsize=23.3MB
2021-01-0821:44:00.951P00INFO:executenon-exclusivepg_stop_backup()andwaitforallWALsegmentstoarchive
2021-01-0821:44:01.153P00INFO:backupstoparchive=000000010000000000000008,lsn=0/8000130
2021-01-0821:44:01.154P00INFO:checkarchiveforsegment(s)000000010000000000000008:000000010000000000000008
2021-01-0821:44:01.273P00INFO:newbackuplabel=20210108-214355F
2021-01-0821:44:01.301P00INFO:backupcommandend:completedsuccessfully(6288ms)
2021-01-0821:44:01.301P00INFO:expirecommandbegin2.31:--exec-id=21661-13c54272--log-level-console=info--repo1-path=/pgbackrest/repos--repo1-retention-full=2--stanza=demo
2021-01-0821:44:01.304P00INFO:expirecommandend:completedsuccessfully(3ms)

可以定义备份的类型为增量还是差异备份

使用—type参数指定

[postgres@sungsasong~]$pgbackrest--stanza=demo--log-level-console=info--type=diffbackup
2021-01-0821:47:00.964P00INFO:backupcommandbegin2.31:--exec-id=21671-d3f8f8bf--log-level-console=info--pg1-path=/data/pg10/pgdata--pg1-port=10001--pg1-user=postgres--repo1-path=/pgbackrest/repos--repo1-retention-full=2--stanza=demo--type=diff
2021-01-0821:47:01.770P00INFO:lastbackuplabel=20210108-214355F,version=2.31
2021-01-0821:47:01.770P00INFO:executenon-exclusivepg_start_backup():backupbeginsafterthenextregularcheckpointcompletes
2021-01-0821:47:02.309P00INFO:backupstartarchive=00000001000000000000000A,lsn=0/A000028
2021-01-0821:47:03.617P01INFO:backupfile/data/pg10/pgdata/global/pg_control(8KB,99%)checksum5f020e7df484269ea245041be3228673560184ef
2021-01-0821:47:03.721P01INFO:backupfile/data/pg10/pgdata/pg_logical/replorigin_checkpoint(8B,100%)checksum347fc8f2df71bd4436e38bd1516ccd7ea0d46532
2021-01-0821:47:03.722P00INFO:diffbackupsize=8KB
2021-01-0821:47:03.722P00INFO:executenon-exclusivepg_stop_backup()andwaitforallWALsegmentstoarchive
2021-01-0821:47:03.942P00INFO:backupstoparchive=00000001000000000000000A,lsn=0/A0000F8
2021-01-0821:47:03.943P00INFO:checkarchiveforsegment(s)00000001000000000000000A:00000001000000000000000A
2021-01-0821:47:04.062P00INFO:newbackuplabel=20210108-214355F_20210108-214701D
2021-01-0821:47:04.092P00INFO:backupcommandend:completedsuccessfully(3129ms)
2021-01-0821:47:04.092P00INFO:expirecommandbegin2.31:--exec-id=21671-d3f8f8bf--log-level-console=info--repo1-path=/pgbackrest/repos--repo1-retention-full=2--stanza=demo
2021-01-0821:47:04.095P00INFO:expirecommandend:completedsuccessfully(3ms)

备份信息查看

[postgres@sungsasong~]$pgbackrestinfo
stanza:demo
status:ok
cipher:none

db(current)
walarchivemin/max(10-1):000000010000000000000001/00000001000000000000000A

fullbackup:20210108-214355F
timestampstart/stop:2021-01-0821:43:55/2021-01-0821:44:01
walstart/stop:000000010000000000000008/000000010000000000000008
databasesize:23.3MB,backupsize:23.3MB
repositorysize:2.7MB,repositorybackupsize:2.7MB

diffbackup:20210108-214355F_20210108-214701D
timestampstart/stop:2021-01-0821:47:01/2021-01-0821:47:03
walstart/stop:00000001000000000000000A/00000001000000000000000A
databasesize:23.3MB,backupsize:8.2KB
repositorysize:2.7MB,repositorybackupsize:425B
backupreferencelist:20210108-214355F

还原一个备份

模拟数据库损坏

[postgres@sungsasong~]$cd$PGDATA/
[postgres@sungsasongpgdata]$rm-rf*
#100分警告:千万不要拿生产库执行

执行还原

1[postgres@sungsasongpgdata]$pgbackrest--stanza=demorestore

重新启动数据库

[postgres@sungsasongpgdata]$pg_ctlstart-D$PGDATA-l/tmp/logfile
waitingforservertostart....done
serverstarted
[postgres@sungsasongpgdata]$psql
psql(10.13)
Type"help"forhelp.

以上就是pgbackrest介绍及使用。当前使用的最新版本已经可以支持PostgreSQL13版本,仅仅是文档上标注支持最新到PostgreSQL11版本。感兴趣的同学可以下去试一下。

Tags:

很赞哦! ()

文章评论

站点信息

  • 建站时间:2011年01月12日
  • 网站程序:帝国CMS7.5
  • 主题模板《今夕何夕》
  • 文章统计625篇文章
  • 标签管理标签云
  • 微信公众号:扫描二维码,关注我们