ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MongoDB备份和恢复

2022-05-16 02:02:44  阅读:160  来源: 互联网

标签:admin -- MongoDB 备份 恢复 authenticationDatabase mongodb oplog port


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/

标签:admin,--,MongoDB,备份,恢复,authenticationDatabase,mongodb,oplog,port
来源: https://www.cnblogs.com/abner123/p/16275477.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有