mongodump备份工具
mongodump --help
查看帮助命令
mongodump -u -p --port 28020 --authenticationDatabase admin -o /tmp/backup
mongodump -u -p --port 28020 --authenticationDatabase admin -d test -o /tmp/backup
mongodump -u -p --port 28020 --authenticationDatabase admin -d test -c customer -o /tmp/backup
压缩备份单库
mongodump -u -p --port 28020 --authenticationDatabase admin -d test -o /tmp/backup --gzip
压缩备份单表
mongodump -u -p --port 28020 --authenticationDatabase admin -d test -c customer -o /tmp/backup --gzip
mongodump --help
查看帮助命令
mongorestore -u -p --port 28018 --authenticationDatabase admin -d test /tmp/backup/test
mongorestore -u -p --port 28018 --authenticationDatabase admin -d test -c customer /tmp/backup/test/customer.bson
恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,备份后添加修改的数据都会被删除,谨慎使用!!!
# 恢复单库
mongorestore -u -p --port 28018 --authenticationDatabase admin -d test --drop /tmp/backup/test
# 恢复单表
mongorestore -u -p --port 28018 --authenticationDatabase admin -d test -c customer --drop /tmp/backup/test/customer.bson
mongorestore -u -p --port 28018 --authenticationDatabase admin --gzip /tmp/backup
oplog在local库: local.oplog
master/slave 架构下:local.oplog.$main;
replica sets 架构下:local.oplog.rs
easydb:PRIMARY> rs.printReplicationInfo()
configured oplog size: 2048MB <--集合大小
log length start to end: 524592secs (145.72hrs) <--预计窗口覆盖时间
oplog first event time: Fri Jan 08 2021 18:30:18 GMT+0800 (CST)
oplog last event time: Thu Jan 14 2021 20:13:30 GMT+0800 (CST)
now: Thu Jan 14 2021 20:13:35 GMT+0800 (CST)
easydb:PRIMARY> use local
easydb:PRIMARY> db.oplog.rs.find().pretty()
{
"ts" : Timestamp(1610101818, 1),
"h" : NumberLong("-124421688313536398"),
"v" : 2,
"op" : "n",
"ns" : "",
"wall" : ISODate("2021-01-08T10:30:18.329Z"),
"o" : {
"msg" : "initiating set"
}
}
下面来分析一下oplog中字段的含义,通过下面的命令取出一条oplog:
db.oplog.rs.find().skip(1).limit(1).toArray()
easydb:PRIMARY> use local
switched to db local
easydb:PRIMARY> db.oplog.rs.find().skip(1).limit(1).toArray()
[
{
"ts" : Timestamp(1610101831, 1),
"t" : NumberLong(1),
"h" : NumberLong("-5190685117971188188"),
"v" : 2,
"op" : "n",
"ns" : "",
"wall" : ISODate("2021-01-08T10:30:31.356Z"),
"o" : {
"msg" : "new primary"
}
}
]
[root@mysql02 ~]# mongodump --help
--oplog use oplog for taking a point-in-time snapshot
参数 | 参数说明 |
---|---|
--oplogReplay | 重放oplog.bson中的操作内容 |
--oplogLimit | 与--oplogReplay一起使用时,可以限制重放到指定的时间点 |
首先我们在mongodb中模拟一个不断有插入操作的集合foo
use test
for(var i = 0; i < 10000; i++) {
db.foo.insert({a: i});
}
然后在插入过程中模拟一次mongodump全备,并添加--oplog参数
[root@mysql02 ~]# mongodump -u -p --port 28018 --authenticationDatabase admin --oplog -o /tmp/backup
2021-01-14T20:51:15.254+0800 writing admin.system.version to
2021-01-14T20:51:15.255+0800 done dumping admin.system.version (1 document)
2021-01-14T20:51:15.255+0800 writing test.foo to
2021-01-14T20:51:15.474+0800 done dumping test.foo (1701 documents)
2021-01-14T20:51:15.483+0800 writing captured oplog to
2021-01-14T20:51:15.509+0800 dumped 73 oplog entries
[root@mysql02 ~]# ll /tmp/backup
总用量 12
drwxr-xr-x 2 root root 69 1月 14 20:51 admin
-rw-r--r-- 1 root root 10804 1月 14 20:51 oplog.bson
drwxr-xr-x 2 root root 47 1月 14 20:51 test
从dump开始到结束的时间系统将记录所有的oplog到oplog.bson文件中
注意:--oplog选项只对全库导出有效,所以不能指定-d选项。
[root@mysql02 ~]# mongorestore -u -p --port 28018 --authenticationDatabase admin --oplogReplay --drop /tmp/backup
2021-01-14T21:13:52.134+0800 preparing collections to restore from
2021-01-14T21:13:52.144+0800 reading metadata for test.foo from /tmp/backup/test/foo.metadata.json
2021-01-14T21:13:52.199+0800 restoring test.foo from /tmp/backup/test/foo.bson
2021-01-14T21:13:52.291+0800 no indexes to restore
2021-01-14T21:13:52.291+0800 finished restoring test.foo (1701 documents) <--表示test.foo集合中恢复了1701个文档
2021-01-14T21:13:52.291+0800 replaying oplog <--表示重放了oplog中的所有操作
2021-01-14T21:13:52.407+0800 done
easydb:PRIMARY> db.foo.count()
1771 <--所以实际比1701个多,因为应用了oplog中的数据
oplog有两种来源:
例如:
mongodump --port 28021 -d local -c oplog.rs
easydb:PRIMARY> use test
switched to db test
easydb:PRIMARY> for(i=0;i<30000;i++){ db.easydb.insert({"id":i,"name":"easydb.net","age":18,"date":new Date()}); }
WriteResult({ "nInserted" : 1 })
[root@mysql02 ~]# mongodump -u -p --port 28018 --authenticationDatabase admin --oplog -o /tmp/backup
2021-01-15T11:46:14.449+0800 writing admin.system.version to
2021-01-15T11:46:14.451+0800 done dumping admin.system.version (1 document)
2021-01-15T11:46:14.452+0800 writing test.easydb to
2021-01-15T11:46:15.799+0800 done dumping test.easydb (20154 documents)
2021-01-15T11:46:15.805+0800 writing captured oplog to
2021-01-15T11:46:15.991+0800 dumped 1601 oplog entries
easydb:PRIMARY> use test
switched to db test
easydb:PRIMARY> show collections
easydb
easydb:PRIMARY> db.dropDatabase()
{
"dropped" : "test",
"ok" : 1,
"operationTime" : Timestamp(1610682482, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1610682482, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
[root@mysql02 ~]# bsondump /tmp/backup/oplog.bson > /tmp/oplog.json
2021-01-16T17:28:56.252+0800 417 objects found
[root@mysql02 ~]# tail -1 /tmp/oplog.json
{"ts":{"$timestamp":{"t":1610789118,"i":416}},"t":{"$numberLong":"6"},"h":{"$numberLong":"8277197316656995929"},"v":2,"op":"i","ns":"test.easydb","ui":{"$binary":"JEUrQxOWS6Gw8vfPIetrqQ==","$type":"04"},"wall":{"$date":"2021-01-16T09:25:18.435Z"},"o":{"_id":{"$oid":"6002b0fe4994bdfbd2b0747e"},"id":17511.0,"name":"easydb.net","age":18.0,"date":{"$date":"2021-01-16T09:25:18.443Z"}}}
{"t":1610789118,"i":416}就是上一次全备最后的时间戳
mongodump -u -p --port 28018 --authenticationDatabase admin -d local -c oplog.rs -q '{ts:{$gt:Timestamp(1610789118,416)}}' -o /tmp/oplog
#这里填的是我们上一步截取的时间戳
mongorestore -u -p --port 28018 --authenticationDatabase admin --oplogReplay /tmp/backup
easydb:PRIMARY> use test
switched to db test
easydb:PRIMARY> db.easydb.count()
17512
检查全备恢复了多少条数据
[root@mysql02 ~]# bsondump /tmp/oplog/local/oplog.rs.bson | grep -C 5 dropDatabase
{"ts":{"$timestamp":{"t":1610789163,"i":1}},"t":{"$numberLong":"6"},"h":{"$numberLong":"-8852159408511820267"},"v":2,"op":"n","ns":"","wall":{"$date":"2021-01-16T09:26:03.551Z"},"o":{"msg":"periodic noop"}}
{"ts":{"$timestamp":{"t":1610789168,"i":1}},"t":{"$numberLong":"6"},"h":{"$numberLong":"-8231808943143165392"},"v":2,"op":"c","ns":"test.$cmd","ui":{"$binary":"JEUrQxOWS6Gw8vfPIetrqQ==","$type":"04"},"wall":{"$date":"2021-01-16T09:26:08.735Z"},"o":{"drop":"easydb"}}
2021-01-16T17:51:45.426+0800 12587 objects found
{"ts":{"$timestamp":{"t":1610789168,"i":2}},"t":{"$numberLong":"6"},"h":{"$numberLong":"-1802663680461751854"},"v":2,"op":"c","ns":"test.$cmd","wall":{"$date":"2021-01-16T09:26:08.807Z"},"o":{"dropDatabase":1}}
{"ts":{"$timestamp":{"t":1610789183,"i":1}},"t":{"$numberLong":"6"},"h":{"$numberLong":"7216379125886047426"},"v":2,"op":"n","ns":"","wall":{"$date":"2021-01-16T09:26:23.551Z"},"o":{"msg":"periodic noop"}}
将过滤出来的Unix时间戳进行转换,再次确认是不是误操作的时间点
cp /tmp/oplog/local/oplog.rs.bson /tmp/backup/oplog.bson
mongorestore -u -p --port 28018 --authenticationDatabase admin --oplogReplay --oplogLimit "1610789168:1" /tmp/backup
easydb:PRIMARY> db.easydb.count()
30000
再次检查,发现30000条数据已经全部恢复
以上就是MongoDB数据库备份(mongodump)以及恢复(mongorestore)工具实践的所有内容,欢迎小伙伴们交流讨论。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。