Whoami:5年+金融、政府、医疗领域工作经验的DBACertificate:PGCM、OCP、YCPSkill:Oracle、Mysql、PostgreSQL、国产数据库Platform:CSDN、墨天轮、公众号(呆呆的私房菜)
1. 创建测试数据
-- 创建数据库postgres=# create database chendb;CREATE DATABASE-- 创建用户postgres=# create role chen with login password 'chen';CREATE ROLE-- 用户授权postgres=# 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 TEXTchendb(> );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 10chendb=> 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)
2. 模拟数据库故障
-- 删除控制文件rm -rf $PGDATA/global/pg_control[postgres@host-01 ~]$ psqlpsql (15.4)Type "help" for help.postgres=# checkpoint ;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 unexpectedlyThis probably means the server terminated abnormallybefore 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 directorystopped 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-- 查看导出文件(以【db-schema-table.sql】格式命令)[postgres@host-01 bin]$ ls -ltr /home/postgres/backup/total 12-rw-rw-r--. 1 postgres postgres 33 3月 16 00:29 postgres-public-t2.sql-rw-rw-r--. 1 postgres postgres 96 3月 16 00:29 postgres-public-t1.sql-rw-rw-r--. 1 postgres postgres 1110 3月 16 00:29 chendb-chen-orders.sql-- 查看数据是否完整[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进行灾难恢复的工具。需要获取工具可以在 ZhangChen老师 公众号获取。
-- 解压工具压缩包(目前支持pg10-17版本)unzip PDU_2.0_for_Postgresql10-17_20250314.zip -d pdu-- 配置PostgreSQL数据目录和归档目录[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 Tool-----------------------------------------------------A dedicated data rescue tool for Postgresql databases.Current Version PG 15.-----------------------------------------------------【LIMITED VERSION】|-Max 10000 Records per Table|-Period of ValidityFor support, contact:WeChat Public Account: ZhangChen-PDUEmail: 1109315180@qq.com━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━使用前请设置ulimit -n为足够大,否则数据导出可能失败
执行【b】进行初始化(必须)
PDU.public=# b;开始初始化...-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 <表名>; -- 适用于单表导出,导出文件路径为 数据库名/模式名/表名.csvunload sch <模式名>; -- 适用于整个模式导出,导出的文件全部放在 数据库名/模式名 路径下。unload ddl; -- 导出当前模式下的所有表的ddl。unload copy; -- 导出数据库/模式名 路径下已导出的所有csv文件的copy语句chendb.public-# unload sch chen;正在解析表 <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.txtCOPY命令导出完成, 文件路径: chendb/COPY/chen_copy.sql,共找到1个csv文件DDL导出完成. 文件路径: chendb/DDL/chen_ddl.sql, 共计 1 张表
查看导出的文件
-- 查看ddl文件[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);-- 查看导出的csv文件[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 农资产品 有机肥料采购-- 查看copy的sql[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,还是建议配置数据库合理的备份策略,避免该类极端情况的出现。
本文内容就到这啦,相信这两款工具也同样能给你带来一些震撼吧!希望本篇内容能给你带来帮助。我们下篇再见!

文章转载自呆呆的私房菜,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




