環境:
OS:Centos 7
DB:mongodb 5
原環境:1主2從,異機恢復的環境:單機(oplog.rs是否開啟都可以,開啟可以參考:https://www.cnblogs.com/hxlasky/p/17979922)
1.模擬寫入資料
連線主庫模擬寫入資料
/usr/local/services/mongodb/bin/mongo 192.168.56.101:29001
use admin
db.auth("test","test123");
myrepl:PRIMARY> use db_pushmsg;
switched to db db_pushmsg
myrepl:PRIMARY> for (var i=0;i<1000;i++){ db.user.save({"userid":i})}
myrepl:PRIMARY> db.user.count();
1000
2.備份並記錄備份完成時間
在主庫上進行備份,備份整個庫
[root@dsc1 bak]# /opt/mongodb-database-tools/bin/mongodump -h 192.168.56.101:29001 -u test -p test123 --authenticationDatabase admin --db=db_pushmsg -o /tmp/bak
2024-04-28T09:22:43.579+0800 writing db_pushmsg.tb_test to /tmp/bak/db_pushmsg/tb_test.bson
2024-04-28T09:22:43.584+0800 writing db_pushmsg.user to /tmp/bak/db_pushmsg/user.bson
2024-04-28T09:22:43.588+0800 done dumping db_pushmsg.tb_test (1 document)
2024-04-28T09:22:43.591+0800 done dumping db_pushmsg.user (1000 documents)
這裡完成備份的時間點是:2024-04-28T09:22:43,轉換成utc時間為:1714267363
後面我們需要從這個時間點開始匯出oplog日誌
3.模擬刪除表
db.user.insert({"userid" :1000})
db.user.insert({"userid" :1001})
db.user.insert({"userid" :1002})
db.user.insert({"userid" :1003})
db.user.insert({"userid" :1004})
db.user.insert({"userid" :1005})
這個時候刪除表,等會恢復我們需要恢復到1005這個時候的資料.
myrepl:PRIMARY> db.user.drop();
然後繼續寫入
db.user.insert({"userid" :1006})
db.user.insert({"userid" :1007})
db.user.insert({"userid" :1008})
db.user.insert({"userid" :1009})
db.user.insert({"userid" :1010})
4.找到刪除表的時間點
下面兩個方法都可以查詢刪除表的日誌
db.oplog.rs.find({"o" : { "drop" : "user" }})
db.oplog.rs.find({"o.drop": {$exists: true}}).sort({$natural: -1}).limit(1);
myrepl:PRIMARY> use local;
switched to db local
myrepl:PRIMARY> db.oplog.rs.find({"o.drop": {$exists: true}}).sort({$natural: -1}).limit(1);
{ "op" : "c", "ns" : "db_pushmsg.$cmd", "ui" : UUID("97085f8f-29a3-460e-a705-100a7b741979"), "o" : { "drop" : "user" }, "o2" : { "numRecords" : 1006 }, "ts" : Timestamp(1714268052, 1), "t" : NumberLong(3), "v" : NumberLong(2), "wall" : ISODate("2024-04-28T01:34:12.031Z") }
myrepl:PRIMARY> db.oplog.rs.find({"o" : { "drop" : "user" }})
{ "op" : "c", "ns" : "db_pushmsg.$cmd", "ui" : UUID("97085f8f-29a3-460e-a705-100a7b741979"), "o" : { "drop" : "user" }, "o2" : { "numRecords" : 1006 }, "ts" : Timestamp(1714268052, 1), "t" : NumberLong(3), "v" : NumberLong(2), "wall" : ISODate("2024-04-28T01:34:12.031Z") }
這裡刪除的時間點是:1714268052
5.匯出oplog.rs
用於異機資料恢復
[root@dsc1 bin]# mkdir /tmp/oplog
基於時間範圍查詢匯出:
/opt/mongodb-database-tools/bin/mongodump -h 192.168.56.101:29001 -u test -p test123 --authenticationDatabase admin --db=local -c oplog.rs --query '{"ts":{"$gt": {"$timestamp":{"t":1714267363, "i":1}},"$lt": {"$timestamp":{"t":1714268052, "i":1}}},"ns":{"$regex":"user"}}' -o /tmp/oplog
也可以在資料庫查詢下,檢視日誌情況
db.oplog.rs.find({ts:{$gt:Timestamp(1714267363, 1),$lt: Timestamp(1714268052, 1)},"ns":{"$regex":"user"}})
檢視匯出的檔案內容:
/opt/mongodb-database-tools/bin/bsondump /tmp/oplog/local/oplog.rs.bson>/tmp/oplog.txt
6.把備份檔案和oplog檔案複製到需要恢復的伺服器
[root@dsc1 tmp]# tar -cvf bak.tar ./bak
[root@dsc1 tmp]# tar -cvf oplog.tar ./oplog
[root@dsc1 tmp]#scp bak.tar root@192.168.56.103:/tmp/
[root@dsc1 tmp]#scp oplog.tar root@192.168.56.103:/tmp/
7.異機解壓備份檔案
[root@localhost tmp]# cd /tmp
[root@localhost tmp]# tar -xvf bak.tar
[root@localhost tmp]# tar -xvf oplog.tar
8.恢復
異機當前是沒有需要恢復的庫的
/opt/mongodb/bin/mongo 192.168.56.103:27001
> use admin
switched to db admin
> db.auth("test","test123");
1
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
恢復資料庫
[root@localhost ~]# /opt/mongodb-database-tools/bin/mongorestore -h 192.168.56.103:27001 -u test -p test123 --authenticationDatabase admin -d db_pushmsg /tmp/bak/db_pushmsg
9.恢復oplog
/opt/mongodb-database-tools/bin/mongorestore -h 192.168.56.103:27001 -u test -p test123 --authenticationDatabase admin --oplogReplay /tmp/oplog/local/oplog.rs.bson
這裡恢復需要指定具體的檔名:/tmp/oplog/local/oplog.rs.bson,而不是目錄
可以看到資料已經恢復
> db.user.find().sort({_id: -1}).limit(5)
{ "_id" : ObjectId("662da76002272b15d9bab6bb"), "userid" : 1005 }
{ "_id" : ObjectId("662da75b02272b15d9bab6ba"), "userid" : 1004 }
{ "_id" : ObjectId("662da75702272b15d9bab6b9"), "userid" : 1003 }
{ "_id" : ObjectId("662da75202272b15d9bab6b8"), "userid" : 1002 }
{ "_id" : ObjectId("662da74d02272b15d9bab6b7"), "userid" : 1001 }