联系:手机/微信(+86 17813235971) QQ(107644445)
作者:惜分飞©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]
张晨同学开发了一个PostgreSQL数据恢复工具PDU(PDU: Postgresql Data Rescue Tool),我这边配合做一些测试
使用帮助命令
PDU.public=# ; PDU数据拯救工具 | 命令帮助 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ **基础操作** b; │ 初始化数据库元信息 exit; │ 退出工具 **数据库切换** use <db>; │ 指定目标数据库 (例: use logs;) set <schema>; │ 指定操作模式 (例: set recovery;) **元数据展示** \l; │ 列出所有数据库 \dn; │ 显示当前数据库模式 \dt; │ 列出当前模式下的表 \d+ <table>; │ 查看表结构详情 (例: \d+ users;) \d <table>; │ 查看表列类型 (例: \d users;) **数据导出** unload <table>; │ 导出表数据 → ./<表名>.csv (例: unload orders;) unload SCH; │ 导出当前模式所有数据 unload DDL; │ 生成模式结构定义文件 unload COPY; │ 生成PSQL COPY语句脚本 **误删数据恢复** scan t1; │ 扫描被误删的表 restore del <Tx Number>; │ 通过事务号恢复被误删的数据 -------------------------------------------------------------------------------------- scan drop; │ 扫描wal日志中的drop事务 restore drop <Tx Number>; │ 通过事务号恢复被drop的表 add <oid> <tablename> <attibutes>; │ 将表信息手动添加到restore库中 例如: <add 12345 t1 varchar,varchar,timestamp,varchar,numeric,varchar,varchar,varchar,numeric;> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 语法规则 ◈ 指令后缀必须带 `;`
加载PostgreSQL元数据
PDU.public=# b; 开始初始化... -pg_database:</var/lib/pgsql/12/data/global/1262> 【postgres】 -pg_schema:</var/lib/pgsql/12/data/base/14399/2615> -pg_class:</var/lib/pgsql/12/data/base/14399/1259>,共55行 -pg_attribute:</var/lib/pgsql/12/data/base/14399/1249>,共2913行 模式: -->public,2张表 【his5_dms】 -pg_schema:</var/lib/pgsql/12/data/base/16386/2615> -pg_class:</var/lib/pgsql/12/data/base/16386/1259>,共793行 -pg_attribute:</var/lib/pgsql/12/data/base/16386/1249>,共31329行 模式: -->public,660张表 -->hiscrm,55张表 -->pgagent,8张表 -->report,7张表 -->statistics,10张表
查看当前有哪些库
PDU.public=# \l; |------------------| | 数据库名 | |------------------| | postgres | | template1 | | template0 | | his5_dms | | restore | |------------------| 5 rows selected
进入某个库
PDU.public=# use his5_dms; |----------------------------------------| | 模式 | 表数量 | |----------------------------------------| | public | 660 | | hiscrm | 55 | | pgagent | 8 | | report | 7 | | statistics | 10 | |----------------------------------------|
进入某个模式
his5_dms.public=# set hiscrm; |--------------------------------------------------| | 表名 | 表大小 | |--------------------------------------------------| | t_patient_other | 600.00 KB | | t_sys_oper_log | 88.00 KB | | t_auth | 88.00 KB | | t_setting_user | 56.00 KB | | t_field_define | 32.00 KB | | t_oper_log | 16.00 KB | | t_role | 16.00 KB | | t_sys_login_log | 8.00 KB | | t_appointment_item | 8.00 KB | | t_clinic | 8.00 KB | | t_dept | 8.00 KB | | t_employee | 8.00 KB | | t_menu | 8.00 KB | | t_patient_label | 8.00 KB | | t_patient_label_detail_tpl | 8.00 KB | | t_patient_source_ref | 8.00 KB | | t_return_visit_tpl | 8.00 KB | | t_setting_clinic | 8.00 KB | | t_setting_notify | 8.00 KB | | t_sms_template_category | 8.00 KB | |--------------------------------------------------| 仅显示表大小排名前 50 的表名
显示部分表
his5_dms.hiscrm=# \dt; |--------------------------------------------------| | 表名 | 表大小 | |--------------------------------------------------| | t_patient_other | 600.00 KB | | t_sys_oper_log | 88.00 KB | | t_auth | 88.00 KB | | t_setting_user | 56.00 KB | | t_field_define | 32.00 KB | | t_oper_log | 16.00 KB | | t_role | 16.00 KB | | t_sys_login_log | 8.00 KB | | t_appointment_item | 8.00 KB | | t_clinic | 8.00 KB | | t_dept | 8.00 KB | | t_employee | 8.00 KB | | t_menu | 8.00 KB | | t_patient_label | 8.00 KB | | t_patient_label_detail_tpl | 8.00 KB | | t_patient_source_ref | 8.00 KB | | t_return_visit_tpl | 8.00 KB | | t_setting_clinic | 8.00 KB | | t_setting_notify | 8.00 KB | | t_sms_template_category | 8.00 KB | |--------------------------------------------------| 仅显示表大小排名前 50 的表名
显示某个表的信息
his5_dms.hiscrm=# \d+ t_auth; ---------------------------------------------------------------- | 建表语句 | ---------------------------------------------------------------- CREATE TABLE t_auth ( id bigint, clinic_id bigint, group_id bigint, parient_id varchar(64), menu_id varchar(64), auth_key varchar(60), auth_name varchar(64), uris varchar, rely varchar(255), state bigint, sort bigint, tag bigint, explain varchar(255), desc varchar(255) ); ---------------------------------------------------------------- | | ---------------------------------------------------------------- his5_dms.hiscrm=# \d t_auth; ---------------------------------------------------------------- | 列类型 | ---------------------------------------------------------------- bigint,bigint,bigint,varchar,varchar,varchar,varchar,varchar,varchar,bigint,bigint,bigint,varchar,varchar
恢复表数据
his5_dms.hiscrm=# unload t_auth; 正在解析表 <t_auth>. 已解析数据页: 11, 已解析数据: 492 条 <t_auth>-</var/lib/pgsql/12/data/base/16386/16895> 解析完成, 12 个数据页 ,共计 492 条数据. 成功 492 条; 失败【0】条 COPY文件路径为:<his5_dms/hiscrm/t_auth.csv>
确认恢复表的数据情况
[root@xifenfeidg hiscrm]# wc -l t_auth.csv 492 t_auth.csv
PostgreSQL中查询表实际数据情况
his5_dms=# select count(1) from hiscrm.t_auth; count ------- 492 (1 row) is5_dms=# \d hiscrm.t_auth; Table "hiscrm.t_auth" Column | Type | Collation | Nullable | Default ------------+------------------------+-----------+----------+------------------------- id | bigint | | not null | clinic_id | bigint | | | group_id | bigint | | | parient_id | character varying(64) | | not null | menu_id | character varying(64) | | not null | auth_key | character varying(60) | | not null | auth_name | character varying(64) | | not null | uris | text | | not null | rely | character varying(255) | | | NULL::character varying state | bigint | | not null | sort | bigint | | not null | tag | bigint | | | '0'::bigint explain | character varying(255) | | | NULL::character varying desc | character varying(255) | | | NULL::character varying
truncate表
his5_dms=# truncate table hiscrm.t_auth; TRUNCATE TABLE his5_dms=# select count(1) from hiscrm.t_auth; count ------- 0 (1 row)
导入数据,并验证恢复效果
his5_dms=# \i /tmp/pdu/his5_dms/COPY/hiscrm_copy.sql SET COPY 492 his5_dms=# select count(1) from hiscrm.t_auth; count ------- 492 (1 row)
通过验证pdu可以在数据库离线的情况下,恢复PostgreSQL数据库中表的数据,更加方便和灵活的实现替代pg_filedump功能