4.pg_rman介绍及使用

1.pg_rman 介绍

1.1 pg_rman 介绍

• pg_rman类似于oracle的rman备份策略,实现了全量、增量、归档的层级的备份,可以很灵活的管理PostgreSQL数据库的备份,pg_rman是一款开源的pg的备份恢复插件支持在线和基于PITR的备份恢复方式。

• pg_rman特点:
    ○ 使用简单.一个命令即可完成备份和恢复.
    ○ 支持在线全备,增量备份,归档备份.
    ○ 支持备份压缩.通过gzip工具实现页内压缩.
    ○ 自动备份维护.自动删除过期的WAL备份文件.
    ○ 支持备份验证.
    ○ 恢复期间无事务丢失.支持基于PITR的配置文件生成器.


  • pg_rman 注意事项:
    ○  pg_rman 基于 pg_start_backup
    ○  pg_rman 基于 需要在本地安装,不能远程德份
    ○  pg_rman 需要下载对应的版本

1.2 pg_rman 下载

下载地址:
https://github.com/ossc-db/pg_rman/
PostgreSQL Rpm相关依赖、插件包下载地址:
https://yum.postgresql.org/10/redhat/rhel-6.7-x86_64/
手册地址:
http://ossc-db.github.io/pg_rman/index.html

2.安装使用

源码包安装方法:
  1、下载源码包
         wget  <a href="https://github.com/ossc-db/pg_rman/releases/download/V1.3.7/pg_rman-1.3.7-pg10.tar.gz" style="text-decoration-skip-ink:none;">https://github.com/ossc-db/pg_rman/releases/download/V1.3.7/pg_rman-1.3.7-pg10.tar.gz</a>    
   2、解压到指定目录
         tar -zxvf  pg_rman-1.3.14-pg12.tar.gz -C /opt/
   3、进入到rman目录进行编译安装
         cd /opt/pg_rman-1.3.14-pg12/
         make && make install
# 设置环境变量:
 PATH=$PATH:$HOME/bin
export PG_RMAN=/u01/postgresql/pg12
export PGHOME=/u01/postgresql/pg12
export PGDATA=/u01/postgresql/data
export PGLIB=/u01/postgresql/pg12/lib
export SRVLOG_PATH=/u01/postgresql/data/log
export ARCLOG_PATH=/u01/postgresql/archive
export BACKUP_PATH=/opt/pg_rman
export PATH=$PGHOME/bin:$PATH:$PG_RMAN/bin
export PATH

3.pg_rman 使用

3.1 pg_rman的用法

pg_rman的使用相对简单核心8个命令
pg_rman manage backup/recovery of PostgreSQL database.
Usage:
  pg_rman OPTION init
  pg_rman OPTION backup
  pg_rman OPTION restore
  pg_rman OPTION show [DATE]
  pg_rman OPTION show detail [DATE]
  pg_rman OPTION validate [DATE]
  pg_rman OPTION delete DATE
  pg_rman OPTION purge
选项功能

init

初始化备份目录

backup

在线备份

restore

恢复

show

查看备份历史

validate

验证备份

delete

删除备份文件

purge

从备份目录中删除已删除备份

3.2 初始化备份目录

首先需要初始化一个backup catalog,目录将用于存放备份的文件,目录也会存放一些元数据,例如备份的配置文件,数据库的systemid,时间线文件历史   
注意:在初始化的时候,要先确认环境变量是否设置正确。
root@mxl ~]# pg_rman init -B /opt/pg_rman
总用量 8
drwx------ 4 root root 35 11月 23 13:30 backup
-rw-r--r-- 1 root root 83 11月 23 13:30 pg_rman.ini
-rw-r--r-- 1 root root 40 11月 23 13:30 system_identifier
drwx------ 2 root root  6 11月 23 13:30 timeline_history

3.3 查看pg_rman的配置文件信息

[postgres@mxl pg_rman]$ cat pg_rman.ini 

