2016/1/4

How to Backup & Retore in MangoDB


以下測試如何備份(mongodump)以及還原(mongorestore) MongoDB 資料庫。


備份 mongodump


在備份資料前,必須先讓在記憶體中的資料同步到磁碟中(fsync),並將資料庫上鎖(lock),避免備份過程中,還有資料異動。


use admin
db.runCommand({fsync:1, lock:1})

查詢 DB 的狀態,看看是不是上鎖了


db.currentOp()

然後以 mongodump 進行資料備份


mkdir -p /home/mongodb/project/data/backup
mongodump -d testdatabase -o /home/mongodb/project/data/backup

也可以直接備份遠端的 DB


mongodump -h 192.168.1.11 -p 37017 -d testdatabase -o /home/mongodb/project/data/backup

備份完成後,要解鎖資料庫


db.fsyncUnlock()

執行過程如下:


> use admin
switched to db admin
> db.runCommand({fsync:1, lock:1})
{
    "info" : "now locked against writes, use db.fsyncUnlock() to unlock",
    "seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
    "ok" : 1
}


> db.currentOp()
{
    "inprog" : [ ],
    "fsyncLock" : true,
    "info" : "use db.fsyncUnlock() to terminate the fsync write/snapshot lock"
}

在另一個 terminal 進行備份


[root@server backup]# /usr/share/mongodb/bin/mongodump -d testdatabase -o /home/mongodb/project/data/backup
2015-11-30T14:43:18.585+0800    writing testdatabase.tracker to /home/mongodb/project/data/backup/testdatabase/tracker.bson
2015-11-30T14:43:18.585+0800    writing testdatabase.user to /home/mongodb/project/data/backup/testdatabase/user.bson
2015-11-30T14:43:18.594+0800    writing testdatabase.user metadata to /home/mongodb/project/data/backup/testdatabase/user.metadata.json
2015-11-30T14:43:18.612+0800    done dumping testdatabase.user (1000 documents)
2015-11-30T14:43:18.612+0800    writing testdatabase.system.indexes to /home/mongodb/project/data/backup/testdatabase/system.indexes.bson
2015-11-30T14:43:18.612+0800    writing testdatabase.event to /home/mongodb/project/data/backup/testdatabase/event.bson
2015-11-30T14:43:18.612+0800    writing testdatabase.event metadata to /home/mongodb/project/data/backup/testdatabase/event.metadata.json
2015-11-30T14:43:18.612+0800    done dumping testdatabase.event (0 documents)
2015-11-30T14:43:18.612+0800    writing testdatabase.eventlog to /home/mongodb/project/data/backup/testdatabase/eventlog.bson
2015-11-30T14:43:18.613+0800    writing testdatabase.eventlog metadata to /home/mongodb/project/data/backup/testdatabase/eventlog.metadata.json
2015-11-30T14:43:18.614+0800    done dumping testdatabase.eventlog (0 documents)
2015-11-30T14:43:21.120+0800    writing testdatabase.tracker metadata to /home/mongodb/project/data/backup/testdatabase/tracker.metadata.json
2015-11-30T14:43:21.121+0800    done dumping testdatabase.tracker (759461 documents)

最後 unlock db


> db.fsyncUnlock()
{ "ok" : 1, "info" : "unlock completed" }

還原 mongorestore


還原指令如下


/usr/share/mongodb/bin/mongorestore -d testdatabase --drop /home/mongodb/project/data/backup/testdatabase

--drop 的部份,就是先將這裡的 database 刪除,然後再進行 restore


執行過程如下


[root@server backup]# /usr/share/mongodb/bin/mongorestore -d testdatabase --drop /home/mongodb/project/data/backup/testdatabase
2015-11-30T14:52:45.306+0800    building a list of collections to restore from /home/mongodb/project/data/backup/testdatabase dir
2015-11-30T14:52:45.306+0800    reading metadata file from /home/mongodb/project/data/backup/testdatabase/tracker.metadata.json
2015-11-30T14:52:45.306+0800    restoring testdatabase.tracker from file /home/mongodb/project/data/backup/testdatabase/tracker.bson
2015-11-30T14:52:45.308+0800    reading metadata file from /home/mongodb/project/data/backup/testdatabase/user.metadata.json
2015-11-30T14:52:45.309+0800    restoring testdatabase.user from file /home/mongodb/project/data/backup/testdatabase/user.bson
2015-11-30T14:52:45.310+0800    reading metadata file from /home/mongodb/project/data/backup/testdatabase/event.metadata.json
2015-11-30T14:52:45.812+0800    restoring testdatabase.event from file /home/mongodb/project/data/backup/testdatabase/event.bson
2015-11-30T14:52:45.812+0800    restoring indexes for collection testdatabase.event from metadata
2015-11-30T14:52:45.813+0800    reading metadata file from /home/mongodb/project/data/backup/testdatabase/eventlog.metadata.json
2015-11-30T14:52:45.813+0800    restoring testdatabase.eventlog from file /home/mongodb/project/data/backup/testdatabase/eventlog.bson
2015-11-30T14:52:45.815+0800    finished restoring testdatabase.event (0 documents)
2015-11-30T14:52:45.815+0800    restoring indexes for collection testdatabase.eventlog from metadata
2015-11-30T14:52:45.817+0800    finished restoring testdatabase.eventlog (0 documents)
2015-11-30T14:52:45.821+0800    restoring indexes for collection testdatabase.user from metadata
2015-11-30T14:52:45.822+0800    finished restoring testdatabase.user (1000 documents)
2015-11-30T14:52:48.306+0800    [#####...................]  testdatabase.tracker  76.7 MB/342.5 MB  (22.4%)
2015-11-30T14:52:51.306+0800    [############............]  testdatabase.tracker  184.9 MB/342.5 MB  (54.0%)
2015-11-30T14:52:54.306+0800    [####################....]  testdatabase.tracker  293.1 MB/342.5 MB  (85.6%)
2015-11-30T14:52:57.025+0800    restoring indexes for collection testdatabase.tracker from metadata
2015-11-30T14:52:57.025+0800    finished restoring testdatabase.tracker (759461 documents)
2015-11-30T14:52:57.025+0800    done