PG_RMAN使用手册

Wesley13
• 阅读 986

众所周知,Oracle下可以使用rman进行物理备份,支持数据库的全量、增量、归档的备份模式;而PostgreSQL作为开源数据库,也推出了开源功工具pg_rman,同样具备了oracle rman大部分的功能。本文写的很早,近期才翻出来,挂到博客上,希望能帮助需要的朋友。

1、下载安装软件

[root@mydb01 ~]# wget https://github.com/ossc-db/pg_rman/releases/download/V1.3.6/pg_rman-1.3.6-1.pg10.rhel7.x86_64.rpm
[root@mydb01 ~]# yum -y localinstall pg_rman-1.3.6-1.pg10.rhel7.x86_64.rpm

2、配置PG参数

首先设置postgres用户的profile,加入以下内容:

-bash-4.2$ vi .bash_profile
export PATH=/usr/pgsql-10/bin:$PATH
export LD_LIBRARY_PATH=/usr/pgsql-10/lib
export BACKUP_PATH=/u02/backups
-bash-4.2$ vi pg_hba.conf
host    all             all             0.0.0.0/0               md5
-bash-4.2$ vi postgresql.conf
listen_addresses = '*'  
port = 5432  
wal_level = archive ------->>若为HOT STANDBY环境则此处设置为hot_standby  
archive_mode = on  
archive_command = 'test ! -f /u02/archivelog/%f && cp %p /u02/archivelog/%f'
[root@mydb01 ~]# systemctl restart postgresql10.service

3、初始化备份环境

[root@mydb01 ~]# su - postgres
-bash-4.2$ pg_rman init

4、全备份

-bash-4.2$ pg_rman backup --backup-mode=full
INFO: copying database files
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10, keep days = 10)
INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00"
INFO: start deleting old backup (keep generations = 3 AND keep after = 2017-11-28 00:00:00)
INFO: does not include the backup just tak

备份完成后,可以使用下面的命令进行验证:

-bash-4.2$ pg_rman validate
INFO: validate: "2018-03-28 16:21:34" backup and archive log files by CRC
INFO: backup "2018-03-28 16:21:34" is valid
-bash-4.2$ pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status 
=====================================================================
2018-03-28 16:21:34  2018-03-28 16:21:36  FULL  4465kB     1  OK

5、增量备份

-bash-4.2$ pg_rman backup --backup-mode=incremental --with-serverlog
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.
INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10, keep days = 10)
INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00"
INFO: start deleting old server files from SRVLOG_PATH (keep files = 10, keep days = 10)
INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00"
INFO: start deleting old backup (keep generations = 3 AND keep after = 2017-11-28 00:00:00)
INFO: does not include the backup just taken
INFO: backup "2018-03-28 16:21:34" should be kept
DETAIL: This is the 1st latest full backup.
-bash-4.2$ pg_rman validate
INFO: validate: "2018-03-28 16:27:57" backup, archive log files and server log files by CRC
INFO: backup "2018-03-28 16:27:57" is valid
-bash-4.2$ pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status 
=====================================================================
2018-03-28 16:27:57  2018-03-28 16:27:59  INCR   992kB     1  OK
2018-03-28 16:21:34  2018-03-28 16:21:36  FULL  4465kB     1  OK

6、归档备份

-bash-4.2$ pg_rman backup --backup-mode=archive --with-serverlog
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.
INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10, keep days = 10)
INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00"
INFO: start deleting old server files from SRVLOG_PATH (keep files = 10, keep days = 10)
INFO: the threshold timestamp calculated by keep days is "2018-03-18 00:00:00"
INFO: start deleting old backup (keep generations = 3 AND keep after = 2017-11-28 00:00:00)
INFO: does not include the backup just taken
INFO: backup "2018-03-28 16:27:57" should be kept
DETAIL: This belongs to the 1st latest full backup.
INFO: backup "2018-03-28 16:21:34" should be kept
DETAIL: This is the 1st latest full backup.
-bash-4.2$ pg_rman show
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status 
=====================================================================
2018-03-28 16:28:51  2018-03-28 16:28:52  ARCH    26kB     1  DONE
2018-03-28 16:27:57  2018-03-28 16:27:59  INCR   992kB     1  OK
2018-03-28 16:21:34  2018-03-28 16:21:36  FULL  4465kB     1  OK