ARCLOG_PATH=‘/home/postgres/arch’                        --归档目录
SRVLOG_PATH=‘/home/postgres/data/log’                   --数据库错误日志目录
COMPRESS_DATA = YES                                   --压缩数据
KEEP_ARCLOG_FILES = 10                                 --保存归档文件个数
KEEP_ARCLOG_DAYS = 10                                  --保存归档的天数
KEEP_DATA_GENERATIONS = 3                               --备份冗余度
KEEP_DATA_DAYS = 10                                   --保存备份集时间
KEEP_SRVLOG_FILES = 10                                 --保存日志文件个数
KEEP_SRVLOG_DAYS = 10                                  --保存日志文件天数

3.4 pg_rman的全量备份

如果没有调设置export BACKUP_PATH=/opt/pg_rman  ,需要手动指明备份路径 --backup-path=/opt/pg_rman
postgres@s2ahumysqlpg01-> pg_rman backup --backup-mode=full --with-serverlog  -h 127.0.0.1
INFO: copying database files
INFO: copying archived WAL files
INFO: copying server log files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.

一、pg_rman 校验备份集:
postgres@s2ahumysqlpg01-> pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status 
=====================================================================
2022-02-22 17:18:39  2022-02-22 17:19:12  FULL   942MB     5  DONE

二、未校验备份集 Status 显示为 DONE,开始校验
[postgres@mxl pg_rman]$ pg_rman validate
INFO: validate: "2019-12-03 11:53:39" backup, archive log files and server log files by CRC
INFO: backup "2019-12-03 11:53:39" is valid
[postgres@mxl pg_rman]$ pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status 
=====================================================================
2019-12-03 11:53:39  2018-12-03 11:54:00  FULL   461MB     1  OK

一、pg_rman 备份集查看:
[postgres@mxl pg_rman]$ pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status 
=====================================================================
2019-12-03 11:53:39  2019-12-03 11:54:00  FULL   461MB     1  DONE

二、未校验备份集 Status 显示为 DONE,开始校验
postgres@s2ahumysqlpg01-> pg_rman validate
INFO: validate: "2022-02-22 17:18:39" backup, archive log files and server log files by CRC
INFO: backup "2022-02-22 17:18:39" is valid


postgres@s2ahumysqlpg01->  pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status 
=====================================================================
2022-02-22 17:18:39  2022-02-22 17:19:12  FULL   942MB     5  OK

3.5 pg_rman的增量备份

增量备份是基于文件系统的update time时间线,增量备份必须有个对应的全库备份
postgres@s2ahumysqlpg01-> pg_rman backup --backup-mode=incremental --progress --compress-data  -h 127.0.0.1
INFO: copying database files
INFO: copying archived WAL filesipped 3028
Processed 16 of 16 files, skipped 13
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.

备份集查看:
$ pg_rman show
    postgres@s2ahumysqlpg01-> pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status 
=====================================================================
2022-02-22 17:22:54  2022-02-22 17:22:58  INCR    33kB     5  DONE
2022-02-22 17:18:39  2022-02-22 17:19:12  FULL   942MB     5  OK


postgres@s2ahumysqlpg01-> pg_rman validate
INFO: validate: "2022-02-22 17:22:54" backup and archive log files by CRC
INFO: backup "2022-02-22 17:22:54" is valid
postgres@s2ahumysqlpg01-> 
postgres@s2ahumysqlpg01-> 
postgres@s2ahumysqlpg01-> pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status 
=====================================================================
2022-02-22 17:22:54  2022-02-22 17:22:58  INCR    33kB     5  OK
2022-02-22 17:18:39  2022-02-22 17:19:12  FULL   942MB     5  OK
postgres@s2ahumysqlpg01-> 

3.6 pg_rman 归档备份

一、pg_rman 归档备份:
归档备份就是静态文件的拷贝。
$ pg_rman backup --backup-mode=archive --progress --compress-data 
$ pg_rman validate
$ pg_rman show

二、删除备份集
删除备份集指定日期之前的,不需要用来指定时间点之前的备份删除掉
[postgres@mxl ~]$ pg_rman delete " pg_rman delete "2022-02-22 17:22:58""

