分类目录归档:pdu工具

pg删除数据恢复—pdu恢复pg delete数据

误操作,在应用端选择每页50条记录然后点击删除,类似这样误操作删除了近10000条记录,现在希望对其进行恢复.对于这种表被误删除数据的,可以使用pdu进行解析wal然后进行恢复.
1. 配置pdu.ini指定wal日志路径

PGDATA=/data/pg/16/data
ARCHIVE_DEST=/data/arch

2. 初始化字典信息

PDU.public=# b;
开始初始化...
 -pg_database:</data/pg/16/data/global/1262>
    【postgres】
      -pg_schema:</data/pg/16/data/base/5/2615>
      -pg_class:</data/pg/16/data/base/5/1259>,共86行
      -pg_attribute:</data/pg/16/data/base/5/1249>,共3273行
      模式:
        -->public,2张表

3. 关于删除恢复相关帮助

**误删数据恢复**
scan t1;                                │ 扫描被误删的表
restore del <Tx Number>;                │ 通过 事务号 恢复被误删的数据
restore del all;                        │ 通过 时间区间 恢复被误删的数据
--------------------------------------------------------------------------------------------------
add <filenode> <tablename> <attibutes>; │ 将特定表信息手动添加到restore库中
例如: <add 12345 t1 varchar,varchar,timestamp,varchar,numeric,varchar,varchar,varchar,numeric;>
--------------------------------------------------------------------------------------------------
restore db <dbname> <DB Path>;          │ 初始化特定的数据库目录 (例: restore db xmandb /home/postgres/data/base/290113;)

**参数设置**
param startwal 000000010000000800000008;│ 设置scan扫描的起始WAL文件,如果未设置则默认是归档目录的第一个文件
param endwal 000000010000000800000009;  │ 设置scan扫描的结束WAL文件,如果未设置则默认是归档目录的最后一个文件
param resmode tx|time;                  │ 设置restore恢复的模式,选择按照【事务号/TX】或【时间区间/TIME】进行恢复
param starttime 2025-01-01_00:00:00;    │ 设置scan扫描的起始时间 
param endtime 2025-01-01_00:00:00;      │ 设置scan扫描的结束时间 
reset <param name>                      │ 重置某个参数 
show;                                   │ 查看当前所有参数

4.可以通过param相关参数设置扫描wal日志范围,时间,恢复模式等.我们先使用事务模式确认开始时间

postgres.public=# scan t_delete;

正在扫描表<t_delete>的删除记录...

开始扫描归档目录
|-起始文件<000000030000052500000050>
|-终点文件<0000000300000525000000EF>

正在扫描中

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
当前为 事务号恢复 模式,扫描结果将以【事务号】为单位进行输出
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━



扫描结束,当前扫描的时间段为
|-开始时间:2025-03-05 18:05:38.006348 CST
|-结束时间:2025-03-05 19:00:19.515825 CST

--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:06:20.420863 CST | 事务号:1360118630 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:38:06.749572 CST | 事务号:1360430105 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.947320 CST | 事务号:1360532435 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.948734 CST | 事务号:1360532430 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.948754 CST | 事务号:1360532434 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.948781 CST | 事务号:1360532432 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
……………………
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:56.872903 CST | 事务号:1360536441 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:57.125587 CST | 事务号:1360536457 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:36 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:56:24.931723 CST | 事务号:1360596835 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:59:38.314220 CST | 事务号:1360621790 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
postgres.public=# 

5.基于上述数据,事务情况,可以确认第一次发生删除时间为2025-03-05 18:48:41.947320,最后删除时间为:2025-03-05 18:48:57.125587,重新设置基于时间的恢复模式

postgres.public=# param starttime 2025-03-05_18:48:40; 
OK! starttime=2025-03-05_18:48:40
postgres.public=# param endtime 2025-03-05_18:49:00;
OK! endtime=2025-03-05_18:49:00
postgres.public=# param resmode time;
OK! restoreMode=time
postgres.public=# show;
---------------------------------------------------
|       参数       |             当前值           |
---------------------------------------------------
|startwal          |                              |
|endwal            |                              |
|starttime         |2025-03-05 18:48:40.000000 CST|
|endtime           |2025-03-05 18:49:00.000000 CST|
|resmode           |             TIME             |
---------------------------------------------------

6.重新扫描需要恢复表

postgres.public=# scan t_delete;

正在扫描表<t_delete>的删除记录...

开始扫描归档目录
|-起始文件<000000030000052500000050>
|-终点文件<0000000300000525000000EF>

正在扫描中

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
当前为 时间区间恢复 模式,将输出该表在时间区间内的所有删除结果
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━



扫描结束,当前扫描的时间段为
|-开始时间:2025-03-05 18:48:40.000000 CST
|-结束时间:2025-03-05 18:49:00.000000 CST

----------------------------------------------------------------------------------------------------------------------------------------------------
 开始时间:2025-03-05 18:48:40.000000 CST | 结束时间:2025-03-05 18:49:00.000000 CST | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:9636 
----------------------------------------------------------------------------------------------------------------------------------------------------
postgres.public=# 

7.确认删除的记录9636条全部被扫描出来,现在对其进行恢复

