pg_rman在恢复服务器上恢复源库的备份

news/2024/7/9 20:21:13 标签: postgresql

文章目录

  • 说明
  • 恢复笔记
      • 待恢复的数据库文件所在目录与文件层级如下
      • 检查事项
      • 具体操作
    • 修改3: mv recovery.conf recovery.done
    • 需要重置 wal 日志然后启库
    • 检查效果
  • 补充笔记(2024-02-07更新)

pg_rman 安装与使用参考:
pg_rman 编译安装与使用

说明

  • 需求是能启库就行, 可以接受数据丢失, 恢复步骤有偷懒, 本文仅供个人记录笔记使用, 请不要完全照抄
  • 规范合理的步骤是需要使用 recovery.conf 文件, 写清楚目标环境上的归档日志路径(当前提前得将源库中的wal日志拷贝到目标环境归档路径下) 来启动并恢复

恢复笔记

以 db_test 为例:

待恢复的数据库文件所在目录与文件层级如下

/data/restore/pg_restore/backups/db_test20231216/014025/backup.ini

检查事项

0.目录级别与生产环境保持一致,如果不一致, 需要自己手动创建确保一致,
1.最上级目录(20231216同级目录)存在 pg_rman.ini
2.20231216/014025下存在 file_database.txt
3.文件 20231216/014025/backup.ini 中的 STATUS=OK

	-bash-4.2$ cat backup.ini
	# configuration
	BACKUP_MODE=FULL
	WITH_SERVERLOG=false
	COMPRESS_DATA=true
	# result
	TIMELINEID=5
	START_LSN=4bb/4e000028
	STOP_LSN=4bb/7e81c420
	START_TIME='2023-02-15 01:35:03'
	END_TIME='2023-02-15 07:42:53'
	RECOVERY_XID=181372900
	RECOVERY_TIME='2023-02-15 06:04:45'
	TOTAL_DATA_BYTES=233395485133
	READ_DATA_BYTES=233408059346
	READ_ARCLOG_BYTES=32782681011
	WRITE_BYTES=35758753390
	BLOCK_SIZE=8192
	XLOG_BLOCK_SIZE=8192
	STATUS=OK

具体操作

  • 创建恢复目录(你要将 数据库备份集恢复到哪里去), 相当于是执行了 pg_rman -B $BACKUP_PATH init
mkdir -p   /data/restore/2023_pg_restore/db_test/data
  • 检查待恢复的数据库文件集合是否正常, 如果显示 OK 则可以继续操作, 如果显示 RUNNING或者ERROR 则说明备份文件集合有问题, 需要重新弄一份新的
    pg_rman -B /data/restore/pg_restore/backups/db_test show

  • 具体恢复命令 (-D 指向你要将数据库文件集合恢复到哪里去)
    pg_rman -B /data/restore/pg_restore/backups/db_test restore -D /data/restore/2023_pg_restore/db_test/data
    这里如果报 ARC_PATH 相关的错误(ERROR), 需要手动创建该目录 ,如果只是报 warning 可以先忽略
    比如:
    mkdir -p /data/restore/2023_pg_restore/db_test/arc_log
    mkdir -p /data/restore/2023_pg_restore/db_test/srv_log
    更新 /data/restore/pg_restore/backups/db_test/pg_rman.ini 文件

  • 修改 配置文件 /data/restore/2023_pg_restore/db_test/data/postgresql.conf 与 /data/restore/2023_pg_restore/db_test/data/backup_label(如果存在的话)
    修改1: 禁用 archive_mode
    修改2: 修改 log_directory 与 data_directory 与 hba_file 配置项中的 data 值 与 /data/restore/2023_pg_restore/db_test/data 保持一致

修改3: mv recovery.conf recovery.done

修改4: mv backup_label backup_label_lxm
修改5: 将 listen_addresses= ‘10.33.45.110’ 改为 listen_addresses = ‘*’

  • 手动启动数据库 (-D 启动哪个目录对应的数据库实例 )
    pg_ctl start -D /data/restore/2023_pg_restore/db_test/data

如果还报错, 查看日志文件

