重庆思庄Oracle、KingBase、PostgreSQL、Redhat认证学习论坛

标题: mongo数据库基于时间戳的增备 [打印本页]

作者: jiawang    时间: 2026-3-12 14:11
标题: mongo数据库基于时间戳的增备
1、创建测试库和集合
rs0 [direct: primary] test> use test_db
switched to db test_db
rs0 [direct: primary] test_db> db.test_coll.insertMany([
...   { id: 1, name: "基准数据1", create_time: new Date() },
...   { id: 2, name: "基准数据2", create_time: new Date() }
... ])
{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId('69b2552fa579447ebb9dc29d'),
    '1': ObjectId('69b2552fa579447ebb9dc29e')
  }
}
rs0 [direct: primary] test_db> exit


2、记录基准时间戳(增量备份的起始点)
[mongodb@dbserver mongodb]$ date +%s
1773294752



3、模拟数据增量变更
rs0 [direct: primary] test> use test_db
switched to db test_db
rs0 [direct: primary] test_db> db.test_coll.insertOne({ id: 3, name: "增量数据1", create_time: new Date() })
{
  acknowledged: true,
  insertedId: ObjectId('69b25551ad89b5bf959dc29d')
}
rs0 [direct: primary] test_db> db.test_coll.updateOne({ id: 2 }, { $set: { name: "基准数据2-已更新" } })
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}
rs0 [direct: primary] test_db> db.test_coll.deleteOne({ id: 1 });
{ acknowledged: true, deletedCount: 1 }
rs0 [direct: primary] test_db> db.test_coll.find();
[
  {
    _id: ObjectId('69b2552fa579447ebb9dc29e'),
    id: 2,
    name: '基准数据2-已更新',
    create_time: ISODate('2026-03-12T05:54:55.643Z')
  },
  {
    _id: ObjectId('69b25551ad89b5bf959dc29d'),
    id: 3,
    name: '增量数据1',
    create_time: ISODate('2026-03-12T05:55:29.083Z')
  }
]
rs0 [direct: primary] test_db>



基于时间戳的增量备份:
mongodump -u admin -p 'Admin@123' -h localhost --port 27017 --authenticationDatabase admin -d local -c oplog.rs --query '{"ts": {"$gt": {"$timestamp": {"t": 1773294752, "i": 1}}}}' -o /backup/mongodb/test_oplog_$(date +%Y%m%d)
1026469b25912ef964.png
登录/注册后可看大图