walminer和pdu:太牛了,拯救PostgreSQL数据的工具来了
|
admin
2025年3月22日 20:11
本文热度 1503
|
阅读本文可以了解关于PostgreSQL灾难级数据恢复的两个工具(walminer和pdu),它能帮助你在无备份、数据库又损坏无法启动的极端情况下恢复出数据。postgres=# create database chendb;CREATE DATABASEpostgres=# create role chen with login password 'chen';CREATE ROLEpostgres=# grant connect, create on database chendb to chen;GRANT
postgres=# \c chendb chenYou are now connected to database "chendb" as user "chen".
chendb=> create schema chen authorization chen;CREATE SCHEMA
chendb=> set search_path = chen;SET
chendb=> CREATE TABLE orders (chendb(> order_id SERIAL PRIMARY KEY, chendb(> customer_name VARCHAR(50) NOT NULL, chendb(> order_date DATE NOT NULL DEFAULT CURRENT_DATE,chendb(> amount NUMERIC(10,2) CHECK (amount > 0), chendb(> status VARCHAR(20) CHECK (status IN ('pending', 'completed', 'shipped')),chendb(> product_category VARCHAR(30), chendb(> notes TEXT chendb(> );CREATE TABLEchendb=> INSERT INTO orders (customer_name, order_date, amount, status, product_category, notes) VALUESchendb-> ('张三', '2024-03-01', 1500.00, 'completed', '电子产品', '客户要求加急配送'),chendb-> ('李四科技', DEFAULT, 8999.99, 'pending', '工业设备', '需确认付款方式'),chendb-> ('王五餐饮', '2024-02-28', 450.50, 'shipped', '食品饮料', '冷链运输'),chendb-> ('赵六书店', '2025-01-10', 200.00, 'completed', '图书', '会员折扣已应用'),chendb-> ('陈七服饰', '2024-12-25', 1200.00, 'shipped', '服装鞋帽', '节日促销订单'),chendb-> ('周八物流', '2024-11-11', 9800.00, 'pending', '物流服务', '大客户年度合约'),chendb-> ('吴九教育', '2024-05-30', 600.00, 'completed', '教育培训', '在线课程购买'),chendb-> ('郑十医疗', '2024-08-15', 3500.75, 'shipped', '医疗器材', '需提供质检报告'),chendb-> ('孙一建筑', '2025-02-14', 5500.00, 'pending', '建筑材料', '设计图纸待确认'),chendb-> ('钱二农业', '2024-07-04', 780.40, 'completed', '农资产品', '有机肥料采购');INSERT 0 10
chendb=> select * from orders; order_id | customer_name | order_date | amount | status | product_category | notes 1 | 张三 | 2024-03-01 | 1500.00 | completed | 电子产品 | 客户要求加急配送 2 | 李四科技 | 2025-03-16 | 8999.99 | pending | 工业设备 | 需确认付款方式 3 | 王五餐饮 | 2024-02-28 | 450.50 | shipped | 食品饮料 | 冷链运输 4 | 赵六书店 | 2025-01-10 | 200.00 | completed | 图书 | 会员折扣已应用 5 | 陈七服饰 | 2024-12-25 | 1200.00 | shipped | 服装鞋帽 | 节日促销订单 6 | 周八物流 | 2024-11-11 | 9800.00 | pending | 物流服务 | 大客户年度合约 7 | 吴九教育 | 2024-05-30 | 600.00 | completed | 教育培训 | 在线课程购买 8 | 郑十医疗 | 2024-08-15 | 3500.75 | shipped | 医疗器材 | 需提供质检报告 9 | 孙一建筑 | 2025-02-14 | 5500.00 | pending | 建筑材料 | 设计图纸待确认 10 | 钱二农业 | 2024-07-04 | 780.40 | completed | 农资产品 | 有机肥料采购(10 rows)
-- 删除控制文件rm -rf $PGDATA/global/pg_control
[postgres@host-01 ~]$ psqlpsql (15.4)Type "help" for help.postgres=WARNING: terminating connection because of crash of another server processDETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.HINT: In a moment you should be able to reconnect to the database and repeat your command.server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.The connection to the server was lost. Attempting reset: Failed.The connection to the server was lost. Attempting reset: Failed.
-- 重启一下数据库[postgres@host-01 ~]$ pg_ctl startpg_ctl: another server might be running; trying to start server anywaywaiting for server to start....postgres: could not find the database systemExpected to find it in the directory "/home/postgres/pg15/data",but could not open file "/home/postgres/pg15/data/global/pg_control": No such file or directory stopped waitingpg_ctl: could not start serverExamine the log output.
- 来自 传成老师 的walminer,个人版支持单线程离线解析数据文件并导出为sql文件。
https://gitee.com/movead/XLogMiner/releases
[postgres@host-01 ~]$ mkdir -p /home/postgres/backup
[postgres@host-01 ~]$ cd /home/postgres/walminer_x86_64_v4.10.0_dev4/bin[postgres@host-01 bin]$ ./walminer ddump -D $PGDATA -t /home/postgres/backup/#################################################Walminer for PostgreSQL walContact Author by mail 'lchch1990@sina.cn'No License for walminer test, for get a license you can read:https://gitee.com/movead/XLogMiner/wikis/walminer%20license#################################################>>>>>>>>DUMP DATABASE [postgres]********DUMP TABLE [public.t1]********DUMP TABLE [public.t2]>>>>>>>>DUMP DATABASE [chendb]********DUMP TABLE [chen.orders]DD SUCCESS
[postgres@host-01 bin]$ ls -ltr /home/postgres/backup/total 12-rw-rw-r-rw-rw-r-rw-rw-r
[postgres@host-01 bin]$ cat /home/postgres/backup/chendb-chen-orders.sqlCREATE TABLE chen.orders(order_id int4, customer_name varchar, order_date date, amount numeric, status varchar, product_category varchar, notes text);INSERT INTO chen.orders VALUES(1 ,'张三' ,'2024-03-01' ,1500.00 ,'completed' ,'电子产品' ,'客户要求加急配送'),(2 ,'李四科技' ,'2025-03-16' ,8999.99 ,'pending' ,'工业设备' ,'需确认付款方式'),(3 ,'王五餐饮' ,'2024-02-28' ,450.50 ,'shipped' ,'食品饮料' ,'冷链运输'),(4 ,'赵六书店' ,'2025-01-10' ,200.00 ,'completed' ,'图书' ,'会员折扣已应用'),(5 ,'陈七服饰' ,'2024-12-25' ,1200.00 ,'shipped' ,'服装鞋帽' ,'节日促销订单'),(6 ,'周八物流' ,'2024-11-11' ,9800.00 ,'pending' ,'物流服务' ,'大客户年度合约'),(7 ,'吴九教育' ,'2024-05-30' ,600.00 ,'completed' ,'教育培训' ,'在线课程购买'),(8 ,'郑十医疗' ,'2024-08-15' ,3500.75 ,'shipped' ,'医疗器材' ,'需提供质检报告'),(9 ,'孙一建筑' ,'2025-02-14' ,5500.00 ,'pending' ,'建筑材料' ,'设计图纸待确认'),(10 ,'钱二农业' ,'2024-07-04' ,780.40 ,'completed' ,'农资产品' ,'有机肥料采购');
- 太漂亮了,真不错!当然 walminer 不止这个离线导出功能,还有数据误删恢复等多种功能,有需要的朋友可以多多支持一下 传成老师 这个工具。
- 来自 ZhangChen老师 的PDU(PostgreSQL Data unloader),专门针对PG进行灾难恢复的工具。
unzip PDU_2.0_for_Postgresql10-17_20250314.zip -d pdu
[postgres@host-01 pdu]$ cat pdu.iniPGDATA=/home/postgres/pg15/dataARCHIVE_DEST=/home/postgres/pg15/data/pg_archive
[postgres@host-01 pdu]$ ./pdu15━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Copyright © 2024-2025 ZhangChen. All rights reserved.PDU: Postgresql Data Rescue ToolA dedicated data rescue tool for Postgresql databases.Current Version PG 15.【LIMITED VERSION】|-Max 10000 Records per Table|-Period of Validity
For support, contact:WeChat Public Account: ZhangChen-PDUEmail: 1109315180@qq.com
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━使用前请设置ulimit -n为足够大,否则数据导出可能失败
PDU.public=开始初始化... -pg_database:</home/postgres/pg15/data/global/1262> 【postgres】 -pg_schema:</home/postgres/pg15/data/base/5/2615> -pg_class:</home/postgres/pg15/data/base/5/1259>,共84行 -pg_attribute:</home/postgres/pg15/data/base/5/1249>,共3069行 模式: -->public,2张表 【chendb】 -pg_schema:</home/postgres/pg15/data/base/24639/2615> -pg_class:</home/postgres/pg15/data/base/24639/1259>,共84行 -pg_attribute:</home/postgres/pg15/data/base/24639/1249>,共3088行 模式: -->public,0张表 -->chen,1张表
PDU.public-# use chendb;|----------------------------------------|| 模式 | 表数量 ||----------------------------------------|| public | 0 || chen | 1 ||----------------------------------------|
- unload 离线导出数据(注意磁盘空间使用情况)
选项:unload tab <表名>; -- 适用于单表导出,导出文件路径为 数据库名/模式名/表名.csv unload sch <模式名>; -- 适用于整个模式导出,导出的文件全部放在 数据库名/模式名 路径下。unload ddl; -- 导出当前模式下的所有表的ddl。unload copy; -- 导出数据库/模式名 路径下已导出的所有csv文件的copy语句
chendb.public-正在解析表 <orders>. 已解析数据页: 1, 已解析数据: 10 条表名<orders>-</home/postgres/pg15/data/base/24639/24662> 解析完成, 1 个数据页 ,共计 10 条数据. 成功 10 条; 失败【0】条COPY文件路径为:<chendb/chen/orders.csv>
模式<chen>共 1 张表。成功:1, 失败【0】日志路径:log/log/chendb_unload_schema_chen_err.txt
COPY命令导出完成, 文件路径: chendb/COPY/chen_copy.sql,共找到1个csv文件
DDL导出完成. 文件路径: chendb/DDL/chen_ddl.sql, 共计 1 张表
[postgres@host-01 pdu]$ cat chendb/DDL/chen_ddl.sqlCREATE SCHEMA chen;set search_path to chen;CREATE TABLE orders( order_id int, customer_name varchar, order_date date, amount numeric(10,2), status varchar, product_category varchar, notes varchar);[postgres@host-01 pdu]$ cat chendb/chen/orders.csv1 张三 2024-03-01 1500.00 completed 电子产品 客户要求加急配送2 李四科技 2025-03-16 8999.99 pending 工业设备 需确认付款方式3 王五餐饮 2024-02-28 450.50 shipped 食品饮料 冷链运输4 赵六书店 2025-01-10 200.00 completed 图书 会员折扣已应用5 陈七服饰 2024-12-25 1200.00 shipped 服装鞋帽 节日促销订单6 周八物流 2024-11-11 9800.00 pending 物流服务 大客户年度合约7 吴九教育 2024-05-30 600.00 completed 教育培训 在线课程购买8 郑十医疗 2024-08-15 3500.75 shipped 医疗器材 需提供质检报告9 孙一建筑 2025-02-14 5500.00 pending 建筑材料 设计图纸待确认10 钱二农业 2024-07-04 780.40 completed 农资产品 有机肥料采购
[postgres@host-01 pdu]$ cat chendb/COPY/chen_copy.sqlset search_path to chen;COPY orders FROM '/home/postgres/pdu/chendb/chen/orders.csv';
- 不错不错真不错。这两款工具又给DBA们一颗故障处理定心丸。
- BUT,还是建议配置数据库合理的备份策略,避免该类极端情况的出现。
阅读原文:原文链接
该文章在 2025/3/24 17:16:26 编辑过
|
|