分类目录归档:pdu工具

pg单个数据库目录恢复-pdu恢复单个数据库目录数据

在某些情况下,无法获取pg完整的PGDATA目录中的所有数据库文件目录和文件,只能恢复出来一个数据库目录,对于这种情况,可以通过pdu进行直接恢复,比如有一个目录16805

[postgres@localhost 16805]$ pwd
/tmp/16805
[postgres@localhost 16805]$ ls 
112        16841     2613      2682      3079      3596      4159
113        174       2615      2683      3079_fsm  3597      4160
1247       175       2615_fsm  2684      3079_vm   3598      4163
1247_fsm   2187      2615_vm   2685      3080      3599      4164
1247_vm    2224      2616      2686      3081      3600      4165
1249       2228      2616_fsm  2687      3085      3600_fsm  4166
1249_fsm   2328      2616_vm   2688      3118      3600_vm   4167
1249_vm    2336      2617      2689      3119      3601      4168
1255       2337      2617_fsm  2690      3164      3601_fsm  4169
1255_fsm   2579      2617_vm   2691      3256      3601_vm   4170
1255_vm    2600      2618      2692      3257      3602      4171
1259       2600_fsm  2618_fsm  2693      3258      3602_fsm  4172
1259_fsm   2600_vm   2618_vm   2696      3350      3602_vm   4173
1259_vm    2601      2619      2699      3351      3603      4174
13362      2601_fsm  2619_fsm  2701      3379      3603_fsm  5002
13362_fsm  2601_vm   2619_vm   2702      3380      3603_vm   548
13362_vm   2602      2620      2703      3381      3604      549
13365      2602_fsm  2650      2704      3394      3605      6102
13366      2602_vm   2651      2753      3394_fsm  3606      6104
13367      2603      2652      2753_fsm  3394_vm   3607      6106
13367_fsm  2603_fsm  2653      2753_vm   3395      3608      6110
13367_vm   2603_vm   2654      2754      3429      3609      6111
13370      2604      2655      2755      3430      3712      6112
13371      2605      2656      2756      3431      3764      6113
13372      2605_fsm  2657      2757      3433      3764_fsm  6116
13372_fsm  2605_vm   2658      2830      3439      3764_vm   6117
13372_vm   2606      2659      2831      3440      3766      6175
13375      2606_fsm  2660      2832      3455      3767      6176
13376      2606_vm   2661      2833      3456      3997      6228
13377      2607      2662      2834      3456_fsm  4143      6229
13377_fsm  2607_fsm  2663      2835      3456_vm   4144      6237
13377_vm   2607_vm   2664      2836      3466      4145      6238
13380      2608      2665      2836_fsm  3467      4146      6239
13381      2608_fsm  2666      2836_vm   3468      4147      826
1417       2608_vm   2667      2837      3501      4148      827
1418       2609      2668      2838      3502      4149      828
16806      2609_fsm  2669      2838_fsm  3503      4150      pg_filenode.map
16806_fsm  2609_vm   2670      2838_vm   3534      4151      pg_internal.init
16806_vm   2610      2673      2839      3541      4152      PG_VERSION
16809      2610_fsm  2674      2840      3541_fsm  4153
16810      2610_vm   2675      2840_fsm  3541_vm   4154
16833      2611      2678      2840_vm   3542      4155
16833_fsm  2612      2679      2841      3574      4156
16833_vm   2612_fsm  2680      2995      3575      4157
16840      2612_vm   2681      2996      3576      4158
[postgres@localhost 16805]$ 

利用pdu进行恢复

PDU.public=# restore db xifenfei /tmp/16805;
      -pg_schema:</tmp/16805/2615>
      -pg_class:</tmp/16805/1259>,共86行
      -pg_attribute:</tmp/16805/1249>,共3274行
      模式:
        -->public,2张表
PDU.public=# use xifenfei;
|----------------------------------------|
|          模式             |  表数量    |
|----------------------------------------|
|    public                 |  2         |
|----------------------------------------|
xifenfei.public=# set public;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_cas_paymentbill                |  8.24 MB   |
|    t_auth                           |  80.00 KB  |
|--------------------------------------------------|

        仅显示表大小排名前 2 的表名
xifenfei.public=# \dt;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_cas_paymentbill                |  8.24 MB   |
|    t_auth                           |  80.00 KB  |
|--------------------------------------------------|

        共计 2 张表
xifenfei.public=# unload sch public;
正在解析表 <t_cas_paymentbill>. 已解析数据页: 1055, 已解析数据: 9636 条
表名<t_cas_paymentbill>-</tmp/16805//16806> 解析完成, 1055 个数据页 ,共计 9636 条数据. 成功 9636 条; 失败【0】条 
COPY文件路径为:<xifenfei/public/t_cas_paymentbill.csv>

正在解析表 <t_auth>. 已解析数据页: 10, 已解析数据: 129 条
表名<t_auth>-</tmp/16805//16833> 解析完成, 10 个数据页 ,共计 129 条数据. 成功 129 条; 失败【0】条 
COPY文件路径为:<xifenfei/public/t_auth.csv>



模式<public>共 2 张表。成功:2, 失败【0】
日志路径:log/log/xifenfei_unload_schema_public_err.txt 

COPY命令导出完成, 文件路径: xifenfei/COPY/public_copy.sql,共找到2个csv文件

DDL导出完成. 文件路径: xifenfei/DDL/public_ddl.sql, 共计 2 张表
xifenfei.public=# 
xifenfei.public=# unload ddl;

DDL导出完成. 文件路径: xifenfei/DDL/public_ddl.sql, 共计 2 张表

通过pdu解析和恢复,该目录中一共两个业务表,均正常恢复出来,创建新库,导入数据测试

postgres=# create database xifenfei;
CREATE DATABASE
postgres=# \c xifenfei;
You are now connected to database "xifenfei" as user "postgres".
xifenfei=# \i /data/tools/pdu/xifenfei/DDL/public_ddl.sql
psql:/data/tools/pdu/xifenfei/DDL/public_ddl.sql:1: ERROR:  schema "public" already exists
SET
CREATE TABLE
CREATE TABLE
xifenfei=# \i /data/tools/pdu/xifenfei/COPY/public_copy.sql
SET
COPY 9636
COPY 129
xifenfei=# select count(1) from t_auth;
 count 
-------
   129
(1 row)

xifenfei=# select count(1) from t_cas_paymentbill;
 count 
-------
  9636
(1 row)
发表在 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工具 | 标签为 , , , , , | 留下评论