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

相关文章

解决docker目录内存不足扩容处理方案

《解决docker目录内存不足扩容处理方案》文章介绍了Docker存储目录迁移方法:因系统盘空间不足,需将Docker数据迁移到更大磁盘(如/home/docker),通过修改daemon.json配... 目录1、查看服务器所有磁盘的使用情况2、查看docker镜像和容器存储目录的空间大小3、停止dock

5 种使用Python自动化处理PDF的实用方法介绍

《5种使用Python自动化处理PDF的实用方法介绍》自动化处理PDF文件已成为减少重复工作、提升工作效率的重要手段,本文将介绍五种实用方法,从内置工具到专业库,帮助你在Python中实现PDF任务... 目录使用内置库(os、subprocess)调用外部工具使用 PyPDF2 进行基本 PDF 操作使用

分析 Java Stream 的 peek使用实践与副作用处理方案

《分析JavaStream的peek使用实践与副作用处理方案》StreamAPI的peek操作是中间操作,用于观察元素但不终止流,其副作用风险包括线程安全、顺序混乱及性能问题,合理使用场景有限... 目录一、peek 操作的本质:有状态的中间操作二、副作用的定义与风险场景1. 并行流下的线程安全问题2. 顺

Ubuntu向多台主机批量传输文件的流程步骤

《Ubuntu向多台主机批量传输文件的流程步骤》:本文主要介绍在Ubuntu中批量传输文件到多台主机的方法,需确保主机互通、用户名密码统一及端口开放,通过安装sshpass工具,准备包含目标主机信... 目录Ubuntu 向多台主机批量传输文件1.安装 sshpass2.准备主机列表文件3.创建一个批处理脚

Python异常处理之避免try-except滥用的3个核心原则

《Python异常处理之避免try-except滥用的3个核心原则》在Python开发中,异常处理是保证程序健壮性的关键机制,本文结合真实案例与Python核心机制,提炼出避免异常滥用的三大原则,有需... 目录一、精准打击:只捕获可预见的异常类型1.1 通用异常捕获的陷阱1.2 精准捕获的实践方案1.3

Pandas处理缺失数据的方式汇总

《Pandas处理缺失数据的方式汇总》许多教程中的数据与现实世界中的数据有很大不同,现实世界中的数据很少是干净且同质的,本文我们将讨论处理缺失数据的一些常规注意事项,了解Pandas如何表示缺失数据,... 目录缺失数据约定的权衡Pandas 中的缺失数据None 作为哨兵值NaN:缺失的数值数据Panda

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

使用Node.js和PostgreSQL构建数据库应用

《使用Node.js和PostgreSQL构建数据库应用》PostgreSQL是一个功能强大的开源关系型数据库,而Node.js是构建高效网络应用的理想平台,结合这两个技术,我们可以创建出色的数据驱动... 目录初始化项目与安装依赖建立数据库连接执行CRUD操作查询数据插入数据更新数据删除数据完整示例与最佳

Python动态处理文件编码的完整指南

《Python动态处理文件编码的完整指南》在Python文件处理的高级应用中,我们经常会遇到需要动态处理文件编码的场景,本文将深入探讨Python中动态处理文件编码的技术,有需要的小伙伴可以了解下... 目录引言一、理解python的文件编码体系1.1 Python的IO层次结构1.2 编码问题的常见场景二

MySQL批量替换数据库字符集的实用方法(附详细代码)

《MySQL批量替换数据库字符集的实用方法(附详细代码)》当需要修改数据库编码和字符集时,通常需要对其下属的所有表及表中所有字段进行修改,下面:本文主要介绍MySQL批量替换数据库字符集的实用方法... 目录前言为什么要批量修改字符集?整体脚本脚本逻辑解析1. 设置目标参数2. 生成修改表默认字符集的语句3