postgres.public=# restore del all; 
开始扫描归档目录
|-起始文件<000000030000052500000050>
|-终点文件<0000000300000525000000EF>

正在扫描中
|-已解析出9636条数据 

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
解析结果:成功 9636,【失败 0】(即对应FPW未找到), 共计9636条数据
恢复出的csv文件路径为<restore/public/t_delete_2025-03-05 18:48:40.000000 CST_2025-03-05 18:49:00.000000 CST.csv>
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

扫描结束,当前扫描的时间段为
|-开始时间:2025-03-05 18:48:40.000000 CST
|-结束时间:2025-03-05 18:49:00.000000 CST

restore完成
postgres.public=#

8.恢复数据导入库中

[postgres@localhost pdu]$ psql -f postgres/COPY/public_copy.sql
SET
COPY 9636

QQ20250315-235527

发表在 pdu工具 | 评论关闭

PostgreSQL表文件损坏恢复—pdu恢复损坏的表文件

在某些情况下,由于PostgreSQL表文件损坏导致无法正常访问,可以通过pdu把好的block中的数据恢复出来
准备一张测试表,里面有97条记录

his5_dms=# \d hiscrm.t_sys_oper_log;
                                            Table "hiscrm.t_sys_oper_log"
       Column       |          Type          | Collation | Nullable |Default                     
--------------------+------------------------+-----------+----------+------------
 id                 | bigint                 |           | not null | 
 module             | character varying(50)  |           |          | 
 title              | character varying(50)  |           |          | 
 alias              | character varying(50)  |           |          | 
 business_type      | integer                |           |          | 0
 method             | character varying(200) |           |          | 
 request_method     | character varying(10)  |           |          | 
 operator_type      | integer                |           |          | 0
 oper_name          | character varying(50)  |           |          | 
 dept_name          | character varying(50)  |           |          | 
 oper_url           | character varying(255) |           |          | 
 oper_ip            | character varying(50)  |           |          | 
 oper_location      | character varying(255) |           |          | 
 oper_param         | text                   |           |          | 
 json_result        | text                   |           |          | 
 status             | integer                |           |          | 0
 error_msg          | text                   |           |          | 
 oper_time          | date                   |           |          | 
 create_id          | bigint                 |           |          | 
 create_time        | bigint                 |           |          | 
 clinic_id          | bigint                 |           |          | 
 group_id           | bigint                 |           |          | 
 patient_id         | bigint                 |           |          | 
 is_patient_related | integer                |           |          | 
 business_content   | json                   |           |          | 

his5_dms=# select count(1) from hiscrm.t_sys_oper_log;
 count 
-------
    97
(1 row)

查询表对应的具体文件

his5_dms=# SELECT oid,relfilenode FROM pg_class WHERE relname='t_sys_oper_log';
  oid  | relfilenode 
-------+-------------
 16850 |       16850
(1 row)

his5_dms=# SELECT pg_relation_filepath('hiscrm.t_sys_oper_log');
 pg_relation_filepath 
----------------------
 base/16386/16850
(1 row)

his5_dms=# SHOW data_directory;
     data_directory     
------------------------
 /var/lib/pgsql/12/data
(1 row)

使用dd对文件进行破坏

[postgres@xifenfeidg ~]$ ls -l  /var/lib/pgsql/12/data/base/16386/16850
-rw-------. 1 postgres postgres 90112 Sep  5 20:26 /var/lib/pgsql/12/data/base/16386/16850
[postgres@xifenfeidg ~]$ dd if=/dev/zero of=/var/lib/pgsql/12/data/base/16386/16850 bs=512 count=1 conv=notrunc
1+0 records in
1+0 records out
512 bytes copied, 0.000158756 s, 3.2 MB/s

重启pg库

[postgres@xifenfeidg bin]$ ./pg_ctl -m fast -D /var/lib/pgsql/12/data/ stop
waiting for server to shut down.... done
server stopped
[postgres@xifenfeidg bin]$ ./pg_ctl -D /var/lib/pgsql/12/data/ start
waiting for server to start....2025-03-02 19:02:11.395 HKT [64515] LOG:  
  starting PostgreSQL 12.20 on x86_64-pc-linux-gnu, 
  compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-22), 64-bit
2025-03-02 19:02:11.396 HKT [64515] LOG:  listening on IPv6 address "::1", port 5432
2025-03-02 19:02:11.396 HKT [64515] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2025-03-02 19:02:11.396 HKT [64515] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2025-03-02 19:02:11.397 HKT [64515] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2025-03-02 19:02:11.403 HKT [64515] LOG:  redirecting log output to logging collector process
2025-03-02 19:02:11.403 HKT [64515] HINT:  Future log output will appear in directory "log".
 done
server started

查询数据报错

[postgres@xifenfeidg bin]$ psql
psql (16.8, server 12.20)
Type "help" for help.

postgres=# \c his5_dms;
psql (16.8, server 12.20)
You are now connected to database "his5_dms" as user "postgres".
his5_dms=#  select count(1) from hiscrm.t_sys_oper_log;
ERROR:  invalid page in block 0 of relation base/16386/16850

