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

相关文章

Oracle迁移PostgreSQL隐式类型转换配置指南

《Oracle迁移PostgreSQL隐式类型转换配置指南》Oracle迁移PostgreSQL时因类型差异易引发错误,需通过显式/隐式类型转换、转换关系管理及冲突处理解决,并配合验证测试确保数据一致... 目录一、问题背景二、解决方案1. 显式类型转换2. 隐式转换配置三、维护操作1. 转换关系管理2.

Python调用LibreOffice处理自动化文档的完整指南

《Python调用LibreOffice处理自动化文档的完整指南》在数字化转型的浪潮中,文档处理自动化已成为提升效率的关键,LibreOffice作为开源办公软件的佼佼者,其命令行功能结合Python... 目录引言一、环境搭建:三步构建自动化基石1. 安装LibreOffice与python2. 验证安装

shell脚本批量导出redis key-value方式

《shell脚本批量导出rediskey-value方式》为避免keys全量扫描导致Redis卡顿,可先通过dump.rdb备份文件在本地恢复,再使用scan命令渐进导出key-value,通过CN... 目录1 背景2 详细步骤2.1 本地docker启动Redis2.2 shell批量导出脚本3 附录总

批量导入txt数据到的redis过程

《批量导入txt数据到的redis过程》用户通过将Redis命令逐行写入txt文件,利用管道模式运行客户端,成功执行批量删除以Product*匹配的Key操作,提高了数据清理效率... 目录批量导入txt数据到Redisjs把redis命令按一条 一行写到txt中管道命令运行redis客户端成功了批量删除k

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

Python实现批量提取BLF文件时间戳

《Python实现批量提取BLF文件时间戳》BLF(BinaryLoggingFormat)作为Vector公司推出的CAN总线数据记录格式,被广泛用于存储车辆通信数据,本文将使用Python轻松提取... 目录一、为什么需要批量处理 BLF 文件二、核心代码解析:从文件遍历到数据导出1. 环境准备与依赖库

Spring Boot 中的默认异常处理机制及执行流程

《SpringBoot中的默认异常处理机制及执行流程》SpringBoot内置BasicErrorController,自动处理异常并生成HTML/JSON响应,支持自定义错误路径、配置及扩展,如... 目录Spring Boot 异常处理机制详解默认错误页面功能自动异常转换机制错误属性配置选项默认错误处理

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

linux批量替换文件内容的实现方式

《linux批量替换文件内容的实现方式》本文总结了Linux中批量替换文件内容的几种方法,包括使用sed替换文件夹内所有文件、单个文件内容及逐行字符串,强调使用反引号和绝对路径,并分享个人经验供参考... 目录一、linux批量替换文件内容 二、替换文件内所有匹配的字符串 三、替换每一行中全部str1为st