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')
  }
]
相关推荐
讯方洋哥14 分钟前
HarmonyOS App开发——关系型数据库应用App开发
数据库·harmonyos
惊讶的猫1 小时前
Redis持久化介绍
数据库·redis·缓存
Apple_羊先森1 小时前
ORACLE数据库巡检SQL脚本--19、磁盘读次数最高的前5条SQL语句
数据库·sql·oracle
全栈前端老曹2 小时前
【MongoDB】Node.js 集成 —— Mongoose ORM、Schema 设计、Model 操作
前端·javascript·数据库·mongodb·node.js·nosql·全栈
神梦流2 小时前
ops-math 算子库的扩展能力:高精度与复数运算的硬件映射策略
服务器·数据库
让学习成为一种生活方式2 小时前
trf v4.09.1 安装与使用--生信工具42-version2
数据库
啦啦啦_99992 小时前
Redis-5-doFormatAsync()方法
数据库·redis·c#
生产队队长2 小时前
Redis:Windows环境安装Redis,并将 Redis 进程注册为服务
数据库·redis·缓存
老邓计算机毕设2 小时前
SSM找学互助系统52568(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架·javaweb 毕业设计
痴儿哈哈2 小时前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python