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')
  }
]
相关推荐
杨超越luckly1 天前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强
Elastic 中国社区官方博客1 天前
Elasticsearch:Workflows 介绍 - 9.3
大数据·数据库·人工智能·elasticsearch·ai·全文检索
仍然.1 天前
MYSQL--- 聚合查询,分组查询和联合查询
数据库
一 乐1 天前
校园二手交易|基于springboot + vue校园二手交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
啦啦啦_99991 天前
Redis-0-业务逻辑
数据库·redis·缓存
自不量力的A同学1 天前
Redisson 4.2.0 发布,官方推荐的 Redis 客户端
数据库·redis·缓存
Exquisite.1 天前
Mysql
数据库·mysql
全栈前端老曹1 天前
【MongoDB】深入研究副本集与高可用性——Replica Set 架构、故障转移、读写分离
前端·javascript·数据库·mongodb·架构·nosql·副本集
R1nG8631 天前
CANN资源泄漏检测工具源码深度解读 实战设备内存泄漏排查
数据库·算法·cann
阿钱真强道1 天前
12 JetLinks MQTT直连设备事件上报实战(继电器场景)
linux·服务器·网络·数据库·网络协议