MogDB 学习笔记之 -- PITR恢复
# 概念描述
## 背景信息
当数据库崩溃或希望回退到数据库之前的某一状态时,MogDB的即时恢复功能(Point-In-Time Recovery,简称PITR)可以支持恢复到备份归档数据之后的任意时间点。
## 说明:
PITR仅支持恢复到物理备份数据之后的某一时间点。
仅主节点可以进行PITR恢复,备机需要进行全量build达成与主机数据同步。
## 前提条件
基于经过物理备份的全量数据文件。
基于已归档的WAL日志文件。
## PITR恢复流程
将物理备份的文件替换目标数据库目录。
删除数据库目录下pg_xlog/中的所有文件。
将归档的WAL日志文件复制到pg_xlog文件中(此步骤可以省略,通过配置recovery.conf恢复命令文件中的restore_command项替代)。
在数据库目录下创建恢复命令文件recovery.conf,指定数据库恢复的程度。
启动数据库。
连接数据库,查看是否恢复到希望预期的状态。
若已经恢复到预期状态,通过pg_xlog_replay_resume()指令使主节点对外提供服务。
## 恢复目标设置(四选一)
recovery_target_name = 'restore_point_1' ## 还原到一个使用pg_create_restore_point()创建的还原点
recovery_target_time = '2020-01-01 12:00:00' ## 还原到一个指定时间戳
recovery_target_xid = '3000' ## 还原到一个事务ID
recovery_target_lsn = '0/0FFFFFF' ## 还原到日志的指定LSN点
recovery_target_inclusive = true ## 声明是否在指定恢复目标之后停止(true) 或 之前停止(false),不支持 recovery_target_name 配置#注意:如果不配置任何恢复目标 或 配置目标不存在,则默认恢复到最新的WAL日志点。
# 测试验证
## 1、环境准备(gs_baasebackup 备份)
```
[omm@db1 db1]$ gs_basebackup -U em_ljc -W -h 192.168.3.24 -p 26000 -D /home/omm/gs_pitr
Password:
INFO: The starting position of the xlog copy of the full build is: 4/EA000028. The slot minimum LSN is: 0/0.
[2022-10-29 14:18:24]:begin build tablespace list
[2022-10-29 14:18:24]:finish build tablespace list
[2022-10-29 14:18:24]:begin get xlog by xlogstream
[2022-10-29 14:18:24]: check identify system success
[2022-10-29 14:18:24]: send START_REPLICATION 4/EA000000 success
[2022-10-29 14:18:24]: keepalive message is received
[2022-10-29 14:18:24]: keepalive message is received
[2022-10-29 14:18:27]: keepalive message is received
[2022-10-29 14:18:30]: keepalive message is received
[2022-10-29 14:18:30]: keepalive message is received
[2022-10-29 14:18:33]: keepalive message is received
[2022-10-29 14:18:36]: keepalive message is received
[2022-10-29 14:18:36]: keepalive message is received
[2022-10-29 14:18:39]: keepalive message is received
[2022-10-29 14:18:42]: keepalive message is received
[2022-10-29 14:18:42]: keepalive message is received
[2022-10-29 14:18:45]: keepalive message is received
[2022-10-29 14:18:47]: keepalive message is received
[2022-10-29 14:18:50]: keepalive message is received
[2022-10-29 14:18:53]: keepalive message is received
[2022-10-29 14:18:56]: keepalive message is received
[2022-10-29 14:18:59]: keepalive message is received
[2022-10-29 14:18:59]: keepalive message is received
[2022-10-29 14:19:02]: keepalive message is received
[2022-10-29 14:19:05]: keepalive message is received
[2022-10-29 14:19:05]: keepalive message is received
[2022-10-29 14:19:08]: keepalive message is received
[2022-10-29 14:19:11]: keepalive message is received
[2022-10-29 14:19:11]: keepalive message is received
[2022-10-29 14:19:14]: keepalive message is received
[2022-10-29 14:19:17]: keepalive message is received
[2022-10-29 14:19:17]: keepalive message is received
[2022-10-29 14:19:20]: keepalive message is received
[2022-10-29 14:19:23]: keepalive message is received
[2022-10-29 14:19:23]: keepalive message is received
[2022-10-29 14:19:29]:gs_basebackup: base backup successfully
[omm@db1 db1]$
```
## 2、创建测试数据(还原点recovery_target_name )
```
miao=> create table t1 (id int,tm timestamp,LSN varchar(20));
insert into t1 values(1,now(),'Started');
select * from t1;
select * from pg_switch_xlog();
CREATE TABLE
miao=> insert into t1 values(1,now(),'Started');
INSERT 0 1
miao=> select * from t1;
id | tm | lsn
----+----------------------------+---------
1 | 2022-10-29 14:22:52.273009 | Started
(1 row)
miao=> select * from pg_switch_xlog();
pg_switch_xlog
----------------
4/EC0029F8
(1 row)
miao=>
-- 创建一个还原点 restore_point_1
miao=> select pg_create_restore_point('restore_point_1');
pg_create_restore_point
-------------------------
4/ED00EF40
(1 row)
```
## 3、第2次插入数据(时间recovery_target_time )
```
miao=> insert into t1 values(2,now(),'First Insert');
INSERT 0 1
miao=> select * from t1;
id | tm | lsn
----+----------------------------+--------------
1 | 2022-10-29 14:22:52.273009 | Started
2 | 2022-10-29 14:26:10.55628 | First Insert
(2 rows)
miao=> select pg_switch_xlog();
select now();
pg_switch_xlog
----------------
4/ED017848
(1 row)
miao=> select now();
now
-------------------------------
2022-10-29 14:26:10.754567+08
(1 row)
```
## 4、第3次插入数据(LSN recovery_target_lsn )
```
miao=> insert into t1 values(3,now(),'Second Insert');
INSERT 0 1
miao=> select * from t1;
id | tm | lsn
----+----------------------------+---------------
1 | 2022-10-29 14:22:52.273009 | Started
2 | 2022-10-29 14:26:10.55628 | First Insert
3 | 2022-10-29 14:31:13.220708 | Second Insert
(3 rows)
miao=> select pg_switch_xlog();
select * from pg_current_xlog_location();
pg_switch_xlog
----------------
4/EE0230C0
(1 row)
miao=>
miao=> select * from pg_current_xlog_location();
pg_current_xlog_location
--------------------------
4/EF000148
(1 row)
```
## 5、模拟故障删除目录
```
[omm@db1 gs_pitr]$ cp /archivelog/* /home/omm/gs_pitr/archivelog/
[omm@db1 gs_pitr]$ gs_om -t stop
Stopping cluster.
=========================================
Successfully stopped cluster.
=========================================
End stop cluster.
[omm@db1 gs_pitr]$ rm -fr /mogdb/data/db1/
```
## 6、第一阶段恢复(按照还原点进行恢复)
```
[omm@db1 data]$ mkdir /mogdb/data/db1/
[omm@db1 db1]$ cp -fr /home/omm/gs_pitr/* /mogdb/data/db1/
[omm@db1 db1]$ vi recovery.conf
restore_command = 'cp /mogdb/data/db1/archivelog/%f %p'
recovery_target_name = 'restore_point_1' ## 恢复到指定的还原点restore_point_1,此时后面2条数据
recovery_target_inclusive = true
[omm@db1 db1]$ gs_om -t start
Starting cluster.
=========================================
[SUCCESS] db1
2022-10-29 15:22:47.061 635cd4c6.1 [unknown] 140295007823424 [unknown] 0 dn_6001 01000 0 [BACKEND] WARNING: could not create any HA TCP/IP sockets
2022-10-29 15:22:47.064 635cd4c6.1 [unknown] 140295007823424 [unknown] 0 dn_6001 01000 0 [BACKEND] WARNING: Failed to initialize the memory protect for g_instance.attr.attr_storage.cstore_buffers (1024 Mbytes) or shared memory (1967 Mbytes) is larger.
=========================================
Successfully started.
[omm@db1 db1]$ gsql -d miao -p 26000 -U dbmt -r
Password for user dbmt:
gsql ((MogDB 2.1.1 build b5f25b20) compiled at 2022-03-21 14:42:30 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
miao=> \d
List of relations
Schema | Name | Type | Owner | Storage
--------+--------------------------------+-------+-------+------------------------------------------------------
dbmt | b1921101_bak | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | cc_chx | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | cd_material | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | customer_me_degree_wbinfo | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | customer_me_degree_wbinfo_test | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | erp_i_contract_class | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | scm_warning_reauidt_detail | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | sm_filemanagerconfig | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | t_o | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | t_o_t | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | t_p_t | table | dbmt | {orientation=row,compression=no,storage_type=USTORE}
dbmt | temp1 | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | tiji_update | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | tmp_mis_code_zq | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | tmp_mul_stock_detail_20200614 | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | tmp_output_bill_20190821 | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | tmp_project_party | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | tmp_unit_vol | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | tmp_user_rep | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | ur_s_role | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | w_work_cooperate_gjback0721 | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
dbmt | yang_tempate3 | table | dbmt | {orientation=row,compression=no,storage_type=ustore}
public | t1 | table | omm | {orientation=row,compression=no,storage_type=USTORE}
(105 rows)
miao=> select * from t1;
id | tm | lsn
----+----------------------------+---------
1 | 2022-10-29 14:22:52.273009 | Started
(1 row)
miao=>
```
## 7、 第二阶段恢复(按照时间进行还原)
```
[omm@db1 db1]$ vi recovery.conf
restore_command = 'cp /mogdb/data/db1/archivelog/%f %p'
recovery_target_time = '2022-10-29 14:26:44'
recovery_target_inclusive = true
~
"recovery.conf" [New] 3L, 137C written
[omm@db1 db1]$ gs_om -t start
Starting cluster.
=========================================
[SUCCESS] db1
2022-10-29 15:22:47.061 635cd4c6.1 [unknown] 140295007823424 [unknown] 0 dn_6001 01000 0 [BACKEND] WARNING: could not create any HA TCP/IP sockets
2022-10-29 15:22:47.064 635cd4c6.1 [unknown] 140295007823424 [unknown] 0 dn_6001 01000 0 [BACKEND] WARNING: Failed to initialize the memory protect for g_instance.attr.attr_storage.cstore_buffers (1024 Mbytes) or shared memory (1967 Mbytes) is larger.
=========================================
Successfully started.
[omm@db1 db1]$ gsql -d miao -U dbmt -p 26999 -r
failed to connect /opt/mogdb/tools/omm_mppdb:26999.
[omm@db1 db1]$ gsql -d miao -U dbmt -p 26000 -r
Password for user dbmt:
gsql ((MogDB 2.1.1 build b5f25b20) compiled at 2022-03-21 14:42:30 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
miao=> select * from t1;
id | tm | lsn
----+----------------------------+--------------
1 | 2022-10-29 14:22:52.273009 | Started
2 | 2022-10-29 14:26:10.55628 | First Insert
(2 rows)
```
## 8、 第三阶段恢复(按照lsn进行恢复)
```
restore_command = 'cp /mogdb/data/db1/archivelog/%f %p'
recovery_target_lsn = '4/EE0230C0'
recovery_target_inclusive = true
[omm@db1 archivelog]$ gs_om -t stop
Stopping cluster.
[omm@db1 db1]$ gs_om -t start
Starting cluster.
=========================================
[SUCCESS] db1
2022-10-29 16:08:54.842 635cdf96.1 [unknown] 140627704972864 [unknown] 0 dn_6001 01000 0 [BACKEND] WARNING: could not create any HA TCP/IP sockets
2022-10-29 16:08:54.887 635cdf96.1 [unknown] 140627704972864 [unknown] 0 dn_6001 01000 0 [BACKEND] WARNING: Failed to initialize the memory protect for g_instance.attr.attr_storage.cstore_buffers (1024 Mbytes) or shared memory (1967 Mbytes) is larger.
=========================================
Successfully started.
[omm@db1 db1]$ gsql -d miao -U dbmt -p 26000
Password for user dbmt:
gsql ((MogDB 2.1.1 build b5f25b20) compiled at 2022-03-21 14:42:30 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
miao=> select 8 from t1;
?column?
----------
8
8
8
(3 rows)
miao=> select * from t1;
id | tm | lsn
----+----------------------------+---------------
1 | 2022-10-29 14:22:52.273009 | Started
2 | 2022-10-29 14:26:10.55628 | First Insert
3 | 2022-10-29 14:31:13.220708 | Second Insert
(3 rows)
miao=>
```
## 9、 手动结束PITR状态
```
miao=> select pg_is_in_recovery();
pg_is_in_recovery
-------------------
t
(1 row)
miao=> select pg_xlog_replay_resume();
pg_xlog_replay_resume
-----------------------
(1 row)
[omm@db1 db1]$ gsql -d miao -U dbmt -p 26000 -r
Password for user dbmt:
gsql ((MogDB 2.1.1 build b5f25b20) compiled at 2022-03-21 14:42:30 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
miao=> select pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
(1 row)
miao=>
```
MogDB 学习笔记之 -- PITR恢复的更多相关文章
- RMAN_学习笔记3_RMAN Catalog恢复目录
2014-12-23 Created By BaoXinjian
- git 学习笔记 —— 切换和恢复提交版本( git reset/reflog/tag 命令)
记录一下关于 git 不同提交版本间切换的操作以及如何恢复至切换之前的版本. 切换到之前提交的版本 —— git reset --hard 笔者在使用 git 时,首先接触到了一个"黑魔法& ...
- Centos7安装gitlab11 学习笔记之备份恢复及邮箱配置
一.备份 修改配置文件 vim /etc/gitlab/gitlab.rb 默认路径为 # gitlab_rails['backup_path'] = "/var/opt/gitlab/ba ...
- Dynamic CRM 2013学习笔记(六)备份和恢复
这节我们将讨论下怎么维护CRM,包括以下主要内容: 备份CRM DB 备份CRM 系统 恢复CRM DB 恢复 CRM 系统 一. 备份CRM DB 下面的步骤将为CRM DB创建一个维护计划 ...
- MongoDB学习笔记(三)--权限 && 导出导入备份恢复 && fsync和锁
权限 绑定内网I ...
- ucos实时操作系统学习笔记——操作系统在STM32的移植
使用ucos实时操作系统是在上学的时候,导师科研项目中.那时候就是网上找到操作系统移植教程以及应用教程依葫芦画瓢,功能实现也就罢了,没有很深入的去研究过这个东西.后来工作了,闲来无聊就研究了一下这个只 ...
- redis 学习笔记(6)-cluster集群搭建
上次写redis的学习笔记还是2014年,一转眼已经快2年过去了,在段时间里,redis最大的变化之一就是cluster功能的正式发布,以前要搞redis集群,得借助一致性hash来自己搞shardi ...
- 转:学习笔记: Delphi之线程类TThread
学习笔记: Delphi之线程类TThread - 5207 - 博客园http://www.cnblogs.com/5207/p/4426074.html 新的公司接手的第一份工作就是一个多线程计算 ...
- SQLite学习笔记(七)&&事务处理
说到事务一定会提到ACID,所谓事务的原子性,一致性,隔离性和持久性.对于一个数据库而言,通常通过并发控制和故障恢复手段来保证事务在正常和异常情况下的ACID特性.sqlite也不例外,虽然简单,依然 ...
- 0030 Java学习笔记-面向对象-垃圾回收、(强、软、弱、虚)引用
垃圾回收特点 垃圾:程序运行过程中,会为对象.数组等分配内存,运行过程中或结束后,这些对象可能就没用了,没有变量再指向它们,这时候,它们就成了垃圾,等着垃圾回收程序的回收再利用 Java的垃圾回收机制 ...
随机推荐
- Eureka出现Connect to localhost:8761 timed out问题
原先使用application.properties spring.application.name=hello-serviceeureka.client.service-url.defauleZon ...
- 七、25.创建user子分支并把代码推送到码云仓库中
打开终端点击+新建一个终端 注意 :如下操作都是在2:powershell下进行 先来检查一下当前所处分支 git branch 我们应该把这些代码都写到user分支上 接下来应该把这些代码统一迁移到 ...
- 【面试题】面试突击71:GET 和 POST 有什么区别?
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第4天,点击查看活动详情 GET 和 POST 是 HTTP 请求中最常用的两种请求方法,在日常开发的 RESTful 接口中,都能 ...
- A调用B方法,@Transactional事务问题
总结:方法A调用方法B:1.如果只有A加@Transactional注解:则AB在同一事务中,任意异常都回滚:2.如果只有B加@Transactional注解:AB方法为同一类,事务失效任意异常都不回 ...
- ps 合并两张图片为一张
打开PS并点击左上角的"文件":之后再点击"打开"(也可以按下快捷键"Ctrl+O"),打开文件选择窗口. 2 在打开的文件选择窗口中,找到 ...
- 初学银河麒麟linux笔记 第三章 QT缺少的模块下载、更新软件源操作
在共享文件夹里将WINDOWS里已经编写好的QT程序编译之后,提示 Qt5: Unknown module(s) in QT: serialport 经查询,需要安装对应的模块库,写入 sudo ap ...
- linux添加分辨率
由于屏幕分辨率是1920X1080,但是虚拟机中的centos的分辨率设置中没有这个值,因此需要添加一个.在终端中输入如下命令:1.cvt 1920 1080得到: # 1920x1080 59.96 ...
- .net core layui折叠表格的应用。
效果展示 头部的折叠,展开,搜索按钮 <div class="layui-fluid"> <div style="margin-top: 20px;&q ...
- 自定义注解获取请求Header中的值
前言 这几天开发一个项目,为了方便,前台将当前登陆人的ID和名称放在每个请求的Header中(这里不考虑安全性之类的),这样后台只要需要用到,就直接从Header中get出来就可以了. 后台实现方法 ...
- docker compose服务编排简介、基于发布包构建多个webapi容器 和 基于镜像实现Nginx反向代理webapi
一. docker compose服务编排简介 1. 背景 微服务架构的应用系统中一般包含若干个微服务,每个微服务一般都会部署多个实例,如果每个微服务都要手动启停,维护的工作量会很大: A. 要创建镜 ...