• 售前

  • 售后

热门帖子
入门百科

PostgreSQL备份工具 pgBackRest使用详解

[复制链接]
123456823 显示全部楼层 发表于 2021-8-14 15:25:22 |阅读模式 打印 上一主题 下一主题
媒介

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

并行备份和还原
备份操纵期间压缩通常是其瓶颈所在。pgBackRest通过并行处置惩罚办理了备份期间压缩出现的瓶颈问题。
本地远程操纵
自界说协议允许 pgBackRest以最小化设置通过SSH在本地或者远程执行备份、还原和归档。而且该步伐也通过协议层提供了PostgreSQL查询接口,以便于必须要再远程访问PostgreSQL,从而包管了其安全性能。
全量,增量和差异备份
支持全量,增量和差异备份。pgBackRest不受异步时间影响,因此差异和增量备份完全安全。
备份保留计谋和和归档逾期
支持保留计谋设置可以在恣意时间创建全备和差异备份的覆盖。
备份完备性
可以计算备份中每个文件的校验和,并在还原期间重新检查。备份完成文件复制后,将等待直到每个WAL段所需的备份保持划一然后存储到备份堆栈中。
块校验和断点备份流压缩和校验和增量还原并行异步WAL Push和Get表空间重新映射和Link支持S3存储支持和Azure兼容对象存储支持加密
使用

安装解压
  1. [postgres@pgserver12 tools]$ tar -zxf pgbackrest-release-2.31.tar.gz
  2. [postgres@pgserver12 tools]$ ls
复制代码
创建须要目录
  1. ostgres@sungsasong ~]$ sudo mkdir -p -m 770 /var/log/pgbackrest
  2. [postgres@sungsasong ~]$ sudo chown postgres.postgres /var/log/pgbackrest/
  3. [postgres@sungsasong ~]$ sudo mkdir -p /etc/pgbackrest
  4. [postgres@sungsasong ~]$ sudo mkdir -p /etc/pgbackrest/conf.d
  5. [postgres@sungsasong ~]$ sudo touch /etc/pgbackrest/pgbackrest.conf
  6. [postgres@sungsasong ~]$ sudo chmod 640 /etc/pgbackrest/pgbackrest.conf
  7. [postgres@sungsasong ~]$ sudo chown postgres.postgres -R /etc/pgbackrest/
  8. [postgres@sungsasong ~]$ sudo mkdir /usr/bin/pgbackrest
  9. [postgres@sungsasong ~]$ sudo chown postgres.postgres /usr/bin/pgbackrest/
  10. [postgres@sungsasong ~]$ sudo chmod 755 /usr/bin/pgbackrest/
复制代码
编译安装
  1. [postgres@sungsasong src]$ ./configure --prefix=/usr/bin/pgbackrest/
  2. [postgres@sungsasong src]$ make -j24
  3. [postgres@sungsasong src]$ make install -j24
  4. install -d /usr/bin/pgbackrest/bin
  5. install -m 755 pgbackrest /usr/bin/pgbackrest/bin
复制代码
命令测试
  1. [postgres@sungsasong src]$ /usr/bin/pgbackrest/bin/pgbackrest
  2. pgBackRest 2.31 - General help
  3. Usage:
  4. pgbackrest [options] [command]
  5. Commands:
  6. archive-get Get a WAL segment from the archive.
  7. archive-push Push a WAL segment to the archive.
  8. backup Backup a database cluster.
  9. check Check the configuration.
  10. expire Expire backups that exceed retention.
  11. help Get help.
  12. info Retrieve information about backups.
  13. restore Restore a database cluster.
  14. stanza-create Create the required stanza data.
  15. stanza-delete Delete a stanza.
  16. stanza-upgrade Upgrade a stanza.
  17. start Allow pgBackRest processes to run.
  18. stop Stop pgBackRest processes from running.
  19. version Get version.
  20. Use 'pgbackrest help [command]' for more information.
复制代码
设置数据库监听和访问及日记(可选)
  1. [postgres@sungsasong pgbackrest-release-2.31]$ egrep "10.10" $PGDATA/pg_hba.conf
  2. host all all 10.10.20.0/24 trust
  3. [postgres@sungsasong pgbackrest-release-2.31]$ egrep -v "^#" $PGDATA/postgresql.auto.conf
  4. logging_collector = 'on'
  5. listen_addresses = '*'
复制代码
设置PostgreSQL数据库数据存储目录
  1. [postgres@sungsasong src]$ cat >>/etc/pgbackrest/pgbackrest.conf <<EOF
  2. > [demo]
  3. > pgl-path=/data/pg10/pgdata
  4. > EOF