7、查看备份集信息

-bash-4.2$ pg_rman show 2018-03-28 16:21:34
# configuration
BACKUP_MODE=FULL
FULL_BACKUP_ON_ERROR=false
WITH_SERVERLOG=false
COMPRESS_DATA=true
# result
TIMELINEID=1
START_LSN=0/02000028
STOP_LSN=0/02000130
START_TIME='2018-03-28 16:21:34'
END_TIME='2018-03-28 16:21:36'
RECOVERY_XID=555
RECOVERY_TIME='2018-03-28 16:21:36'
TOTAL_DATA_BYTES=24535637
READ_DATA_BYTES=24535483
READ_ARCLOG_BYTES=33554741
WRITE_BYTES=4465406
BLOCK_SIZE=8192
XLOG_BLOCK_SIZE=8192
STATUS=OK
-bash-4.2$ pg_rman show 2018-03-28 16:27:57
# configuration
BACKUP_MODE=INCREMENTAL
FULL_BACKUP_ON_ERROR=false
WITH_SERVERLOG=true
COMPRESS_DATA=true
# result
TIMELINEID=1
START_LSN=0/04000028
STOP_LSN=0/040000f8
START_TIME='2018-03-28 16:27:57'
END_TIME='2018-03-28 16:27:59'
RECOVERY_XID=561
RECOVERY_TIME='2018-03-28 16:27:59'
TOTAL_DATA_BYTES=32585789
READ_DATA_BYTES=8186187
READ_ARCLOG_BYTES=33554741
READ_SRVLOG_BYTES=518
WRITE_BYTES=992901
BLOCK_SIZE=8192
XLOG_BLOCK_SIZE=8192
STATUS=OK

8、删除备份

-bash-4.2$ pg_rman delete "2018-03-28 16:28:51"
WARNING: cannot delete backup with start time "2018-03-28 16:28:51"
DETAIL: This is the archive backup necessary for successful recovery.
WARNING: cannot delete backup with start time "2018-03-28 16:27:57"
DETAIL: This is the incremental backup necessary for successful recovery.
WARNING: cannot delete backup with start time "2018-03-28 16:21:34"
DETAIL: This is the latest full backup necessary for successful recovery.

9、恢复数据库

删除/var/lib/pgsql/10/data文件夹或者清空,然后进行恢复操作。如果data文件夹是新建的,则必须注意data目录的属性为700,否则在恢复的时候会报错。

-bash-4.2$ pg_rman restore --recovery-target-time "2018-03-29 14:35:53"
WARNING: pg_controldata file "/var/lib/pgsql/10/data/global/pg_control" does not exist
INFO: backup "2018-03-29 14:30:11" is valid
INFO: the recovery target timeline ID is not given
INFO: use timeline ID of latest full backup as recovery target: 1
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: "2018-03-29 14:30:11"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2018-03-29 14:30:11" backup and archive log files by SIZE
INFO: backup "2018-03-29 14:30:11" is valid
INFO: restoring database files from the full mode backup "2018-03-29 14:30:11"
INFO: searching incremental backup to be restored
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2018-03-29 14:30:11" is valid
INFO: restoring WAL files from backup "2018-03-29 14:30:11"
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.
-bash-4.2$ pg_ctl start
waiting for server to start....2018-03-29 14:43:47.613 CST [1465] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2018-03-29 14:43:47.613 CST [1465] LOG:  listening on IPv6 address "::", port 5432
2018-03-29 14:43:47.619 CST [1465] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2018-03-29 14:43:47.637 CST [1465] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2018-03-29 14:43:47.650 CST [1465] LOG:  redirecting log output to logging collector process
2018-03-29 14:43:47.650 CST [1465] HINT:  Future log output will appear in directory "log".
. done
server started

更详细的pg_rman用法,请参考官方手册。

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Easter79 Easter79
3年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这