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

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 137|回复: 0
打印 上一主题 下一主题

mongo数据库基于时间戳的增备

[复制链接]
跳转到指定楼层
楼主
发表于 2026-3-12 14:11:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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)
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2026-4-17 17:20 , Processed in 0.282998 second(s), 24 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表