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')
  }
]
相关推荐
l1t1 小时前
一个在postgresql中运行很快,但是在duckdb中运行很慢的SQL
数据库·sql·postgresql·duckdb
曹牧1 小时前
Oracle:增加十分钟
数据库·oracle
码界奇点1 小时前
深入解析MySQL9主从复制架构详解从原理到实战
数据库·sql·架构·可用性测试
独自归家的兔2 小时前
深度对比:PostgreSQL与MySQL的核心差异及选型指南
数据库·mysql·postgresql
结衣结衣.2 小时前
Redis的基本全局命令以及数据类型和内部编码
数据库·redis·bootstrap
抠脚学代码2 小时前
Qt与Linux
linux·数据库·qt
xb11322 小时前
WinForms 多窗体应用程序详解
数据库·microsoft·c#
天码-行空2 小时前
达梦数据库(DM8)详细安装教程
linux·运维·数据库
霖霖总总2 小时前
[小技巧36]MySQL 配置参数全解:参数含义、作用域与运维建议
运维·数据库·mysql