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

相关文章

Ubuntu 22.04 服务器安装部署(nginx+postgresql)

《Ubuntu22.04服务器安装部署(nginx+postgresql)》Ubuntu22.04LTS是迄今为止最好的Ubuntu版本之一,很多linux的应用服务器都是选择的这个版本... 目录是什么让 Ubuntu 22.04 LTS 变得安全?更新了安全包linux 内核改进一、部署环境二、安装系统

MySQL InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据

《MySQLInnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据》mysql的ibdata文件被误删、被恶意修改,没有从库和备份数据的情况下的数据恢复,不能保证数据库所有表数据... 参考:mysql Innodb表空间卸载、迁移、装载的使用方法注意!此方法只适用于innodb_fi

Gin框架中的GET和POST表单处理的实现

《Gin框架中的GET和POST表单处理的实现》Gin框架提供了简单而强大的机制来处理GET和POST表单提交的数据,通过c.Query、c.PostForm、c.Bind和c.Request.For... 目录一、GET表单处理二、POST表单处理1. 使用c.PostForm获取表单字段:2. 绑定到结

mysql通过frm和ibd文件恢复表_mysql5.7根据.frm和.ibd文件恢复表结构和数据

《mysql通过frm和ibd文件恢复表_mysql5.7根据.frm和.ibd文件恢复表结构和数据》文章主要介绍了如何从.frm和.ibd文件恢复MySQLInnoDB表结构和数据,需要的朋友可以参... 目录一、恢复表结构二、恢复表数据补充方法一、恢复表结构(从 .frm 文件)方法 1:使用 mysq

mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespace id不一致处理

《mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespaceid不一致处理》文章描述了公司服务器断电后数据库故障的过程,作者通过查看错误日志、重新初始化数据目录、恢复备... 周末突然接到一位一年多没联系的妹妹打来电话,“刘哥,快来救救我”,我脑海瞬间冒出妙瓦底,电信火苲马扁.

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

Python自动化处理手机验证码

《Python自动化处理手机验证码》手机验证码是一种常见的身份验证手段,广泛应用于用户注册、登录、交易确认等场景,下面我们来看看如何使用Python自动化处理手机验证码吧... 目录一、获取手机验证码1.1 通过短信接收验证码1.2 使用第三方短信接收服务1.3 使用ADB读取手机短信1.4 通过API获取

Python自动化Office文档处理全攻略

《Python自动化Office文档处理全攻略》在日常办公中,处理Word、Excel和PDF等Office文档是再常见不过的任务,手动操作这些文档不仅耗时耗力,还容易出错,幸运的是,Python提供... 目录一、自动化处理Word文档1. 安装python-docx库2. 读取Word文档内容3. 修改

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

使用Python实现批量分割PDF文件

《使用Python实现批量分割PDF文件》这篇文章主要为大家详细介绍了如何使用Python进行批量分割PDF文件功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、架构设计二、代码实现三、批量分割PDF文件四、总结本文将介绍如何使用python进js行批量分割PDF文件的方法