pg_rman 实现对postgresql数据库的备份,恢复与管理

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: --下载软件https://github.com/ossc-db/pg_rman--安装 unzip pg_rman-master.
--下载软件
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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
13天前
|
关系型数据库 MySQL 分布式数据库
阿里云PolarDB云原生数据库收费价格:MySQL和PostgreSQL详细介绍
阿里云PolarDB兼容MySQL、PostgreSQL及Oracle语法,支持集中式与分布式架构。标准版2核4G年费1116元起,企业版最高性能达4核16G,支持HTAP与多级高可用,广泛应用于金融、政务、互联网等领域,TCO成本降低50%。
|
14天前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
|
14天前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
20天前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
|
4月前
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
296 62
|
2月前
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL数据库的WAL日志与数据写入的过程
PostgreSQL中的WAL(预写日志)是保证数据完整性的关键技术。在数据修改前,系统会先将日志写入WAL,确保宕机时可通过日志恢复数据。它减少了磁盘I/O,提升了性能,并支持手动切换日志文件。WAL文件默认存储在pg_wal目录下,采用16进制命名规则。此外,PostgreSQL提供pg_waldump工具解析日志内容。
173 0
|
4月前
|
存储 关系型数据库 分布式数据库
【赵渝强老师】基于PostgreSQL的分布式数据库:Citus
Citus 是基于 PostgreSQL 的开源分布式数据库,采用 shared nothing 架构,具备良好的扩展性。它以插件形式集成,部署简单,适用于处理大规模数据和高并发场景。本文介绍了 Citus 的基础概念、安装配置步骤及其在单机环境下的集群搭建方法。
308 2
|
6月前
|
存储 关系型数据库 MySQL
利用Cron表达式实现MySQL数据库的定时备份
以上就是如何使用Cron表达式和mysqldump命令实现MySQL数据库的定时备份。这种方法的优点是简单易用,而且可以根据需要定制备份的时间和频率。但是,它也有一些限制,例如,它不能备份MySQL服务器的配置文件和用户账户信息,也不能实现增量备份。如果需要更复杂的备份策略,可能需要使用专门的备份工具或服务。
136 15
|
6月前
|
SQL 关系型数据库 数据库
【赵渝强老师】创建PostgreSQL的数据库
本文介绍了在PostgreSQL中通过SQL命令“create database”创建数据库的方法。首先查询系统目录pg_database以查看现有数据库集合,然后使用“create database”命令创建新数据库,并了解其在$PDATA/base目录下对应的文件夹生成。最后重新查询数据库集合确认创建结果,附带视频讲解便于理解操作步骤及注意事项。
139 1
|
7月前
|
关系型数据库 数据库 PostgreSQL
【赵渝强老师】PostgreSQL的模板数据库
在PostgreSQL中,创建新数据库时,默认通过拷贝`template1`实现。`template1`包含标准系统对象,可自定义以影响新数据库内容;而`template0`是纯净模板,仅含预定义对象且不应修改。视频讲解和代码示例展示了如何查看现有数据库信息及标识字段的作用。 ![图示](https://ucc.alicdn.com/pic/developer-ecology/yub6x2mlkqwck_398ed06397a44c2d9bfbb5ae5c90bbc0.png) [视频链接](https://www.bilibili.com/video/BV1szyfY4EQn)
119 0
【赵渝强老师】PostgreSQL的模板数据库

推荐镜像

更多