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')
  }
]
相关推荐
haixingtianxinghai1 小时前
Redis的定期删除和惰性删除
数据库·redis·缓存
资深web全栈开发1 小时前
PostgreSQL Schema 最佳实践:架构师的命名与组织艺术
数据库·postgresql
麦聪聊数据2 小时前
利用实时数据管道与 SQL2API 重构企业自动化审计架构
数据库·sql·低代码
麦聪聊数据2 小时前
重构开放生态:利用 QuickAPI 跨越遗留系统与敏捷交付的工程实践
数据库·sql·低代码·restful
百结2147 小时前
Mysql数据库操作
数据库·mysql·oracle
keep one's resolveY7 小时前
时区问题解决
数据库
Leinwin7 小时前
OpenClaw 多 Agent 协作框架的并发限制与企业化规避方案痛点直击
java·运维·数据库
qq_417695057 小时前
机器学习与人工智能
jvm·数据库·python
漫随流水7 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
ego.iblacat7 小时前
MySQL 服务基础
数据库·mysql