PostgreSQL恢复系列:pg_filedump批量处理---惜分飞

2024-04-20 08:12

本文主要是介绍PostgreSQL恢复系列:pg_filedump批量处理---惜分飞,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

pg_filedump工具使用起来比较麻烦,主要存在问题:
1. 需要人工一个个枚举各个列类型无法实现批量恢复,参考以前写的PostgreSQL恢复系列:pg_filedump基本使用
2. 特别是在pg库无法正常运行的情况下,如果没有业务提供表创建语句,恢复基本上无法正常进行.
基于这两个问题,在以前的文章中写过PostgreSQL恢复系列:pg_filedump恢复字典构造,为了解决上述的两个,弄了一个pg_filedump_batch脚本实现批量恢复需求

在测试的pg库中创建了一些测试表,并查看部分表数据,便于对比后续恢复效果

postgres=# \d

             List of relations

 Schema |      Name      | Type  |  Owner  

--------+----------------+-------+----------

 public | t_tbs          | table | postgres

 public | t_xff          | table | postgres

 public | t_xff2         | table | postgres

 public | t_xff3         | table | postgres

 public | t_xff4         | table | postgres

 public | t_xifenfei     | table | postgres

 public | tab_attribute  | table | postgres

 public | tab_class      | table | postgres

 public | tab_database   | table | postgres

 public | tab_namespace  | table | postgres

 public | tab_tablespace | table | postgres

 public | tab_type       | table | postgres

(12 rows)

postgres=# select * from tab_database;

  oid  |   datname   | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoi

d | datfrozenxid | datminmxid | dattablespace

-------+-------------+--------+----------+-------------+-------------+---------------+--------------+--------------+-------------

--+--------------+------------+---------------

 14187 | postgres    |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         1418

6 |          479 |          1 |          1663

 16403 | db_xff      |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         1418

6 |          479 |          1 |          1663

     1 | template1   |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | t             | t            |           -1 |         1418

6 |          479 |          1 |          1663

 14186 | template0   |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | t             | f            |           -1 |         1418

6 |          479 |          1 |          1663

 16407 | db_xifenfei |  16405 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         1418

6 |          479 |          1 |         16406

(5 rows)

postgres=# select count(1) from tab_class;

 count

-------

   407

(1 row)

postgres=# select *from pg_tablespace;

  oid  |   spcname    | spcowner | spcacl | spcoptions

-------+--------------+----------+--------+------------

  1663 | pg_default   |       10 |        |

  1664 | pg_global    |       10 |        |

 16406 | tbs_xifenfei |    16405 |        |

(3 rows)

使用pg_filedump_bath脚本来实现批量恢复

[root@xifenfei tmp]# ./pg_filedump_batch recover --database-oid=14187  \

 --output-directory=/data/recovery --pgdata=/var/lib/pgsql/12/data

Recover tables in database with oid: 14187

LOG: starting to process table tab_attribute

LOG: starting to process table tab_class

LOG: starting to process table tab_database

LOG: starting to process table tab_namespace

LOG: starting to process table tab_tablespace

LOG: starting to process table tab_type

LOG: starting to process table t_tbs

LOG: starting to process table t_xff

LOG: starting to process table t_xff2

LOG: starting to process table t_xff3

LOG: starting to process table t_xff4

LOG: starting to process table t_xifenfei

Check dumps in /data/recovery

参考数据恢复

[root@xifenfei tmp]# cd /data/recovery/

[root@xifenfei recovery]# ls -ltr

total 156

-rw-r--r-- 1 root root 82797 Apr 18 20:35 recovered-14187-tab_attribute.csv

-rw-r--r-- 1 root root 31129 Apr 18 20:35 recovered-14187-tab_class.csv

-rw-r--r-- 1 root root   343 Apr 18 20:35 recovered-14187-tab_database.csv

-rw-r--r-- 1 root root   118 Apr 18 20:35 recovered-14187-tab_namespace.csv

-rw-r--r-- 1 root root    50 Apr 18 20:35 recovered-14187-tab_tablespace.csv

-rw-r--r-- 1 root root  7907 Apr 18 20:35 recovered-14187-tab_type.csv

-rw-r--r-- 1 root root     0 Apr 18 20:35 recovered-14187-t_tbs.csv

-rw-r--r-- 1 root root    38 Apr 18 20:35 recovered-14187-t_xff.csv

-rw-r--r-- 1 root root    38 Apr 18 20:35 recovered-14187-t_xff2.csv

-rw-r--r-- 1 root root    38 Apr 18 20:35 recovered-14187-t_xff3.csv

-rw-r--r-- 1 root root    38 Apr 18 20:35 recovered-14187-t_xff4.csv

-rw-r--r-- 1 root root    38 Apr 18 20:35 recovered-14187-t_xifenfei.csv

[root@xifenfei recovery]# cat recovered-14187-tab_database.csv

14187   postgres        10      6       en_US.UTF-8     en_US.UTF-8     f       t       -1      14186   479     1       1663

