【PG备份恢复】基于时间点的恢复(踩坑指南)

server/2024/10/25 16:22:47/

目录

1 设置基于时间点恢复所需的配置

sql.conf-toc" style="margin-left:80px;">1.1 修改配置文件 postgresql.conf

1.2 生效配置

2 进行一次全备 

3 模拟增量数据插入 

4 查看当前时间点,LSN号,XID 事务ID,当前的WAL 文件等

5 进行一次WAL 日志的切换

6 模拟故障发生

7 进行基于时间点的恢复

7.1 停止原实例

7.2 将原来的数据目录 mv 走,

7.3  将全备拷贝一份为pg 的数据目录:

7.4  修改目录权限和属组用户

sql.auto.conf%C2%A0-toc" style="margin-left:80px;">7.5 配置增量恢复 修改文件 postgresql.auto.conf 

7.6 创建recovery.signal文件

7,7 启动恢复的实例 

8 检验数据

9 恢复可写 

常见报错 

报错1 使用了错误的恢复配置文件

报错1原因 : 高版本移除了recovery.conf 文件

报错2 找不到需要的WAL日志

TODO 

参考 


1 设置基于时间点恢复所需的配置

要启用WAL归档,

  • 需设置wal_level配置参数为replica或更高,
  • 设置archive_mode为on
  • 并且使用archive_command配置参数指定一个shell命令

sql.conf">1.1 修改配置文件 postgresql.conf

vim postgresql.conf
 

archive_mode = on
archive_command = 'cp %p /data1/backups/pg_wal_archive/%f'
wal_level = replica

1.2 生效配置

2 进行一次全备 

2.1 创建备份目录 


mkdir -p /data/backup/pg_backup

chown postgres:postgres /data/backup/pg_backup

2.2 进行一次全备 

pg_basebackup -D /data/backup/pg_backup  -v -P -R

备份输出日志 :

pg_basebackup -D /data/backup/pg_backup  -v -P -R
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/1F000028 on timeline 10
pg_basebackup: starting background WAL receiver
pg_basebackup: created temporary replication slot "pg_basebackup_20961"
57113/57113 kB (100%), 1/1 tablespace
pg_basebackup: write-ahead log end point: 0/1F000100
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: renaming backup_manifest.tmp to backup_manifest
pg_basebackup: base backup completed

3 模拟增量数据插入 

sql">yanhaihang=#  insert into foo values  (100011),(100012),(100013);
INSERT 0 3

4 查看当前时间点,LSN号,XID 事务ID,当前的WAL 文件等

sql">yanhaihang=#   select now();now
-------------------------------2024-09-25 17:16:45.699173+08
(1 row)yanhaihang=#  select pg_current_wal_lsn();pg_current_wal_lsn
--------------------0/20000BD8
(1 row)yanhaihang=# select txid_current();txid_current
--------------778
(1 row)yanhaihang=#  select pg_walfile_name(pg_current_wal_lsn());pg_walfile_name
--------------------------0000000A0000000000000020
(1 row)