-bash-4.2$ tailf /data/restore/2023_pg_restore/db_test/data/pg_log/postgresql-25.*csv
2023-12-25 15:44:37.909 HKT,,,119321,,658932e5.1d219,4,,2023-12-25 15:44:37 HKT,,0,FATAL,XX000,"could not locate required checkpoint record",,"If you are not restoring from a backup, try removing the file ""/data/restore/2023_pg_restore/db_test/data/backup_label"".",,,,,,,""
2023-12-25 15:44:37.910 HKT,,,119317,,658932e5.1d215,1,,2023-12-25 15:44:37 HKT,,0,LOG,00000,"startup process (PID 119321) exited with exit code 1",,,,,,,,,""
2023-12-25 15:44:37.910 HKT,,,119317,,658932e5.1d215,2,,2023-12-25 15:44:37 HKT,,0,LOG,00000,"aborting startup due to startup process failure",,,,,,,,,""
2023-12-25 15:49:10.374 HKT,,,136964,,658933f6.21704,1,,2023-12-25 15:49:10 HKT,,0,LOG,00000,"database system was interrupted; last known up at 2023-12-16 02:17:39 HKT",,,,,,,,,""
2023-12-25 15:49:10.375 HKT,,,136964,,658933f6.21704,2,,2023-12-25 15:49:10 HKT,,0,LOG,00000,"could not open tablespace directory ""pg_tblspc/0/PG_9.4_201409291"": No such file or directory",,,,,,,,,""
2023-12-25 15:49:10.459 HKT,,,136964,,658933f6.21704,3,,2023-12-25 15:49:10 HKT,,0,LOG,00000,"invalid primary checkpoint record",,,,,,,,,""
2023-12-25 15:49:10.459 HKT,,,136964,,658933f6.21704,4,,2023-12-25 15:49:10 HKT,,0,LOG,00000,"invalid secondary checkpoint record",,,,,,,,,""

FATAL,XX000,"could not locate required checkpoint record",

需要重置 wal 日志然后启库

Report bugs to <pgsql-bugs@postgresql.org>.
-bash-4.2$ pg_resetxlog -f /data/restore/2023_pg_restore/db_test/data
Transaction log reset
-bash-4.2$ 
-bash-4.2$  pg_ctl start -D /data/restore/2023_pg_restore/db_test/data
server starting
-bash-4.2$ 2023-12-25 15:50:13 HKT::@:[138504]: LOG:  redirecting log output to logging collector process
2023-12-25 15:50:13 HKT::@:[138504]: HINT:  Future log output will appear in directory "/data/restore/2023_pg_restore/db_test/data/pg_log".

检查效果

-bash-4.2$ netstat -anp |grep 5472
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:5472            0.0.0.0:*               LISTEN      138504/postgres     
tcp6       0      0 :::5472                 ::: *                    LISTEN      138504/postgres     
unix  2      [ ACC ]     STREAM     LISTENING     2208435216 138504/postgres      /tmp/.s.PGSQL.5472
unix  3      [ ]         STREAM     CONNECTED     154721   -                    
unix  3      [ ]         STREAM     CONNECTED     154720   -  

-bash-4.2$ psql -p 5472
psql (9.4.14)
Type "help" for help.

postgres=# \l+
                                                                    List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   |  Size   | Tablespace |                Description                 
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
 db_test   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 69 GB   | pg_default | 
 repmgr    | repmgr   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 687 MB  | pg_default | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 6729 kB | pg_default | unmodifiable empty database
           |          |          |             |             | postgres=CTc/postgres |         |            | 
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 8697 kB | pg_default | default template for new databases
           |          |          |             |             | postgres=CTc/postgres |         |            | 

补充笔记(2024-02-07更新)

1. 修改 /postgresql/hq/bk/restore/pg_restore/backups/iobs/pg_rman.ini 
将 ARCLOG_PATH 值改为一个新路径, 例如/postgresql/hq/bk/restore/2023_pg_restore/iobs_new_arc_log, 需要先手动创建该路径并授予 postgres的属主权限

2. 执行 pg_rman -B /postgresql/hq/bk/restore/pg_restore/backups/iobs/ restore -D /postgresql/hq/bk/restore/2023_pg_restore/iobs_new_lxm2 >> ./res.log 2>&1 &
进行恢复

3. 检查恢复结果
tail res.log
-bash-4.2$ tail res.log 
LOG: (16485/16492) 00000009000010B800000014
LOG: (16486/16492) 00000009000010B800000015
LOG: (16487/16492) 00000009000010B800000016
LOG: (16488/16492) 00000009000010B800000017
LOG: (16489/16492) 00000009000010B800000018
LOG: (16490/16492) 00000009000010B800000019
LOG: (16491/16492) 00000009000010B80000001A
LOG: (16492/16492) 00000009000010B80000001B
LOG: backup 2024-02-24 01:35:25 is valid
INFO: restore complete. Recovery starts automatically when the PostgreSQL server is started.

4.进入新的 data目录
 cd /postgresql/hq/bk/restore/2023_pg_restore/iobs_new_lxm2
 cp postgresql.conf postgresql.conf_bak_`date +%F`
 cp recovery.conf recovery.conf_bak_`date +%F`
 vim recovery.conf 
将 recovery_target_timeline 值改为 'latest'

