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

相关文章

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

Python自动化处理PDF文档的操作完整指南

《Python自动化处理PDF文档的操作完整指南》在办公自动化中,PDF文档处理是一项常见需求,本文将介绍如何使用Python实现PDF文档的自动化处理,感兴趣的小伙伴可以跟随小编一起学习一下... 目录使用pymupdf读写PDF文件基本概念安装pymupdf提取文本内容提取图像添加水印使用pdfplum

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

基于Redis自动过期的流处理暂停机制

《基于Redis自动过期的流处理暂停机制》基于Redis自动过期的流处理暂停机制是一种高效、可靠且易于实现的解决方案,防止延时过大的数据影响实时处理自动恢复处理,以避免积压的数据影响实时性,下面就来详... 目录核心思路代码实现1. 初始化Redis连接和键前缀2. 接收数据时检查暂停状态3. 检测到延时过

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库