• MongoDB备份和恢复


    1. mongodb备份
    1.1 全库备份
    mongodump -uadmin -p123 --port 26060 --authenticationDatabase admin --oplog -o mongodb/full
    1.2 备份指定库
    mongodump -uadmin -p123 --port 26060 --authenticationDatabase admin -d test -o mongodb/db/
    1.3 备份指定库下的指定表
    mongodump -uadmin -p123 --port 26060 --authenticationDatabase admin -d test -c log -o mongodb/collection/
    1.4 压缩备份:
    mongodump -uadmin -p123 --port 26060 --authenticationDatabase admin -d test -o mongodb/db/ --gzip
    1.5 导出指定表的指定字段
    mongoexport -d dbname -c collectionname -o file --type json/csv -f field
    mongoexport -d itcast -c articles -o /home/mongodump/articles.json --type json -f "_id,author,dave,score,views"


    2. mongodb恢复
    2.1 恢复全库
    mongorestore -uadmin -p123 --port 26060 --authenticationDatabase admin --oplogReplay mongodb/full/
    2.1 恢复库
    mongorestore -uadmin -p123 --port 26060 --authenticationDatabase admin -d test mongodb/full/test/
    2.2 恢复表:
    mongorestore -uadmin -p123 --port 26060 --authenticationDatabase admin -d test -c log mongodb/collection/test/log.bson
    2.3 恢复前删除原始数据(慎用)
    mongorestore -uadmin -p123 --port 26060 --authenticationDatabase admin -d test --drop mongodb/full/test/
    2.4 导入更新文件
    /usr/bin/mongo -u admin -p 'xxx' --authenticationDatabase admin 127.0.0.1:26060/bg < aa.js bg库下执行的脚本
    2.5 导入
    mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field

    3.误操作恢复
    "i": insert
    "u": update
    "d": delete
    "c": db cmd
    "db":声明当前数据库 (其中ns 被设置成为=>数据库名称+ '.')
    "n": no op,即空操作,其会定期执行以确保时效性

    3.1 备份mongod中的local库的oplog.rs表
    mongodump --port 26060 -uadmin -p --authenticationDatabase admin -d local -c oplog.rs -o /path/dir
    3.2 登陆mongo数据库,查找local库中的oplog.rs,定位误操作的位置
    mongo --port 26060 -uadmin -p --authenticationDatabase admin
    3.3 查看oplog的信息
    ty88:PRIMARY> db.getReplicationInfo()
    {
    "logSizeMB" : 51200, #oplog大小
    "usedMB" : 51067.23, #以用大小
    "timeDiff" : 136111,
    "timeDiffHours" : 37.81, #数据的保存时间,这个值属于预估,根据目前的数据增长结合log大小,预估的值
    "tFirst" : "Tue Jul 07 2020 01:37:51 GMT+0800 (CST)",
    "tLast" : "Wed Jul 08 2020 15:26:22 GMT+0800 (CST)",
    "now" : "Wed Jul 08 2020 15:26:24 GMT+0800 (CST)"
    }
    ty88:PRIMARY> db.printReplicationInfo()
    configured oplog size: 51200MB
    log length start to end: 136260secs (37.85hrs)
    oplog first event time: Tue Jul 07 2020 01:37:51 GMT+0800 (CST)
    oplog last event time: Wed Jul 08 2020 15:28:51 GMT+0800 (CST)
    now: Wed Jul 08 2020 15:28:51 GMT+0800 (CST)

    3.4 确认操作的时间点,该例子是模拟在误删除后的操作。
    my_repl:PRIMARY> use local
    db.oplog.rs.find({op:"c"}).pretty();

    {
    "ts" : Timestamp(1553659908, 1),
    "t" : NumberLong(2),
    "h" : NumberLong("-7439981700218302504"),
    "v" : 2,
    "op" : "c",
    "ns" : "wo.$cmd",
    "ui" : UUID("db70fa45-edde-4945-ade3-747224745725"),
    "wall" : ISODate("2019-03-27T04:11:48.890Z"),
    "o" : {
    "drop" : "ci"
    }
    }
    3.3 通过上面的信息等位到时间戳
    "ts" : Timestamp(1553659908, 1),
    3.4 通过备份oplog恢复数据,替换完整备份时生成oplog
    cp oplog.rs.bson mongodb/full/oplog.bson
    3.5 还原数据,跳过上面时间戳的操作
    --oplogLimit "1553659908:1" 跳过时间点
    mongorestore --port 38021 --oplogReplay --oplogLimit "1553659908:1" --drop /mongodb/backup/

  • 相关阅读:
    白话数字签名(3)——Web程序中的数字签名(转)
    Android下基于XML的Graphics (转)
    js获取html页面传参
    nodejs教程:安装express及配置app.js文件
    Android中的Selector(转)
    org.apache.tools.zip.*和org.apache.commons.httpclient.*实现远程文件打包下载,支持中文文件名(转)
    EditText中进行文字截获和事件监听——(转载)
    常用js验证和常用方法汇总
    解决AVD的“SDL_app:emulator.exe应用程序错误”
    白话数字签名(2)——软件&设备(转)
  • 原文地址:https://www.cnblogs.com/abner123/p/16275477.html
Copyright © 2020-2023  润新知