通过pdu进行恢复
跳过了坏块,把好的block中数据均恢复出来

his5_dms.hiscrm=# unload tab t_sys_oper_log;
正在解析表 <t_sys_oper_log>. 已解析数据页: 0, 已解析数据: 0 条
        |-块号0 空页面或页面已损坏,已跳过
正在解析表 <t_sys_oper_log>. 已解析数据页: 11, 已解析数据: 86 条
表名<t_sys_oper_log>-</var/lib/pgsql/12/data/base/16386/16850> 
    解析完成, 11 个数据页 ,共计 86 条数据. 成功 86 条; 失败【0】条 
COPY文件路径为:<his5_dms/hiscrm/t_sys_oper_log.csv>

导入pg库中

his5_dms=# truncate table hiscrm.t_sys_oper_log;
TRUNCATE TABLE
his5_dms=# \i his5_dms/COPY/hiscrm_copy.sql
SET
COPY 86
his5_dms=# 
发表在 pdu工具 | 标签为 , , , , , | 评论关闭

PostgreSQL恢复工具—pdu恢复单个表文件

在某些情况下,比如我们需要对单个的PostgreSQL库的表文件进行恢复(比如文件系统损坏,drop库/表,truncate表等原因,然后找到了部分oid文件),可以使用pdu对其进行完美恢复(相比pg_filedump也方便很多),具体操作步骤:
1. 由于只有单个表文件,无法获取字典信息,因此需要应用厂商/客户提供具体表创建语句

his5_dms=#    CREATE TABLE t_xff (
his5_dms(#         id                       bigint,
his5_dms(#         hospital_id              bigint,
his5_dms(#         parent_id                bigint,
his5_dms(#         disease_code             varchar(60),
his5_dms(#         disease_name             varchar(60),
his5_dms(#         type                     smallint,
his5_dms(#         py                       varchar(60),
his5_dms(#         wb                       varchar(60),
his5_dms(#         sc                       varchar(20),
his5_dms(#         order_no                 int,
his5_dms(#         state                    smallint,
his5_dms(#         create_datetime          timestamp(6),
his5_dms(#         create_id                bigint,
his5_dms(#         edit_datetime            timestamp(6),
his5_dms(#         edit_id                  bigint,
his5_dms(#         search_path              varchar(300),
his5_dms(#         diagnosis_sort           int,
his5_dms(#         category_name            varchar(40),
his5_dms(#         input_option             varchar(40),
his5_dms(#         category_class           smallint,
his5_dms(#         memo1                    varchar(300),
his5_dms(#         memo2                    varchar(300),
his5_dms(#         other_code               varchar(60),
his5_dms(#         other_name               varchar(60),
his5_dms(#         special_disease_flag     smallint
his5_dms(#    );
CREATE TABLE

2. 把oid文件pdu放到restore库中

[root@xifenfeidg public]# pwd
/tmp/pdu/restore/public
[root@xifenfeidg public]# ls -l
total 7144
-rw-r--r--. 1 root root 7315456 Mar  2 21:04 123456
[root@xifenfeidg public]# 

3. 使用add语句在pdu加载数据类型

restore.public=# add 123456 t_xff bigint,bigint,bigint,varchar,varchar,smallint,varchar,varchar,varchar,
int,smallint,timestamp,bigint,timestamp,bigint,varchar,int,varchar,varchar,
smallint,varchar,varchar,varchar,varchar,smallint;
添加完成,请用\dt;查看可unload的表
restore.public=# \dt;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_xff                            |  6.98 MB   |
|--------------------------------------------------|

        仅显示表大小排名前 1 的表名

4.使用pdu恢复表数据

restore.public=# unload t_xff;
正在解析表 <t_xff>. 已解析数据页: 893, 已解析数据: 46998 条
<t_xff>-<restore/public/123456> 解析完成, 894 个数据页 ,共计 46998 条数据. 成功 46998 条; 失败【0】条 
 COPY文件路径为:<restore/public/t_xff.csv>
restore.public=# unload COPY;

COPY命令导出完成, 文件路径: restore/COPY/public_copy.sql

5.导入数据到pg库中

his5_dms=# \i restore/COPY/public_copy.sql
SET
COPY 46998
his5_dms=# select count(1) from t_xff;
 count 
-------
 46998
(1 row)
his5_dms=# \x
Expanded display is on.
his5_dms=# select * from t_xff limit 1;
-[ RECORD 1 ]--------+---------------------------
id                   | 323839
hospital_id          | 0
parent_id            | 301
disease_code         | 57.8900x003
disease_name         | 腹腔镜下膀胱颈悬吊术
type                 | 2
py                   | fqjxpgjxds
wb                   | eeqgeeceks
sc                   | 
order_no             | 0
state                | 1
create_datetime      | 2022-09-29 15:22:58.588492
create_id            | 
edit_datetime        | 
edit_id              | 
search_path          | 301,
diagnosis_sort       | 
category_name        | 
input_option         | 
category_class       | 3
memo1                | 
memo2                | 
other_code           | 
other_name           | 
special_disease_flag | 0
发表在 pdu工具 | 标签为 , , , , | 评论关闭