复制代码
设置环境变量
  1. [postgres@sungsasong ~]$ echo "export PATH=/usr/bin/pgbackrest/bin:\$PATH" >> .bashrc
复制代码
创建备份和归档堆栈
  1. [postgres@sungsasong ~]$ sudo mkdir -p /pgbackrest/repos
  2. [postgres@sungsasong ~]$ sudo chmod 750 /pgbackrest/ -R
  3. [postgres@sungsasong ~]$ sudo chown postgres.postgres /pgbackrest/ -R
复制代码
将堆栈路径加载在pgBackRest设置文件中
  1. [postgres@sungsasong ~]$ cat /etc/pgbackrest/pgbackrest.conf
  2. [demo]
  3. pgl-path=/data/pg10/pgdata
  4. [global]
  5. repol-path=/pgbackrest/repos
复制代码
设置数据库归档
  1. [postgres@sungsasong ~]$ egrep -v "^#" $PGDATA/postgresql.auto.conf
  2. logging_collector = 'on'
  3. archive_mode = 'on'
  4. archive_command = 'pgbackrest --stanza=demo archive-push %p'
  5. listen_addresses = '*'
  6. log_filename = 'postgresql.log'
  7. log_line_prefix = ''
  8. max_wal_senders = '3'
  9. wal_level = 'replica'
复制代码
重新启动数据库
  1. [postgres@sungsasong ~]$ pg_ctl restart -D $PGDATA -l /tmp/logfile
  2. waiting for server to shut down.... done
  3. server stopped
  4. waiting for server to start.... done
  5. server started
复制代码
可选设置归档压缩
  1. [postgres@sungsasong ~]$ cat /etc/pgbackrest/pgbackrest.conf
  2. [demo]
  3. pgl-path=/data/pg10/pgdata
  4. [global]
  5. repol-path=/pgbackrest/repos [global:archive_push]compress-level=3
复制代码
设置基于保留计谋的归档
  1. [postgres@sungsasong ~]$ cat /etc/pgbackrest/pgbackrest.conf
  2. [demo]
  3. pg1-path=/data/pg10/pgdata
  4. pg1-host-config-path=/data/pg10/pgbackrest
  5. pg1-host-port=22
  6. pg1-host-user=postgres
  7. pg1-host=sungsasong
  8. pg1-port=10001
  9. pg1-user=postgres
  10. [global]
  11. repo1-path=/pgbackrest/repos
  12. repo1-retention-full=2
  13. [global:archive_push]
  14. compress-level=3
复制代码
创建存储空间并检查设置
  1. [postgres@sungsasong ~]$ pgbackrest --stanza=demo --log-level-console=info stanza-create
  2. 2021-01-08 20:42:58.887 P00 INFO: stanza-create command begin 2.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
  3. 2021-01-08 20:42:59.835 P00 INFO: stanza-create command end: completed successfully (948ms)
复制代码
检查设置
  1. [postgres@sungsasong ~]$ pgbackrest --stanza=demo --log-level-console=info check
  2. 2021-01-08 21:41:29.851 P00 INFO: check command begin 2.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
  3. 2021-01-08 21:41:32.826 P00 INFO: WAL segment 000000010000000000000006 successfully archived to '/pgbackrest/repos/archive/demo/10-1/0000000100000000/000000010000000000000006-2f027934f4f35cd3983ca4b1b7b43c32ab089448.gz'
  4. 2021-01-08 21:41:32.826 P00 INFO: check command end: completed successfully (2975ms)
复制代码
执行备份
默认为增量备份,增量备份将会哀求一个基础全备,如果没有基础全备,增量备份将会变更到全备。
  1. pgbackrest --stanza=demo --log-level-console=info backup
  2. 2021-01-08 21:43:55.014 P00 INFO: backup command begin 2.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
  3. WARN: no prior backup exists, incr backup has been changed to full
  4. ...
  5. 此处省略很多输出
  6. ...
  7. 2021-01-08 21:44:00.951 P00 INFO: full backup size = 23.3MB
  8. 2021-01-08 21:44:00.951 P00 INFO: execute non-exclusive pg_stop_backup() and wait for all WAL segments to archive
  9. 2021-01-08 21:44:01.153 P00 INFO: backup stop archive = 000000010000000000000008, lsn = 0/8000130
  10. 2021-01-08 21:44:01.154 P00 INFO: check archive for segment(s) 000000010000000000000008:000000010000000000000008
  11. 2021-01-08 21:44:01.273 P00 INFO: new backup label = 20210108-214355F
  12. 2021-01-08 21:44:01.301 P00 INFO: backup command end: completed successfully (6288ms)
  13. 2021-01-08 21:44:01.301 P00 INFO: expire command begin 2.31: --exec-id=21661-13c54272 --log-level-console=info --repo1-path=/pgbackrest/repos --repo1-retention-full=2 --stanza=demo
  14. 2021-01-08 21:44:01.304 P00 INFO: expire command end: completed successfully (3ms)
