POSTGRESQL9.5之pg_rman工具

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POSTGRESQL9.5之pg_rman工具相关的知识,希望对你有一定的参考价值。

pg_rman是一款专门为postgresql设计的在线备份恢复的工具。其支持在线和基于时间点备份方式,还可以通过创建backup catalog来维护DB cluster备份信息。

技术分享

看起来好像是模仿oracle的RMAN工具。

 pg_rman特点:

  • 使用简单.一个命令即可完成备份和恢复.
  •  支持在线全备,增量备份,归档备份.
  • 支持备份压缩.通过gzip工具实现页内压缩.
  • 自动备份维护.自动删除过期的WAL备份文件.
  • 支持备份验证.
  • 恢复期间无事务丢失.支持基于PITR的配置文件生成器.
1. 安装过程
   包:postgresql95-libs-9.5.2-1PGDG.rhel6.x86_64.rpm
--下载pg_rman,注意与其对应的数据库版本 下载地址:https://github.com/ossc-db/pg_rman/releases

  包:pg_rman-1.3.2-1.pg95.rhel6.x86_64.rpm

 

[[email protected]_210 software]# rpm -ivh postgresql95-libs-9.5.2-1PGDG.rhel6.x86_64.rpm
warning: postgresql95-libs-9.5.2-1PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing...                ########################################### [100%]
   1:postgresql95-libs      ########################################### [100%]
[[email protected]_210 software]# rpm -ivh pg_rman-1.3.2-1.pg95.rhel6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:pg_rman                ########################################### [100%]

 

2. 配置环境变量

  pg_rman 默认是安装在/usr目录下面;注意同时pg_rman需要一个备份目录
export PG_RMAN=/usr/pgsql-9.5
export PATH=$PATH:$HOME/bin:/opt/pgsql95/bin:$PG_RMAN/bin
export BACKUP_PATH=/data/pg_rman      
                                     

3.初始化备份目录

[[email protected]_210 ~]$ pg_rman init -B /data/pg_rman
INFO: ARCLOG_PATH is set to /home/postgres/archive
INFO: SRVLOG_PATH is set to /data/pgdata/pg_log

 

4.备份
[[email protected]_210 ~]$ pg_rman backup --backup-mode=full --progress
INFO: copying database files
Processed 1153 of 1153 files, skipped 0
NOTICE:  pg_stop_backup complete, all required WAL segments have been archived
INFO: copying archived WAL files
Processed 26 of 26 files, skipped 0
INFO: backup complete
HINT: Please execute pg_rman validate to verify the files are correctly copied.

--pg_rman 的备份必须都是经过验证过的,否则不能进行恢复和增量备份

 Backups without validation cannot be used forrestoreand incremental backup--执行完备份要执行一次validate,因为备份后的状态是done,还不能进行恢复
[[email protected]_210 ~]$ pg_rman show

==========================================================
 StartTime           Mode  Duration    Size   TLI  Status 
==========================================================
2016-05-31 16:36:45  FULL        0m   343MB     3  DONE
[[email protected]_210 ~]$ pg_rman validate 
INFO: validate: "2016-05-31 16:36:45" backup and archive log files by CRC
INFO: backup "2016-05-31 16:36:45" is valid
[[email protected]_210 ~]$ pg_rman show
==========================================================
 StartTime           Mode  Duration    Size   TLI  Status 
==========================================================
2016-05-31 16:36:45  FULL        0m   343MB     3  OK

 

5. 模拟恢复
[[email protected]_210 ~]$ ll
total 32
drwxrwxr-x 2 postgres postgres 4096 May 31 17:44 archive
drwxrwxr-x 2 postgres postgres 4096 May 30 16:16 backup
drwx------ 3 postgres postgres 4096 May 31 17:43 data
drwxrwxr-x 2 postgres postgres 4096 May 30 16:09 log
-rw-rw-r-- 1 postgres postgres   82 May 16 14:03 out_file
-rw-rw-r-- 1 postgres postgres  128 May 16 13:52 out.txt
drwxrwxr-x 2 postgres postgres 4096 May 30 16:16 scripts
-rw-r--r-- 1 postgres postgres   20 May 16 15:39 test.sql
[[email protected]_210 ~]$ rm -rf data
[[email protected]_210 ~]$ ll
total 28
drwxrwxr-x 2 postgres postgres 4096 May 31 17:44 archive
drwxrwxr-x 2 postgres postgres 4096 May 30 16:16 backup
drwxrwxr-x 2 postgres postgres 4096 May 30 16:09 log
-rw-rw-r-- 1 postgres postgres   82 May 16 14:03 out_file
-rw-rw-r-- 1 postgres postgres  128 May 16 13:52 out.txt
drwxrwxr-x 2 postgres postgres 4096 May 30 16:16 scripts
-rw-r--r-- 1 postgres postgres   20 May 16 15:39 test.sql
[[email protected]_210 ~]$ pg_stop
waiting for server to shut down.... done
server stopped
[[email protected]_210 ~]$ pg_rman restore --recovery-target-time "2016-05-31 17:30:00"
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: "2016-05-31 16:36:45"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2016-05-31 16:36:45" backup and archive log files by SIZE
INFO: backup "2016-05-31 16:36:45" is valid
INFO: restoring database files from the full mode backup "2016-05-31 16:36:45"
INFO: searching incremental backup to be restored
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2016-05-31 16:36:45" is valid
INFO: restoring WAL files from backup "2016-05-31 16:36:45"
INFO: restoring online WAL files and server log files
INFO: generating recovery.conf
INFO: restore complete
HINT: Recovery will start automatically when the PostgreSQL server is started.
[[email protected]_210 ~]$ pg_start
server starting
[[email protected]_210 ~]$ psql mydb lottu
psql (9.5.0)
Type "help" for help.
 
mydb=> \d
       List of relations
 Schema | Name | Type  | Owner 
--------+------+-------+-------
 public | test | table | lottu
(1 row)
 
mydb=> select * from test;
  id  |  name  
------+--------
 1001 | lottu
 1002 | rax
 1003 | xuan
 1004 | li0924
(4 rows)
 
mydb=> \q
[[email protected]_210 ~]$ ll
total 32
drwxrwxr-x 2 postgres postgres 4096 May 31 17:46 archive
drwxrwxr-x 2 postgres postgres 4096 May 30 16:16 backup
drwx------ 3 postgres postgres 4096 May 31 17:46 data
drwxrwxr-x 2 postgres postgres 4096 May 30 16:09 log
-rw-rw-r-- 1 postgres postgres   82 May 16 14:03 out_file
-rw-rw-r-- 1 postgres postgres  128 May 16 13:52 out.txt
drwxrwxr-x 2 postgres postgres 4096 May 30 16:16 scripts
-rw-r--r-- 1 postgres postgres   20 May 16 15:39 test.sql

【参考文献】

 

以上是关于POSTGRESQL9.5之pg_rman工具的主要内容,如果未能解决你的问题,请参考以下文章

pg_rman用法

PostgreSQL备份恢复-pg_rman

pg_rman的安装与使用

pg_rman备份恢复测试

PG_RMAN备份遇到 domain socket

PG_RMAN使用手册