分类目录归档:pdu工具

Postgres数据库truncate表无有效备份恢复

创建一个Postgres表,并插入数据

postgres=# CREATE TABLE "PeisInterfaceLog"(
postgres(#         "PeisInterfaceLogId" text,
postgres(#         "PeisDepartmentId" text,
postgres(#         "PeisDepartmentName" text,
postgres(#         "PeisInterfaceSubjectType" text,
postgres(#         "PeisInterfaceSubjectId" text,
postgres(#         "PeisInterfaceNo" text,
postgres(#         "PeisInterfaceName" text,
postgres(#         "PeisInterfaceDirection" text,
postgres(#         "PeisInterfaceCallAddress" text,
postgres(#         "PeisInterfaceLogStartTime" timestamp,
postgres(#         "PeisInterfaceInputContent" json,
postgres(#         "PeisInterfaceInputTranscodeContent" json,
postgres(#         "PeisInterfaceOutContent" json,
postgres(#         "PeisInterfaceOutTranscodeContent" json,
postgres(#         "PeisInterfaceSuccessSign" int4,
postgres(#         "PeisInterfaceLogStatusCode" text,
postgres(#         "PeisInterfaceLogNote" text,
postgres(#         "PeisInterfaceLogTimestamp" timestamp,
postgres(#         "PeisInterfaceLogInfo" text,
postgres(#         "PeisPatientRegisterId" text
postgres(# );
CREATE TABLE
postgres=# \i /postgres/COPY/public_copy.sql 
SET
COPY 722957
postgres=# select count(1) from "PeisInterfaceLog";
 count  
--------
 722957
(1 row)

验证truncate操作,引起Postgres中oid的变化

postgres=# checkpoint;
CHECKPOINT
postgres=#  show data_directory;
 data_directory 
----------------
 /pgdata
(1 row)
postgres=# select oid, datname from pg_database ;
  oid  |  datname  
-------+-----------
 13676 | postgres
     1 | template1
 13675 | template0
(3 rows)
postgres=# select relname, relowner, relfilenode from pg_class where relowner = 10 and relname like '%PeisInterfaceLog%';
     relname      | relowner | relfilenode 
------------------+----------+-------------
 PeisInterfaceLog |       10 |       16384
(1 row)

postgres=# truncate table "PeisInterfaceLog";
TRUNCATE TABLE
postgres=# select count(1) from  "PeisInterfaceLog";
 count 
-------
     0
(1 row)

postgres=# select relname, relowner, relfilenode from pg_class where relowner = 10 and relname like '%PeisInterfaceLog%';
     relname      | relowner | relfilenode 
------------------+----------+-------------
 PeisInterfaceLog |       10 |       16394
(1 row)

使用工具进行初始化字典信息

PDU.public=# b;

开始初始化...
 -pg_database:</pgdata/global/1262>

数据库:postgres 
      -pg_schema:</pgdata/base/13676/2615>
      -pg_class:</pgdata/base/13676/1259> 共88行
      -pg_attribute:</pgdata/base/13676/1249> 共2950行
      模式:
        ▌ public 1张表

PDU.public=# use postgres;

┌────────────────────────────────────────┐
│          模式             │  表数量    │
├────────────────────────────────────────┤
│    public                 │  1         │
└────────────────────────────────────────┘
postgres.public=# set public;

┌──────────────────────────────────────────────────┐
│               表名                  │  表大小    │
├──────────────────────────────────────────────────┤
│    PeisInterfaceLog                 │  0         │
└──────────────────────────────────────────────────┘
        仅显示表大小排名前 1 的表名
postgres.public=# \d PeisInterfaceLog;

┌──────────────────────────────────────────────────────────────┐
│                            列类型                            │
└──────────────────────────────────────────────────────────────┘
text,text,text,text,text,text,text,text,text,timestamp,json,json,json,json,int4,text,text,timestamp,text,text

配置软件磁盘扫描操作(pdu.ini中配置)

#dropScan需要扫描的磁盘
DISK_PATH=/data/test.dd
#dropScan时跳跃的数据块数量,数值越小覆盖磁盘越全面,速度越慢
BLOCK_INTERVAL=5

启用磁盘扫描操作

PDU.public=# p idxmode off;

┌─────────────────────────────────────────────────────────────────┐
│              参数                │             当前值           │
├─────────────────────────────────────────────────────────────────┤
│    startwal                      │                              │
│    endwal                        │                              │
│    startlsnt                     │                              │
│    endlsnt                       │                              │
│    starttime                     │                              │
│    endtime                       │                              │
│    resmode(Data Restore Mode)    │              TIME            │
│    exmode(Data Export Mode)      │              CSV             │
│    encoding                      │              UTF8            │
│    restype(Data Restore Type)    │              DELETE          │
          ----------------------DropScan----------------------
│    dsoff(DropScan startOffset)   │              0               │
│    blkiter(Block Intervals)      │              5               │
│    itmpcsv(Items Per Csv)        │              100             │
│    idxmode                       │              off             │
└─────────────────────────────────────────────────────────────────┘
PDU.public=# ds;

 ▌全量扫描恢复模式 

 ▌数据文件扫描 
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
          表名           │                                   结果                                    
├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 PeisInterfaceLog           99.976 %(21469593600)   数据页: 71158      成功: 722947    (疑似乱码: 2809      ) 失败: 0 

耗时 15.28 秒
└────────────────────────────────────────────────────────────────────────────────────────────────────┘
扫描完毕,文件目录如下: 
        restore/dropscan/PeisInterfaceLog
PDU.public=# ds copy;
已导出: 
/restore/dropscan/PeisInterfaceLog/COPY.sql
PDU.public=# 

[root@xifenfei PeisInterfaceLog]# more COPY.sql 
[root@xifenfei PeisInterfaceLog]# cat /restore/dropscan/PeisInterfaceLog/COPY.sql
COPY PeisInterfaceLog FROM '/restore/dropscan/PeisInterfaceLog/09-28-21:29:55_226738176_32760blks_336787items.csv';
COPY PeisInterfaceLog FROM '/restore/dropscan/PeisInterfaceLog/09-28-21:30:01_595968000_32767blks_330416items.csv';
COPY PeisInterfaceLog FROM '/restore/dropscan/PeisInterfaceLog/09-28-21:30:04_1116061696_5631blks_55744items.csv';

把恢复数据导入到Postgres数据中

[root@xifenfei ~]# su - postgres 
[postgres@xifenfei ~]$ psql
psql (12.8)
Type "help" for help.

postgres=# \i /restore/dropscan/PeisInterfaceLog/COPY.sql
COPY 336787
COPY 330416
COPY 55744
postgres=# select count(1) from "PeisInterfaceLog";
 count  
--------
 722947
(1 row)

经过上述扫描测试证明该工具实现了在Postgres中truncate数据的绝大部分数据恢复(这里的乱码不是由于没有扫描到数据,主要是由于个别字符串由于类型判断关系识别不对导致乱码抛弃).
如果你遇到Postgres 数据库由于drop/truncate等误操作,而且无有效备份进行恢复,面临数据丢失风险,请第一时间保护现场(数据文件所在分区尽可能不要有写入操作),联系我们提供专业恢复技术支持:
电话/微信:17813235971    Q Q:107644445QQ咨询惜分飞    E-Mail:dba@xifenfei.com

发表在 pdu工具, PostgreSQL恢复 | 标签为 , | 评论关闭

pdu完美支持金仓数据库恢复(KingbaseES)

最近朋友对pdu进行了一些完善,让其可以比较好的支持电科金仓数据库(KingbaseES)的v8/v9版本
使用pdu直接unload数据

[kingbase@xifenfei ~]$ isql -Usystem test
Password for user system: 
isql (V9.0)
Licesen Type: SALES-企业版.
Type "help" for help.

test=# select version();
         version         
-------------------------
 KingbaseES V009R002C010
(1 row)

test=# \dt
        List of relations
 Schema | Name  | Type  | Owner  
--------+-------+-------+--------
 public | t_xff | table | system
(1 row)

test=# drop table walminer_contents;
DROP TABLE
test=# select * from t_xff;
 id |       name       
----+------------------
  1 | 汉字
  2 | xff测试
  3 | 惜分飞测试
  4 | www.xifenfei.com
(4 rows)

test=# insert into t_xff values(5,'www.xifenfie.com
test'# www.orasos.com');
INSERT 0 1
test=# select * from t_xff;
 id |       name       
----+------------------
  1 | 汉字
  2 | xff测试
  3 | 惜分飞测试
  4 | www.xifenfei.com
  5 | www.xifenfie.com+
    | www.orasos.com
(5 rows)

test=# checkpoint;
CHECKPOINT

pdu直接unload恢复

test.public=# b; 
开始初始化...
 -pg_database:</data/kingbase/v9/data/global/1262>

数据库:test 
      -pg_schema:</data/kingbase/v9/data/base/13856/2615>
      -pg_class:</data/kingbase/v9/data/base/13856/1259> 共132行
      -pg_attribute:</data/kingbase/v9/data/base/13856/1249> 共8863行
      模式:
        -->public 1张表

数据库:kingbase 
      -pg_schema:</data/kingbase/v9/data/base/13857/2615>
      -pg_class:</data/kingbase/v9/data/base/13857/1259> 共131行
      -pg_attribute:</data/kingbase/v9/data/base/13857/1249> 共8855行
      模式:
        -->public 0张表
PDU.public=# use test;    
|----------------------------------------|
|          模式             |  表数量    |
|----------------------------------------|
|    public                 |  1         |
|----------------------------------------|
test.public=# set public;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_xff                            |  8.00 KB   |
|--------------------------------------------------|

        仅显示表大小排名前 1 的表名
test.public=# param exmode sql;
-------------------------------------------------------------------
|              参数                |             当前值           |
-------------------------------------------------------------------
|    startwal                      |                              |
|    endwal                        |                              |
|    starttime                     |                              |
|    endtime                       |                              |
|    resmode(Data Restore Mode)    |              TX              |
|    exmode(Data Export Mode)      |              SQL             |
------------------------------------------------------------------
test.public=# unload tab t_xff;
正在解析表 <t_xff>. 已解析数据页: 1, 已解析数据: 5 条
表名<t_xff>-</data/kingbase/v9/data/base/13856/16384> 解析完成, 1 个数据页 ,共计 5 条数据. 成功 5 条; 失败 0 条 
COPY文件路径为:<test/public/t_xff.sql>

test.public=# unload ddl;

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

查看恢复结果,证明可以完美恢复金仓中表的数据

[root@xifenfei pdu]# cat test/public/t_xff.sql
INSERT INTO t_xff VALUES(1,'汉字');
INSERT INTO t_xff VALUES(2,'xff测试');
INSERT INTO t_xff VALUES(3,'惜分飞测试');
INSERT INTO t_xff VALUES(4,'www.xifenfei.com');
INSERT INTO t_xff VALUES(5,'www.xifenfie.com
www.orasos.com');
[root@xifenfei pdu]# cat test/DDL/public_ddl.sql
CREATE SCHEMA public;
set search_path to public;
CREATE TABLE t_xff(
        id int,
        name varchar
);

pdu恢复金仓数据库delete数据
删除数据尝试恢复

test=# delete from t_xff where id in(4,2);
DELETE 2
test=# checkpoint;
CHECKPOINT
test=# select * from t_xff;
 id |       name       
----+------------------
  1 | 汉字
  3 | 惜分飞测试
  5 | www.xifenfie.com+
    | www.orasos.com
(3 rows)

使用pdu恢复被删除数据

PDU.public=# use test;
|----------------------------------------|
|          模式             |  表数量    |
|----------------------------------------|
|    public                 |  1         |
|----------------------------------------|
test.public=# set public;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_xff                            |  8.00 KB   |
|--------------------------------------------------|

        仅显示表大小排名前 1 的表名
test.public=# scan t_xff;

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

开始扫描归档目录
|-起始文件<000000010000000000000005>
|-终点文件<000000010000000000000009>

正在扫描中

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

读取到最后的日志段: 0/504EC68: 


扫描结束,当前扫描的时间段为
|-开始时间:2025-04-06 19:13:15.280234 CST
|-结束时间:2025-04-08 01:45:59.493354 CST

---------------------------------------------------------------------------------------------------------
 时间戳:2025-04-08 01:45:59.493354 CST | 事务号:1120 | 数据文件oid:16384 | toast文件oid:0 |待恢复条数:2 
---------------------------------------------------------------------------------------------------------
test.public=# restore del 1120;
开始扫描归档目录
|-起始文件<000000010000000000000005>
|-终点文件<000000010000000000000009>

正在扫描中
读取到最后的日志段: 0/504EC68: 
|-已解析出2条数据读取到最后的日志段: 0/504EC68: 


━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
解析结果:成功 2, 失败 0 (即对应FPW未找到), 共计2条数据
恢复出的csv文件路径为<restore/public/t_xff_1120.csv>
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

扫描结束,当前扫描的时间段为
|-开始时间:2025-04-06 19:13:15.280234 CST
|-结束时间:2025-04-08 01:45:59.493354 CST

restore完成
test.public=# exit;
[root@xifenfei pdu]# cat restore/public/t_xff_1120.csv
2       xff测试
4       www.xifenfei.com

通过上述测试,证明pdu工具可以完美的恢复金仓中刚刚被删除的delete数据.

发表在 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工具 | 标签为 , | 评论关闭