Percona Backup for MongoDB备份恢复操作

Percona Backup for MongoDB的安装部署操作参考Percona Backup for MongoDB安装部署

生成测试数据

json 复制代码
# 插入数据
use product
for(i = 1; i <= 10000; i++) {
    db.order.insertOne({
        "order_id": "ORD" + i,
        "customer": "用户" + (i % 50 + 1),
        "amount": Math.floor(Math.random() * 1000 + 100),
        "status": ["待付款", "已付款", "已发货", "已完成", "已取消"][Math.floor(Math.random() * 5)],
        "create_time": new Date()
    });
}

1.逻辑备份

json 复制代码
[root@MongoDB-07 local]# pbm backup --type=logical
Starting backup '2026-01-19T06:46:48Z'....Backup '2026-01-19T06:46:48Z' to remote store '/backup/mongodb/replica_set/shard03'

2.逻辑恢复

注意事项

  1. 在恢复操作进行期间,请禁止客户端访问数据库。客户端所做的写入操作会导致最终恢复的数据与备份的数据有所不同。
  2. 对于版本 2.3.1 及更早版本,在运行 pbm restore 前请先禁用时间点恢复功能。这是因为时间点式恢复操作日志切片和恢复是不兼容的操作,不能同时进行。
json 复制代码
(1)查看备份列表
[root@MongoDB-07 local]# pbm list
Backup snapshots:
  2026-01-19T06:04:19Z <logical> [restore_to_time: 2026-01-19T06:04:23]
  2026-01-19T06:46:48Z <logical> [restore_to_time: 2026-01-19T06:46:52]

(2)执行逻辑恢复
[root@MongoDB-07 local]# pbm restore 2026-01-19T06:46:48Z
Starting restore 2026-01-19T06:49:16.136514223Z from '2026-01-19T06:46:48Z'.....Restore of the snapshot from '2026-01-19T06:46:48Z' has started

查看pbm-agent日志,恢复操作会在副本集主agent上运行。

json 复制代码
[root@MongoDB-07 ~]# tail -100f  /data/mongod/logs/pbm-agent.log
{"ts":1768805356,"s":3,"rs":"shard03","node":"10.0.0.37:27017","e":"restore","eobj":"2026-01-19T06:49:16.136514223Z","ep":{"T":1768799407,"I":1},"opid":"696dd3ec3537358b7218d077","msg":"backup: 2026-01-19T06:46:48Z"}
{"ts":1768805356,"s":3,"rs":"shard03","node":"10.0.0.37:27017","e":"restore","eobj":"2026-01-19T06:49:16.136514223Z","ep":{"T":1768799407,"I":1},"opid":"696dd3ec3537358b7218d077","msg":"recovery started"}
{"ts":1768805357,"s":3,"rs":"shard03","node":"10.0.0.37:27017","e":"restore","eobj":"2026-01-19T06:49:16.136514223Z","ep":{"T":1768799407,"I":1},"opid":"696dd3ec3537358b7218d077","msg":"moving to state cleanupCluster"}
{"ts":1768805359,"s":3,"rs":"shard03","node":"10.0.0.37:27017","e":"restore","eobj":"2026-01-19T06:49:16.136514223Z","ep":{"T":1768799407,"I":1},"opid":"696dd3ec3537358b7218d077","msg":"moving to state running"}
2026-01-19T14:49:22.007+0800    Setting num cpus to 1
2026-01-19T14:49:22.013+0800    connected to node type: replset
..........................
{"ts":1768805364,"s":3,"rs":"shard03","node":"10.0.0.37:27017","e":"restore","eobj":"2026-01-19T06:49:16.136514223Z","ep":{"T":1768799407,"I":1},"opid":"696dd3ec3537358b7218d077","msg":"restoring indexes for admin.pbmPITRChunks: rs_1_start_ts_1_end_ts_1, start_ts_1_end_ts_1"}
{"ts":1768805365,"s":3,"rs":"shard03","node":"10.0.0.37:27017","e":"restore","eobj":"2026-01-19T06:49:16.136514223Z","ep":{"T":1768799407,"I":1},"opid":"696dd3ec3537358b7218d077","msg":"recovery successfully finished"}

执行恢复后,时间点功能将会停止,需要再次开启