vim postgresql.conf 修改里面的相关路径
包括如下配置
-bash-4.2$ diff postgresql.conf postgresql.conf_bak_20240311 
2,3c2,3
< data_directory				    ='/postgresql/hq/bk/restore/2023_pg_restore/iobs_new_lxm2'
< hba_file					    ='/postgresql/hq/bk/restore/2023_pg_restore/iobs_new_lxm2/pg_hba.conf'
---
> data_directory				    ='/postgresql/pg5472/data'
> hba_file					    ='/postgresql/pg5472/data/pg_hba.conf'
7c7
< listen_addresses				   = '*'
---
> listen_addresses				   = '10.33.45.110'
57,58c57,58
< #archive_command = 'test ! -f /postgresql/pgbackup/iobs/archlog/%f && cp %p /postgresql/pgbackup/iobs/archlog/%f'
< archive_command = 'test ! -f /postgresql/hq/bk/restore/2023_pg_restore/iobs_new_arc_log/%f && cp %p /postgresql/hq/bk/restore/2023_pg_restore/iobs_new_arc_log/%f'
---
> archive_command = 'test ! -f /postgresql/pgbackup/iobs/archlog/%f && cp %p /postgresql/pgbackup/iobs/archlog/%f'
> 
67c67
< log_directory = '/postgresql/hq/bk/restore/2023_pg_restore/iobs_new_lxm2/pg_log'
---
> log_directory = '/postgresql/pg5472/data/pg_log'
-bash-4.2$



5. 启动数据库
postmaster -D /postgresql/hq/bk/restore/2023_pg_restore/iobs_new_lxm2 &

6. 登录(根据配置文件中 listen_port 为 5472
-bash-4.2$ psql -p 5472
psql (9.4.14)
Type "help" for help.

postgres=# show data_directory ;
                  data_directory                   
---------------------------------------------------
 /postgresql/hq/bk/restore/2023_pg_restore/iobs_new_lxm2
(1 row)


http://www.niftyadmin.cn/n/5460454.html

相关文章

【JVM】JVM常用性能调优参数详细介绍

JVM常用性能调优参数详细介绍 一、何时进行JVM调优二、性能调优三、JVM调优的基本原则四、JVM调优目标五、JVM调优的步骤六、JVM参数七、JVM参数解析及调优八、JVM参数使用手册8.1 内存相关8.2 GC策略相关8.3 GC日志相关8.4 异常相关8.5 问题定位及优化相关九、参考文档一、何时…

刷爆LeetCode:两数之和 【1/1000 第一题】

&#x1f464;作者介绍&#xff1a;10年大厂数据\经营分析经验&#xff0c;现任大厂数据部门负责人。 会一些的技术&#xff1a;数据分析、算法、SQL、大数据相关、python 作者专栏每日更新&#xff1a;LeetCode解锁1000题: 打怪升级之旅https://blog.csdn.net/cciehl/category…

python——遍历网卡并禁用/启用

一、遍历网卡 注意&#xff1a;只能遍历到启用状态的网卡&#xff0c;如果网卡是禁止状态&#xff0c;则遍历不到&#xff01;&#xff01;&#xff01; import os import time import psutil import loggingdef get_multi_physical_network_card():physical_nic_list []try:…

pytorch | with torch.no_grad()

1.关于with with 是python中上下文管理器&#xff0c;简单理解&#xff0c;当要进行固定的进入&#xff0c;返回操作时&#xff0c;可以将对应需要的操作&#xff0c;放在with所需要的语句中。比如文件的写入&#xff08;需要打开关闭文件&#xff09;等。 以下为一个文件写入…

打工人神器! Raccoon 代码小浣熊

继这三个之后&#xff0c;今天又来了一个 [ Raccoon代码小浣熊 ] 核心精要与产品特点 全面支持多种编程语言和IDE&#xff1a;「代码小浣熊」支持超过90种主流编程语言&#xff0c;包括但不限于Python、Java、JavaScript、C、Go和SQL等。同时&#xff0c;它集成了市面上主流的…

LATTICE进阶篇DDR2--(0)获取ddr2 IP核

前言 想要仿真lattice的DDR2由来已久&#xff0c;但苦于对其了解甚少&#xff0c;在查阅过很多资料后&#xff0c;终于对这个IP核的仿真有了一些了解。 现做一些总结&#xff0c;以备不时之需&#xff0c;也让有需要的朋友&#xff0c;少走一些弯路。 环境&#xff1a;win10…

蓝桥杯23年第十四届省赛真题-三国游戏|贪心,sort函数排序

题目链接&#xff1a; 1.三国游戏 - 蓝桥云课 (lanqiao.cn) 蓝桥杯2023年第十四届省赛真题-三国游戏 - C语言网 (dotcpp.com) 虽然这道题不难&#xff0c;很容易想到&#xff0c;但是这个视频的思路理得很清楚&#xff1a; [蓝桥杯]真题讲解&#xff1a;三国游戏&#xff0…

ubuntu20.04安装截图工具flameshot

ubuntu20.04 自带的截图工具&#xff0c;可以使用快捷键“shift printScreen” ,但是它不能对截图进行编辑。 现在安装截图工具 flameshot&#xff0c;使用以下命令&#xff1a; sudo apt install flameshot 安装完成后&#xff0c;使用以下命令打开&#xff1a; flamesho…