复制代码
可以界说备份的范例为增量照旧差异备份
使用—type参数指定
  1. [postgres@sungsasong ~]$ pgbackrest --stanza=demo --log-level-console=info --type=diff backup
  2. 2021-01-08 21:47:00.964 P00 INFO: backup command begin 2.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
  3. 2021-01-08 21:47:01.770 P00 INFO: last backup label = 20210108-214355F, version = 2.31
  4. 2021-01-08 21:47:01.770 P00 INFO: execute non-exclusive pg_start_backup(): backup begins after the next regular checkpoint completes
  5. 2021-01-08 21:47:02.309 P00 INFO: backup start archive = 00000001000000000000000A, lsn = 0/A000028
  6. 2021-01-08 21:47:03.617 P01 INFO: backup file /data/pg10/pgdata/global/pg_control (8KB, 99%) checksum 5f020e7df484269ea245041be3228673560184ef
  7. 2021-01-08 21:47:03.721 P01 INFO: backup file /data/pg10/pgdata/pg_logical/replorigin_checkpoint (8B, 100%) checksum 347fc8f2df71bd4436e38bd1516ccd7ea0d46532
  8. 2021-01-08 21:47:03.722 P00 INFO: diff backup size = 8KB
  9. 2021-01-08 21:47:03.722 P00 INFO: execute non-exclusive pg_stop_backup() and wait for all WAL segments to archive
  10. 2021-01-08 21:47:03.942 P00 INFO: backup stop archive = 00000001000000000000000A, lsn = 0/A0000F8
  11. 2021-01-08 21:47:03.943 P00 INFO: check archive for segment(s) 00000001000000000000000A:00000001000000000000000A
  12. 2021-01-08 21:47:04.062 P00 INFO: new backup label = 20210108-214355F_20210108-214701D
  13. 2021-01-08 21:47:04.092 P00 INFO: backup command end: completed successfully (3129ms)
  14. 2021-01-08 21:47:04.092 P00 INFO: expire command begin 2.31: --exec-id=21671-d3f8f8bf --log-level-console=info --repo1-path=/pgbackrest/repos --repo1-retention-full=2 --stanza=demo
  15. 2021-01-08 21:47:04.095 P00 INFO: expire command end: completed successfully (3ms)
复制代码
备份信息查看
  1. [postgres@sungsasong ~]$ pgbackrest info
  2. stanza: demo
  3. status: ok
  4. cipher: none
  5. db (current)
  6. wal archive min/max (10-1): 000000010000000000000001/00000001000000000000000A
  7. full backup: 20210108-214355F
  8. timestamp start/stop: 2021-01-08 21:43:55 / 2021-01-08 21:44:01
  9. wal start/stop: 000000010000000000000008 / 000000010000000000000008
  10. database size: 23.3MB, backup size: 23.3MB
  11. repository size: 2.7MB, repository backup size: 2.7MB
  12. diff backup: 20210108-214355F_20210108-214701D
  13. timestamp start/stop: 2021-01-08 21:47:01 / 2021-01-08 21:47:03
  14. wal start/stop: 00000001000000000000000A / 00000001000000000000000A
  15. database size: 23.3MB, backup size: 8.2KB
  16. repository size: 2.7MB, repository backup size: 425B
  17. backup reference list: 20210108-214355F
复制代码
还原一个备份
模拟数据库粉碎
  1. [postgres@sungsasong ~]$ cd $PGDATA/
  2. [postgres@sungsasong pgdata]$ rm -rf *
  3. #100分警告:千万不要拿生产库执行
复制代码
执行还原
  1. [postgres@sungsasong pgdata]$ pgbackrest --stanza=demo restore
复制代码
重新启动数据库
  1. [postgres@sungsasong pgdata]$ pg_ctl start -D $PGDATA -l /tmp/logfile
  2. waiting for server to start.... done
  3. server started
  4. [postgres@sungsasong pgdata]$ psql
  5. psql (10.13)
  6. Type "help" for help.
复制代码
以上就是pgbackrest介绍及使用。当前使用的最新版本已经可以支持PostgreSQL13版本,仅仅是文档上标注支持最新到PostgreSQL11版本。感爱好的同学可以下去试一下。
希望能给各人一个参考,也希望各人多多支持脚本之家。如有错误或未思量完全的地方,望不吝赐教。

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作