查看插入增量数据的时间点 (作为恢复的时间点

yanhaihang=#   select now();
              now
-------------------------------
 2024-09-25 17:16:45.699173+08
 

LSN号

yanhaihang=#  select pg_current_wal_lsn();
 pg_current_wal_lsn
--------------------
 0/20000BD8
 

XID 事务ID

yanhaihang=# select txid_current();
 txid_current
--------------
          778
 

当前的WAL 文件

yanhaihang=#  select pg_walfile_name(pg_current_wal_lsn());
     pg_walfile_name
--------------------------
 0000000A0000000000000020


5 进行一次WAL 日志的切换

yanhaihang=# SELECT pg_switch_wal();
 pg_switch_wal
---------------
 0/20000D38
 

再次查看当前的WAL日志,可以看到 由原来的20 变为了 21 

yanhaihang=#  select pg_walfile_name(pg_current_wal_lsn());
     pg_walfile_name
--------------------------
 0000000A0000000000000021
 

wal 日志目录 ,

查看归档目录 ,可以看到  0000000A0000000000000020 日志已经归档

6 模拟故障发生

假如以下场景发生: 有研发删除了foo全表 ,或者错误更新了foo全表 ,或者机房故障导致数据库启动不了。

如果只用全备恢复,从全备时刻到故障时刻的增量的DML 就会丢失 。

7 进行基于时间点的恢复

7.1 停止原实例

pg_ctl   -D /data/storage/pgsql/data stop

7.2 将原来的数据目录 mv 走,

mv data data_bak

7.3  将全备拷贝一份为pg 的数据目录:

 cp -R /data/backup/pg_backup/ /data/storage/pgsql/data

7.4  修改目录权限和属组用户

chown -R postgres:postgres /data/storage/pgsql/data

chmod 700  /data/storage/pgsql/data

sql.auto.conf%C2%A0">7.5 配置增量恢复 修改文件 postgresql.auto.conf 

vim postgresql.auto.conf

restore_command = 'cp /data1/backups/pg_wal_archive/%f %p'
recovery_target_time = '2024-09-25 17:16:45'

7.6 创建recovery.signal文件

因为备份的时候加了-R 所以会创建 standby.signal 文件 ,如果你是想让改实例作为主库启动 将该文件删除

创建recovery.signal,

我这里直接mv 

mv standby.signal recovery.signal

7,7 启动恢复的实例 

pg_ctl   -D /data/storage/pgsql/data start

日志输出:

sql">$pg_ctl   -D /data/storage/pgsql/data start
waiting for server to start....2024-09-25 17:35:02.439 CST [14388] LOG:  starting PostgreSQL 13.12 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2024-09-25 17:35:02.440 CST [14388] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2024-09-25 17:35:02.441 CST [14388] LOG:  could not create IPv6 socket for address "::": Address family not supported by protocol
2024-09-25 17:35:02.441 CST [14388] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2024-09-25 17:35:02.443 CST [14395] LOG:  database system was interrupted; last known up at 2024-09-25 17:12:10 CST
cp: cannot stat ‘/data1/backups/pg_wal_archive/0000000B.history’: No such file or directory
2024-09-25 17:35:02.485 CST [14395] LOG:  starting point-in-time recovery to 2024-09-25 17:16:45+08
2024-09-25 17:35:02.487 CST [14395] LOG:  restored log file "0000000A.history" from archive
2024-09-25 17:35:02.501 CST [14395] LOG:  restored log file "0000000A000000000000001F" from archive
2024-09-25 17:35:02.515 CST [14395] LOG:  redo starts at 0/1F000028
2024-09-25 17:35:02.516 CST [14395] LOG:  consistent recovery state reached at 0/1F000100
2024-09-25 17:35:02.516 CST [14388] LOG:  database system is ready to accept read only connections
2024-09-25 17:35:02.529 CST [14395] LOG:  restored log file "0000000A0000000000000020" from archivedone
server started
[postgres@erp-db-mysql-s17.ys:/data/storage/pgsql/data]$2024-09-25 17:35:02.538 CST [14395] LOG:  recovery stopping before commit of transaction 778, time 2024-09-25 17:16:58.116118+08
2024-09-25 17:35:02.538 CST [14395] LOG:  pausing at the end of recovery
2024-09-25 17:35:02.538 CST [14395] HINT:  Execute pg_wal_replay_resume() to promote.

8 检验数据

 select * from foo  order by id desc limit 10;

9 恢复可写 

写入数据报错 

 insert into foo values  (100014);

sql">yanhaihang=#  insert into foo values  (100014);
2024-09-25 17:36:48.574 CST [18061] ERROR:  cannot execute INSERT in a read-only transaction
2024-09-25 17:36:48.574 CST [18061] STATEMENT:  insert into foo values  (100014);
ERROR:  cannot execute INSERT in a read-only transaction

恢复可写 

 select pg_wal_replay_resume();

sql">yanhaihang=# select pg_wal_replay_resume();pg_wal_replay_resume
----------------------(1 row)yanhaihang=# 2024-09-25 17:37:52.704 CST [14395] LOG:  redo done at 0/20000BD8
2024-09-25 17:37:52.704 CST [14395] LOG:  last completed transaction was at log time 2024-09-25 17:14:27.560912+08
cp: cannot stat ‘/data1/backups/pg_wal_archive/0000000B.history’: No such file or directory
2024-09-25 17:37:52.708 CST [14395] LOG:  selected new timeline ID: 11
2024-09-25 17:37:52.727 CST [14395] LOG:  archive recovery complete
2024-09-25 17:37:52.729 CST [14395] LOG:  restored log file "0000000A.history" from archive
2024-09-25 17:37:52.737 CST [14388] LOG:  database system is ready to accept connections

常见报错 

报错1 使用了错误的恢复配置文件

报错关键字  FATAL:  using recovery command file "recovery.conf" is not supported

报错1原因 : 高版本移除了recovery.conf 文件

PostgreSQL12 附录 E. 版本说明

  • recovery.conf设置移动到postgresql.conf中。 (Masao Fujii, Simon Riggs, Abhijit Menon-Sen, Sergei Kornilov)

recovery.conf不再使用,如果该文件存在,服务器将无法启动。 recovery.signal和standby.signal文件现在用于切换到非主模式。 trigger_file设置已更名为promote_trigger_file。 删除了standby_mode设置。

官方文档参考

http://postgres.cn/docs/13/continuous-archiving.html

报错2 找不到需要的WAL日志

报错关键字

cp: cannot stat ‘/data1/backups/pg_wal_archive/0000000A.history’: No such file or directory

cp: cannot stat ‘/data1/backups/pg_wal_archive/00000009000000000000001C’: No such file or directory

FATAL: recovery ended before configured recovery target was reached

原因:需要的wal 没有被归档?

解决 : 进行一次WAL切换  SELECT pg_switch_wal(); 

TODO 

WAL的切换规则 

参考 

多种恢复方式  官方文档 中的 19.5.4. 归档恢复

http://postgres.cn/docs/13/runtime-config-wal.html#RUNTIME-CONFIG-WAL-RECOVERY-TARGET

WAL 日志 

http://postgres.cn/docs/13/wal.html

基于时间点的恢复 官方文档

http://postgres.cn/docs/13/continuous-archiving.html


http://www.ppmy.cn/server/122313.html

相关文章

18-pg内核之日志管理器(六)checkpoint

概念 数据库中除了实际存储的数据之外,还存在许多事务相关的日志,如WAL日志,CLOG日志。MultiXact日志等,每次包含DML操作的事务都会产生这些日志,随着时间的推移,如果不进行清理,日志会一直增大…

react crash course 2024(7) react router dom

安装 npm i react-router-dom 引入 import {Route,createBrowserRouter,createRoutesFromElements,RouterProvider} from react-router-dom 在app.jsx const router createBrowserRouter(createRoutesFromElements(<Route index element {<h1>My App</h1>…

springboot3通过HttpRequest请求soap

http://10.20.0.11:80/wms/index.php/Home/SOAP/getVehStatus为请求地址 其中所有的getOfflineAmount字段都是调用的方法名 public static String getOfflineAmount() {String result2 HttpRequest.post("http://10.20.0.11:80/wms/index.php/Home/SOAP/getVehStatus&quo…

【深度学习】注意力机制与自注意力机制详解

深度学习中的注意力机制/自注意力机制详解 1. 注意力机制的通俗理解2. 注意力和自注意力机制的区别3. 自注意力机制原理与计算流程3.1 引入自注意力机制的目的与思想3.2 从向量角度理解 [R1]3.3 从Self-Attention核心公式理解 [R3] 4. 多头自注意力机制&#xff08;Multi-head …

Python 单元测试:深入理解与实战应用20240919

Python 单元测试&#xff1a;深入理解与实战应用 引言 在动态语言如 Python 中&#xff0c;代码的灵活性和动态特性使得开发效率大大提升&#xff0c;但也带来了潜在的风险&#xff1a;小的改动可能导致不可预见的功能失效。因此&#xff0c;确保代码逻辑的正确性和稳健性至关…

每天一个数据分析题(四百七十八)- 线性回归建模

关于线性回归建模&#xff0c;QQ图用来检验什么&#xff1f; A. 被检验分布是否服从正态分布 B. 两个分布是否一致 C. 被检验分布是否服从t分布 D. 被检验分布是否服从卡方分布 数据分析认证考试介绍&#xff1a;点击进入 题目来源于CDA模拟题库 点击此处获取答案 数据…

理解信息安全中的SOAR

在信息安全领域&#xff0c;SOAR&#xff08;Security Orchestration, Automation, and Response&#xff0c;安全编排、自动化与响应&#xff09;是一个现代化的解决方案&#xff0c;旨在通过集成和自动化的方式优化安全操作流程&#xff0c;提升威胁检测、事件响应的速度与效…

ICT企业主数据项目实战:实施全过程详解

主数据在企业信息化战略中处于核心地位、基础支撑地位&#xff0c;今天小亿分享一个成功实施的ICT行业主数据管理项目案例。这个项目不仅见证了主数据治理的深刻变革&#xff0c;更展现了技术创新与业务需求的完美融合。通过细致入微的剖析与实战经验的总结&#xff0c;我们希望…