openGauss/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 ... -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=>

openGauss/MogDB 学习笔记之 -- PITR恢复的更多相关文章

  1. MogDB 学习笔记之 -- PITR恢复

    # 概念描述## 背景信息当数据库崩溃或希望回退到数据库之前的某一状态时,MogDB的即时恢复功能(Point-In-Time Recovery,简称PITR)可以支持恢复到备份归档数据之后的任意时间 ...

  2. MogDB 学习笔记之 --exchange partition

    # 概念描述MogDB 提供了从分区交换的功能,如单表转化到一个分区中基本语法:ALTER TABLE...EXCHANGE PARTITION数据库版本# 测试验证## 1.环境准备``` miao ...

  3. RMAN_学习笔记3_RMAN Catalog恢复目录

    2014-12-23 Created By BaoXinjian

  4. git 学习笔记 —— 切换和恢复提交版本( git reset/reflog/tag 命令)

    记录一下关于 git 不同提交版本间切换的操作以及如何恢复至切换之前的版本. 切换到之前提交的版本 —— git reset --hard 笔者在使用 git 时,首先接触到了一个"黑魔法& ...

  5. Centos7安装gitlab11 学习笔记之备份恢复及邮箱配置

    一.备份 修改配置文件 vim /etc/gitlab/gitlab.rb 默认路径为 # gitlab_rails['backup_path'] = "/var/opt/gitlab/ba ...

  6. MogDB 学习笔记之 -- 索引失效

    [[toc]]# 概念描述哪些操作会导致分区表的全局索引失效(比如 move partition,drop partition ,truncate partition ,split partition ...

  7. MogDB 学习笔记之 -- truncate 属于dml语句

    # 概念描述验证create 语句.alter 语句.truncate语句.drop语句 是属于ddl 还是dml # 测试验证 1.环境准备 ```修改log_statement 参数miao=# ...

  8. Dynamic CRM 2013学习笔记(六)备份和恢复

      这节我们将讨论下怎么维护CRM,包括以下主要内容: 备份CRM DB 备份CRM 系统 恢复CRM DB 恢复 CRM 系统   一. 备份CRM DB 下面的步骤将为CRM DB创建一个维护计划 ...

  9. MongoDB学习笔记(三)--权限 && 导出导入备份恢复 && fsync和锁

    权限                                                                                             绑定内网I ...

  10. ucos实时操作系统学习笔记——操作系统在STM32的移植

    使用ucos实时操作系统是在上学的时候,导师科研项目中.那时候就是网上找到操作系统移植教程以及应用教程依葫芦画瓢,功能实现也就罢了,没有很深入的去研究过这个东西.后来工作了,闲来无聊就研究了一下这个只 ...

随机推荐

  1. 【Filament】材质系统

    1 前言 ​ 本文主要介绍 Filament 的材质系统,官方介绍详见 → Filament Materials Guide.材质系统中会涉及到一些空间和变换的知识点,可以参考:[Unity3D]空间 ...

  2. mysql-对应删除 dict 脚本

    -- 1. 此 dict 是在不同租户下的数据字典,查询时需要根据 departid 进行分类查询 -- 2. 删除dict, dict分类主表类型与挂载的子表数据 -- 3. 通过查询到的主表的 g ...

  3. 3. JVM运行时数据区

    1. 运行时数据区概述 前面的章节中已经将类的加载过程大致过程说清楚了,此时类已经加载到内存中,,后面就是运行时数据区的各个组件的工作了 由上图可以看出来, jvm将class字节码加载完成后,后面运 ...

  4. Toyota Programming Contest 2024#2(AtCoder Beginner Contest 341)D - Only one of two(数论、二分)

    目录 链接 题面 题意 题解 代码 总结 链接 D - Only one of two 题面 题意 求第\(k\)个只能被\(N\)或\(M\)整除的数 题解 \([1,x]\)中的能被\(n\)整除 ...

  5. ants - 目前开源最优的协程池

    ants - 目前开源最优的协程池 目前我们的项目重度使用 ants 协程池,在开启一个 go 的时候并不是用 go 关键字,而是用一个封装的 go 函数来开启协程.框架底层,则是使用 ants 项目 ...

  6. Djiango视图层和模型层

    Djiango 使用教程 目录 Djiango 使用教程 一. 视图层 1.1 HttpResponse,render,redirect 1.2 JsonResponse 1.3 form表单上传文件 ...

  7. Mac下使用Docker快速布署FastGPT实现AI私有知识库

    FastGPT 是一个基于 LLM 大语言模型的知识库问答系统,提供开箱即用的数据处理.模型调用等能力.同时可以通过 Flow 可视化进行工作流编排,从而实现复杂的问答场景! 官网地址为:https: ...

  8. glibc 2.23 源码分析

    1. 基础知识 1.1 Linux 进程内存布局 Linux 系统在装载 elf 格式的程序文件时,会调用 loader 把可执行文件中的各个段依次载入到从某一地址开始的空间中(载入地址取决于 lin ...

  9. 市场主流的G-sensor芯片盘点

    一 前记 1.简介 随着可穿戴智能硬件的广泛发展,G-sensor成了一个必不可少的器件.梳理,测试和运用这些传感器.是做可穿戴产品必不可少的环节. 二 产品解析 1.ST的G-sensor型号LIS ...

  10. 基于BES2300芯片的开源DSP开发平台简述

    一 什么是开源DSP平台 所谓的开源DSP平台,就是软硬件全部开发接口,开发者可以在上面做DSP算法验证和算法开发.基于目前科研机构缺少开源的微型数字信号处理的情况,我们把bes2300的代码做了优化 ...