Postgresql 备份与恢复工具 pg_rman

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:

--下载软件
https://github.com/ossc-db/pg_rman

--安装
 unzip pg_rman-master.zip 
 
--下载安装postgresql93-libs
http://yum.postgresql.org/9.3/redhat/rhel-6.4-x86_64/ 

[root@rudy tools]# rpm -ivh postgresql93-libs-9.3.10-1PGDG.rhel6.x86_64.rpm 
warning: postgresql93-libs-9.3.10-1PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing...                ########################################### [100%]
   1:postgresql93-libs      ########################################### [100%]

--下载pg_rman,注意与其对应的数据库版本   
 https://github.com/ossc-db/pg_rman/releases  
[root@rudy tools]# rpm -ivh pg_rman-1.3.1-1.pg93.rhel6.x86_64.rpm    
Preparing...                ########################################### [100%]
   1:pg_rman                ########################################### [100%]
   
   
--注意默认pg_rman安装在 /usr/pgsql-9.3 目录下   

--注意pg_rman需要一个备份目录
export BACKUP_PATH=/tmp/pg_rman


--初始化备份目录
mkdir pg_rman
pg_rman init -B /tmp/pg_rman

--查看备份集
 pg_rman show
 
 --删除备份
  pg_rman delete 2015-11-19 15:10:34
  
  
--pg_rman 支持增量备份和压缩备份

 
 --pg_rman必须要有一个全量的备份做增量
 pg_rman backup --backup-mode=full --progress
 
 --基于全库备份的增量备份
  pg_rman backup -b incremental -P -Z 
  
  
  
 --pg_rman 的备份必须都是经过验证过的,否则不能进行恢复和增量备份
 Backups without validation cannot be used for restore and incremental backup  
 --执行完备份要执行一次validate,因为备份后的状态是done,还不能进行恢复
 pg_rman validate
 
 $ pg_rman show detail
==========================================================
 StartTime           Mode  Duration    Size   TLI  Status
==========================================================
2015-07-30 13:36:38  FULL        0m    15MB    15  DONE
The status of the backup we have just taken is DONE. This is because we does not do validate yet. So, do validate command next.

$ pg_rman validate
INFO: validate: "2015-07-30 13:36:38" backup, archive log files and server log files by CRC
INFO: backup "2015-07-30 13:36:38" is valid

$ pg_rman show
==========================================================
 StartTime           Mode  Duration    Size   TLI  Status
==========================================================
2015-07-30 13:36:38  FULL        0m    15MB    15  OK



--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.

 --删除指定时间点之前的数据
 pg_rman delete '2015-12-28 14:40:00'
 --注意pg_rman至少要保存一个完整的备份及其增量
 WARNING: cannot delete backup with start time "2015-12-28 14:29:22"
 DETAIL: This is the incremental backup necessary for successful recovery.
 WARNING: cannot delete backup with start time "2015-12-28 14:26:42"
 DETAIL: This is the latest full backup necessary for successful recovery.
  --如果实在想删除可以指定 -f 参数
  pg_rman delete -f '2015-12-28 14:39:51'
  
  
  
  

-- pg_rman 的delete只是删除其文件,但对于purge操作是删除备份目录中的备份信息
Though delete command removes actual data from file system, there remains some catalog information of deleted backups. In order to remove this, execute purge command

-- pg_rman 可用于初始化一个standby 数据库
pg_rman backup --pgdata=/home/postgres/pgdata_sby --backup-mode=full --host=master --standby-host=localhost --standby-port=5432

--pg_rman 的增量备份是基于文件系统的update time时间线
When taking an incremental backup, pg_rman check the timeline ID of the target database whether it is the same with the one of the full backup in backup list. 
But, pg_rman does not check whether the data itself is same with the full backup in backup list. 
So, you can take an incremental backup over the full backup against the database which has the same timeline ID but has different data



--对于pg_rman,如果备份的数据库全部丢失,包括 pg_xlog目录内的文件,其只能恢复到备份时的时间点
--模拟数据丢失
mv data data_bak

--使用pg_rman进行恢复,注意如果归档目录不存在,则需要手动创建一个归档目录
pg_rman restore

--恢复完成之后,由于xlog丢失,启动数据库失败,错误如下
2015-12-27 19:08:35.848 PST,,,6422,,5680a7b3.1916,2,,2015-12-27 19:08:35 PST,,0,LOG,00000,"invalid primary checkpoint record",,,,,,,,"ReadCheckpointRecord, xlog.c:6411",""
2015-12-27 19:08:35.848 PST,,,6422,,5680a7b3.1916,3,,2015-12-27 19:08:35 PST,,0,LOG,00000,"invalid secondary checkpoint record",,,,,,,,"ReadCheckpointRecord, xlog.c:6415",""
2015-12-27 19:08:35.848 PST,,,6422,,5680a7b3.1916,4,,2015-12-27 19:08:35 PST,,0,PANIC,XX000,"could not locate a valid checkpoint record",,,,,,,,"StartupXLOG, xlog.c:5121",""

--此时只能重置xlog,并取消恢复模式
pg_resetxlog -f /usr/local/postgresql/9.3.4/data
mv recovery.conf recovery.done


--对于pg_rman,如果备份的数据库全部丢失,但xlog日志存在,此时可以恢复到数据库崩溃的时间点
 rm -rf data/pg_xlog/
 --拷贝原有的xlog到恢复的数据库目录中
 cp -r data_bak/pg_xlog/ data
 --删除archive_status文件目录的文件
 rm -rf data/pg_xlog/archive_status/*
 
 --注释掉recovery.conf的恢复点
 #recovery_target_timeline = '2'
--启动数据库
 pg_ctl start


相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
1月前
|
关系型数据库 Java 数据库连接
PostgreSQL从小白到高手教程 - 第47讲:JMETER工具使用
PostgreSQL从小白到高手教程 - 第47讲:JMETER工具使用
106 3
|
3月前
|
关系型数据库 MySQL 数据库
rds备份与恢复
rds备份与恢复
55 3
|
3月前
|
关系型数据库 MySQL 数据库
Python tk dos命令备份mysql数据库
Python tk dos命令备份mysql数据库
24 0
|
3月前
|
存储 关系型数据库 MySQL
mysql数据库如何做到定期备份
mysql数据库如何做到定期备份
288 2
|
2月前
|
SQL 关系型数据库 MySQL
mysql怎么备份
mysql怎么备份
188 7
|
3月前
|
存储 关系型数据库 MySQL
利用Xtrabackup进行mysql增量备份和全量备份
利用Xtrabackup进行mysql增量备份和全量备份
175 0
|
10天前
|
SQL 存储 关系型数据库
mysql数据库备份与恢复
mysql数据库备份与恢复
|
2月前
|
关系型数据库 MySQL Linux
Linux环境下定时备份mysql数据库
Linux环境下定时备份mysql数据库
|
2月前
|
存储 关系型数据库 MySQL
mysql怎么备份
mysql怎么备份
21 7
|
2月前
|
监控 容灾 安全
规划阿里云RDS跨区迁移并构建容灾与备份策略
规划阿里云RDS(Relational Database Service)跨区迁移并构建容灾与备份策略
109 2