16403   db_xff  10      6       en_US.UTF-8     en_US.UTF-8     f       t       -1      14186   479     1       1663

1       template1       10      6       en_US.UTF-8     en_US.UTF-8     t       t       -1      14186   479     1       1663

14186   template0       10      6       en_US.UTF-8     en_US.UTF-8     t       f       -1      14186   479     1       1663

16407   db_xifenfei     16405   6       en_US.UTF-8     en_US.UTF-8     f       t       -1      14186   479     1       16406

[root@xifenfei recovery]# cat recovered-14187-tab_class.csv|wc -l

407

[root@xifenfei recovery]# cat recovered-14187-tab_tablespace.csv

1663    pg_default

1664    pg_global

16406   tbs_xifenfei

把pg_class恢复数据导入库中进行对比,证明恢复的数据完全正确

postgres=# COPY tab_class_new FROM '/data/recovery/recovered-14187-tab_class.csv';

COPY 407

postgres=# select count(1) from tab_class;

 count

-------

   407

(1 row)

 count

-------

   407

(1 row)

postgres=# select count(1) from tab_class_new;

 count

-------

   407

(1 row)

postgres=# select * from tab_class_new

postgres-# EXCEPT

postgres-# select * from tab_class;

 oid | relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | rel

allvisible | reltoastrelid | relhasindex | relisshared | relpersistence | relkind

-----+---------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+----

-----------+---------------+-------------+-------------+----------------+---------

(0 rows)

postgres=# select * from tab_class

postgres-# EXCEPT

postgres-# select * from tab_class_new;

 oid | relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | rel

allvisible | reltoastrelid | relhasindex | relisshared | relpersistence | relkind

-----+---------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+----

-----------+---------------+-------------+-------------+----------------+---------

(0 rows)

通过上述操作证明:
1. 在没有人工列出列类型的情况下实现批量pg_filedump恢复功能
2. 在pg库没有启动的情况下直接解析字典实现恢复功能
3. 实现pg数据库的批量恢复
如果有PostgreSQL的数据库故障,自行无法解决,请联系我们提供专业数据库恢复技术支持

这篇关于PostgreSQL恢复系列:pg_filedump批量处理---惜分飞的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/919729

相关文章

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

SpringSecurity中的跨域问题处理方案

《SpringSecurity中的跨域问题处理方案》本文介绍了跨域资源共享(CORS)技术在JavaEE开发中的应用,详细讲解了CORS的工作原理,包括简单请求和非简单请求的处理方式,本文结合实例代码... 目录1.什么是CORS2.简单请求3.非简单请求4.Spring跨域解决方案4.1.@CrossOr

pandas批量拆分与合并Excel文件的实现示例

《pandas批量拆分与合并Excel文件的实现示例》本文介绍了Pandas中基于整数位置的iloc和基于标签的loc方法进行数据索引和切片的操作,并将大Excel文件拆分合并,具有一定的参考价值,感... 目录一、Pandas 进行索引和切编程片的iloc、loc方法二、Pandas批量拆分与合并Exce

requests处理token鉴权接口和jsonpath使用方式

《requests处理token鉴权接口和jsonpath使用方式》文章介绍了如何使用requests库进行token鉴权接口的处理,包括登录提取token并保存,还详述了如何使用jsonpath表达... 目录requests处理token鉴权接口和jsonpath使用json数据提取工具总结reques

MySQL 批量插入的原理和实战方法(快速提升大数据导入效率)

《MySQL批量插入的原理和实战方法(快速提升大数据导入效率)》在日常开发中,我们经常需要将大量数据批量插入到MySQL数据库中,本文将介绍批量插入的原理、实现方法,并结合Python和PyMySQ... 目录一、批量插入的优势二、mysql 表的创建示例三、python 实现批量插入1. 安装 PyMyS

C# 空值处理运算符??、?. 及其它常用符号

《C#空值处理运算符??、?.及其它常用符号》本文主要介绍了C#空值处理运算符??、?.及其它常用符号,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、核心运算符:直接解决空值问题1.??空合并运算符2.?.空条件运算符二、辅助运算符:扩展空值处理

浅析Python中如何处理Socket超时

《浅析Python中如何处理Socket超时》在网络编程中,Socket是实现网络通信的基础,本文将深入探讨Python中如何处理Socket超时,并提供完整的代码示例和最佳实践,希望对大家有所帮助... 目录开篇引言核心要点逐一深入讲解每个要点1. 设置Socket超时2. 处理超时异常3. 使用sele

SpringMVC配置、映射与参数处理​入门案例详解

《SpringMVC配置、映射与参数处理​入门案例详解》文章介绍了SpringMVC框架的基本概念和使用方法,包括如何配置和编写Controller、设置请求映射规则、使用RestFul风格、获取请求... 目录1.SpringMVC概述2.入门案例①导入相关依赖②配置web.XML③配置SpringMVC