json 复制代码
[root@MongoDB-07 local]# pbm config --list
[root@MongoDB-07 local]# pbm config --set pitr.enabled=true
[pitr.enabled=true]

# 日志中会显示关闭与开启时间点恢复的信息
[root@MongoDB-09 local]# tail -f  /data/mongod/logs/pbm-agent.log
{"ts":1768805364,"s":3,"rs":"shard03","node":"10.0.0.39:27017","e":"pitr","eobj":"","ep":{"T":1768799407,"I":1},"msg":"pausing/stopping with last_ts 2026-01-19 06:49:24 +0000 UTC"}
{"ts":1768806462,"s":3,"rs":"shard03","node":"10.0.0.39:27017","e":"pitr","eobj":"","ep":{"T":1768806439,"I":3},"msg":"streaming started from 2026-01-19 06:49:24 +0000 UTC / 1768805364"}

3.时间点恢复

json 复制代码
(1)误删除集合
shard03 [direct: primary] product> db.order.drop()
true
(2)查看误删除操作的时间
shard03 [direct: primary] admin> use local
switched to db local
shard03 [direct: primary] local> db.oplog.rs.find({"op":"c","o.drop":"order"}).sort({ wall: -1 }).limit(1)
[
  {
    op: 'c',
    ns: 'product.$cmd',
    ui: UUID('c6f403ed-062c-4c7f-8039-e1349920bc23'),
    o: { drop: 'order' },
    o2: { numRecords: 20000 },
    ts: Timestamp({ t: 1768807114, i: 1 }),
    t: Long('2'),
    v: Long('2'),
    wall: ISODate('2026-01-19T07:18:34.556Z')
  }
]

(3)查看备份列表
[root@MongoDB-07 ~]# pbm list
Backup snapshots:
  2026-01-19T06:04:19Z <logical> [restore_to_time: 2026-01-19T06:04:23]
  2026-01-19T06:46:48Z <logical> [restore_to_time: 2026-01-19T06:46:52]
  2026-01-19T07:09:40Z <logical> [restore_to_time: 2026-01-19T07:09:44]

PITR <on>:
  2026-01-19T06:04:24 - 2026-01-19T07:20:15

(4)执行时间点恢复,恢复到删除集合操作之前
[root@MongoDB-07 local]# pbm restore --time="2026-01-19T07:18:33"
Starting restore 2026-01-19T07:26:14.308333705Z to point-in-time 2026-01-19T07:18:33 from '2026-01-19T07:09:40Z'.....

(5)恢复成功
shard03 [direct: primary] local> use product
switched to db product
shard03 [direct: primary] product> db.order.find().sort({ create_time: -1 }).limit(1)
[
  {
    _id: ObjectId('696dd90f3998bd31c58e0d75'),
    order_id: 'ORD20000',
    customer: '用户1',
    amount: 304,
    status: '已取消',
    create_time: ISODate('2026-01-19T07:11:11.710Z')
  }
]
相关推荐
科技小花39 分钟前
数据治理平台架构演进观察:AI原生设计如何重构企业数据管理范式
数据库·重构·架构·数据治理·ai-native·ai原生
一江寒逸41 分钟前
零基础从入门到精通MySQL(中篇):进阶篇——吃透多表查询、事务核心与高级特性,搞定复杂业务SQL
数据库·sql·mysql
D4c-lovetrain43 分钟前
linux个人心得22 (mysql)
数据库·mysql
阿里小阿希1 小时前
CentOS7 PostgreSQL 9.2 升级到 15 完整教程
数据库·postgresql
荒川之神1 小时前
Oracle 数据仓库雪花模型设计(完整实战方案)
数据库·数据仓库·oracle
做个文艺程序员2 小时前
MySQL安全加固十大硬核操作
数据库·mysql·安全
不吃香菜学java2 小时前
Redis简单应用
数据库·spring boot·tomcat·maven
一个天蝎座 白勺 程序猿2 小时前
Apache IoTDB(15):IoTDB查询写回(INTO子句)深度解析——从语法到实战的ETL全链路指南
数据库·apache·etl·iotdb
不知名的老吴2 小时前
Redis的延迟瓶颈:TCP栈开销无法避免
数据库·redis·缓存
YOU OU2 小时前
三大范式和E-R图
数据库