三、清除备份集
物理删除已从catalog删除的备份集
上面从备份集中删除的备份,备份集文件夹并没有一起删除。
[postgres@mxl ~]$ pg_rman purge

4 pg_rman 恢复

pg_rman 恢复默认原地恢复(覆盖式),或者使用新的$PGDATA作为恢复目标。需注意如果在本机恢复pg_rman可能会覆盖原有的数据文件arch,pg_xlog目录中的文件, 可以先将原数据文件做下备份,默认pg_rman restore会在$PGDATA下产生recovery.conf根据实际需要修改相关恢复参数即可。 

1、recovery-target-timeline TIMELINE  
Specifies recovering into a particular timeline. If not specified, the current timeline from ($PGDATA/global/pg_control) is used. 
如果不指定时间线,则使用$PGDATA/global/pg_control,如果没有$PGDATA/global/pg_control,则使用最新的全量备份集的时间线。 

2、recovery-target-time TIMESTAMP  
This parameter specifies the time stamp up to which recovery will proceed. If not specified, continue recovery to the latest time.
如果不指定,则恢复到最新时间  

3、recovery-target-xid XID  
This parameter specifies the transaction ID up to which recovery will proceed. If not specified, continue recovery to the latest xid.
如果不指定,则恢复到最新xid

4、recovery-target-inclusive
    Specifies whether we stop just after the specified recovery target (true), or just before the recovery target (false). Default is true. 
    是否在指定的恢复目标(true)之后停止,默认为true,如果指定false意识是在恢复目标之前停止

• The following parameter determines the behavior of restore.  
    --hard-copy  
    The archive WAL are copied to archive WAL storage area. If not specified, pg_rman makes symbolic link to archive WAL where are in the backup catalog directory. 
    是否使用硬链接复制archive log,如果不指定使用符号连接(软连接)的方式。
#示例:  
    #pg_rman restore  --recovery-target-time "2022-02-22 17:25:30";  下面是没有指定的恢复
    [postgres@mxl~]$ pg_rman restore 
    INFO: backup "2022-02-22 17:25:30" is valid
    INFO: the recovery target timeline ID is not given
    INFO: use timeline ID of current database cluster as recovery target: 5
    INFO: calculating timeline branches to be used to recovery target point
    INFO: searching latest full backup which can be used as restore start point
    INFO: found the full backup can be used as base in recovery: "2022-02-22 17:25:30"
    INFO: copying online WAL files and server log files
    INFO: clearing restore destination
    INFO: validate: "2022-02-22 17:25:30" backup, archive log files and server log files by SIZE
    INFO: backup "2022-02-22 17:25:30" is valid
    INFO: restoring database files from the full mode backup "2022-02-22 17:25:30"
    INFO: searching incremental backup to be restored
    INFO: searching backup which contained archived WAL files to be restored
    INFO: backup "2022-02-22 17:25:30" is valid
    INFO: restoring WAL files from backup "2022-02-22 17:25:30"
    INFO: restoring online WAL files and server log files
    INFO: create pg_rman_recovery.conf for recovery-related parameters.
    INFO: remove an 'include' directive added by pg_rman in postgresql.conf if exists
    INFO: append an 'include' directive in postgresql.conf for pg_rman_recovery.conf
    INFO: generating recovery.signal
    INFO: removing standby.signal if exists to restore as primary
    INFO: restore complete
    HINT: Recovery will start automatically when the PostgreSQL server is started. After the recovery is done, we recommend to remove        recovery-related parameters configured by pg_rman.

    pg_ctl start 

参考:

下载:https://github.com/ossc-db/pg_rman/
依赖包:https://yum.postgresql.org/10/redhat/rhel-6.7-x86_64/
使用手册:http://ossc-db.github.io/pg_rman/index.html





posted @ 2022-02-22 18:11  www.cqdba.cn  阅读(1332)  评论(0编辑  收藏  举报