MongoDB資料檔案備份與恢復詳解及實驗說明
備份與恢復資料對於管理任何資料儲存系統來說都是非常重要的。
1、冷備份與恢復——建立資料檔案的副本(前提是要停止MongoDB伺服器),也就是直接copy
MongoDB將所有資料都儲存在資料目錄下,預設是/data/db/(Windows下是C:\data\db\),啟動MongoDB時也可以用--dbpath指定我們自己設定的資料儲存目錄。
備份MongoDB資料:只要簡單的建立資料儲存目錄的副本就可以了,直接copy一份。
恢復MongoDB資料:在MongoDB啟動時用--dbpath指定資料儲存目錄副本位置。
在伺服器執行的情況下直接copy是有風險的,可能copy出來時,資料已經遭到破壞,這種方式下建立資料目錄的副本需要在關閉MongoDB伺服器的前提下,資料目錄中儲存的就是關閉那一刻資料的快照,在伺服器重新啟動之前可以複製目錄作為備份。
2、熱備份與恢復——MongoDB bin目錄下自帶的mongodump和mongorestore工具
mongodump是一種能在執行時備份的方法。該命令詳細引數如下:
C:\Users\duansf>mongodump --help
Export MongoDB data to BSON files.
Options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times
for more verbosity e.g. -vvvvv)
--quiet silence all non error diagnostic
messages
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set
name>/s1,s2 for sets)
--port arg server port. Can also use --host
hostname:port
--ipv6 enable IPv6 support (disabled by
default)
-u [ --username ] arg username
-p [ --password ] arg password
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg (=MONGODB-CR)
authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg Remote host name to use for purpose of
GSSAPI/Kerberos authentication
--dbpath arg directly access mongod database files
in the given path, instead of
connecting to a mongod server - needs
to lock the data directory, so cannot
be used if a mongod is currently
accessing the same path
--directoryperdb each db is in a separate directory
(relevant only if dbpath specified)
--journal enable journaling (relevant only if
dbpath specified)
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-o [ --out ] arg (=dump) output directory or "-" for stdout
-q [ --query ] arg json query
--oplog Use oplog for point-in-time
snapshotting
--repair try to recover a crashed database
--forceTableScan force a table scan (do not use
$snapshot)
--dumpDbUsersAndRoles Dump user and role definitions for the
given database
mongorestore獲取mongodump的輸出結果,並將備份的資料插入到執行的MongoDB例項中實現資料恢復。該命令詳細引數如下:
C:\Users\duansf>mongorestore --help
Import BSON files into MongoDB.
usage: mongorestore [options] [directory or filename to restore from]
Options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times
for more verbosity e.g. -vvvvv)
--quiet silence all non error diagnostic
messages
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set
name>/s1,s2 for sets)
--port arg server port. Can also use --host
hostname:port
--ipv6 enable IPv6 support (disabled by
default)
-u [ --username ] arg username
-p [ --password ] arg password
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg (=MONGODB-CR)
authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg Remote host name to use for purpose of
GSSAPI/Kerberos authentication
--dbpath arg directly access mongod database files
in the given path, instead of
connecting to a mongod server - needs
to lock the data directory, so cannot
be used if a mongod is currently
accessing the same path
--directoryperdb each db is in a separate directory
(relevant only if dbpath specified)
--journal enable journaling (relevant only if
dbpath specified)
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
--objcheck validate object before inserting
(default)
--noobjcheck don't validate object before inserting
--filter arg filter to apply before inserting
--drop drop each collection before import
--oplogReplay replay oplog for point-in-time restore
--oplogLimit arg include oplog entries before the
provided Timestamp (seconds[:ordinal])
during the oplog replay; the ordinal
value is optional
--keepIndexVersion don't upgrade indexes to newest version
--noOptionsRestore don't restore collection options
--noIndexRestore don't restore indexes
--restoreDbUsersAndRoles Restore user and role definitions for
the given database
--w arg (=0) minimum number of replicas per write
mongodump(備份)與mongorestore(恢復)示例:
1、建立mongodb資料庫:
C:\Users\duansf>mongo
MongoDB shell version: 2.6.6
connecting to: test
> use mongodb
switched to db mongodb
> db.mongodb.insert({'name':'duansf','age':'37'})
WriteResult({ "nInserted" : 1 })
> db.mongodb.find()
{ "_id" : ObjectId("56fcf2ee613bba5454cf1d83"), "name" : "duansf", "age" : "37"
}
>
2、mongodump備份mongodb資料庫:
C:\Users\duansf>mongodump -d mongodb -o backup
connected to: 127.0.0.1
2016-03-31T17:51:24.555+0800 DATABASE: mongodb to backup\mongodb
2016-03-31T17:51:24.567+0800 mongodb.system.indexes to backup\mongodb\system.indexes.bson
2016-03-31T17:51:24.604+0800 1 documents
2016-03-31T17:51:24.604+0800 mongodb.mongodb to backup\mongodb\mongodb.bson
2016-03-31T17:51:24.782+0800 1 documents
2016-03-31T17:51:24.784+0800 Metadata for mongodb.mongodb to backup\mongodb\mongodb.metadata.json
-d指定需要備份的資料庫,-o指定備份位置,上述表示備份mongodb資料庫到C:\Users\duansf\backup目錄下
3、mongorestore恢復mongodb資料庫,並重新命名為mongodb2資料庫:
C:\Users\duansf>mongorestore -d mongodb2 --drop backup/mongodb
connected to: 127.0.0.1
2016-03-31T17:56:47.802+0800 backup/mongodb\mongodb.bson
2016-03-31T17:56:47.804+0800 going into namespace [mongodb2.mongodb]
2016-03-31T17:56:47.804+0800 dropping
1 objects found
2016-03-31T17:56:47.808+0800 Creating index: { key: { _id: 1 }, name: "_id_",
ns: "mongodb2.mongodb" }
-d指定要恢復為的資料庫,可以將備份的資料庫恢復到與原來不同名的資料庫中,這裡為mongodb2,--drop表示在恢復前刪除集合(若存在)。
否則,資料就會與現有集合資料合併,可能會覆蓋一些文件。
此時在MongoDB shell下可以檢視到mongodb2資料庫,及其中的mongodb集合和資料文件。
C:\Users\duansf>mongo
MongoDB shell version: 2.6.6
connecting to: test
> show dbs
admin (empty)
chenfeng 0.078GB
dsf 0.078GB
duansf 0.078GB
idex_t 0.078GB
idx_t 0.078GB
local 0.078GB
mongodb 0.078GB
mongodb2 0.078GB --新建立
test 0.203GB
wangshuai 0.078GB
>
3、fsync和鎖方式備份
上面的1,2兩點都不能保證備份時獲取資料的實時性,因為我們在備份的時候可能還有資料在記憶體緩衝區中沒有寫入到磁碟,MongoDB給我們提供了fsync+lock機制就能滿足我們的需求。
fsync(注意,只支援1.3+版本):該命令會強制伺服器將所有緩衝區中的內容寫入磁碟,
讓我們可以實時性獲取資料。
lock(寫入鎖):透過lock給資料庫一個寫入鎖,阻止對資料庫的進一步寫入操作,其他例項的寫入操作全部被阻
塞,直到釋放鎖為止。寫入鎖是讓fsync在備份時發揮作用的關鍵。
1、在shell中強制執行fsync並獲得lock(寫入鎖),在備份之前fsync並加鎖:
fsync並加鎖: db.runCommand({"fsync":1,"lock":1}),該命令用於admin db。
> use admin
switched to db admin
> db.runCommand({"fsync":1,"lock":1})
{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"seeAlso" : "",
"ok" : 1
}
>
該操作強制伺服器將所有緩衝區中的內容寫入磁碟並對資料庫上鎖,不允許執行寫資料操作,一般在執行資料庫備份時有用。
2、db.$cmd.sys.unlock.findOne():釋放鎖,備份好了就要解鎖,該命令用於admin db。
> db.$cmd.sys.unlock.findOne()
{ "ok" : 1, "info" : "unlock completed" }
>
3、db.currentOp():檢視當前鎖狀態
已經解鎖狀態:
> db.currentOp();
{ "inprog" : [ ] }
>
上鎖狀態:
> db.currentOp();
{
"inprog" : [ ],
"fsyncLock" : true,
"info" : "use db.fsyncUnlock() to terminate the fsync write/snapshot loc
k"
}
>
其中,fsyncLock為1表示MongoDB的fsync程式(負責將寫入改變同步到磁碟)不允許其他程式執行寫資料操作
fsync命令能非常靈活的備份,不用停掉伺服器,也不用犧牲備份的實時特性。要付出的代價就是一些寫入操作被暫時阻塞了。
唯一不耽誤讀寫還能保證實時快照的備份方式就是透過“從伺服器”備份。
4、從屬備份
在從伺服器上備份是MongoDB推薦的備份方式
</set
</set
1、冷備份與恢復——建立資料檔案的副本(前提是要停止MongoDB伺服器),也就是直接copy
MongoDB將所有資料都儲存在資料目錄下,預設是/data/db/(Windows下是C:\data\db\),啟動MongoDB時也可以用--dbpath指定我們自己設定的資料儲存目錄。
備份MongoDB資料:只要簡單的建立資料儲存目錄的副本就可以了,直接copy一份。
恢復MongoDB資料:在MongoDB啟動時用--dbpath指定資料儲存目錄副本位置。
在伺服器執行的情況下直接copy是有風險的,可能copy出來時,資料已經遭到破壞,這種方式下建立資料目錄的副本需要在關閉MongoDB伺服器的前提下,資料目錄中儲存的就是關閉那一刻資料的快照,在伺服器重新啟動之前可以複製目錄作為備份。
2、熱備份與恢復——MongoDB bin目錄下自帶的mongodump和mongorestore工具
mongodump是一種能在執行時備份的方法。該命令詳細引數如下:
C:\Users\duansf>mongodump --help
Export MongoDB data to BSON files.
Options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times
for more verbosity e.g. -vvvvv)
--quiet silence all non error diagnostic
messages
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set
name>/s1,s2 for sets)
--port arg server port. Can also use --host
hostname:port
--ipv6 enable IPv6 support (disabled by
default)
-u [ --username ] arg username
-p [ --password ] arg password
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg (=MONGODB-CR)
authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg Remote host name to use for purpose of
GSSAPI/Kerberos authentication
--dbpath arg directly access mongod database files
in the given path, instead of
connecting to a mongod server - needs
to lock the data directory, so cannot
be used if a mongod is currently
accessing the same path
--directoryperdb each db is in a separate directory
(relevant only if dbpath specified)
--journal enable journaling (relevant only if
dbpath specified)
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-o [ --out ] arg (=dump) output directory or "-" for stdout
-q [ --query ] arg json query
--oplog Use oplog for point-in-time
snapshotting
--repair try to recover a crashed database
--forceTableScan force a table scan (do not use
$snapshot)
--dumpDbUsersAndRoles Dump user and role definitions for the
given database
mongorestore獲取mongodump的輸出結果,並將備份的資料插入到執行的MongoDB例項中實現資料恢復。該命令詳細引數如下:
C:\Users\duansf>mongorestore --help
Import BSON files into MongoDB.
usage: mongorestore [options] [directory or filename to restore from]
Options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times
for more verbosity e.g. -vvvvv)
--quiet silence all non error diagnostic
messages
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set
name>/s1,s2 for sets)
--port arg server port. Can also use --host
hostname:port
--ipv6 enable IPv6 support (disabled by
default)
-u [ --username ] arg username
-p [ --password ] arg password
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg (=MONGODB-CR)
authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg Remote host name to use for purpose of
GSSAPI/Kerberos authentication
--dbpath arg directly access mongod database files
in the given path, instead of
connecting to a mongod server - needs
to lock the data directory, so cannot
be used if a mongod is currently
accessing the same path
--directoryperdb each db is in a separate directory
(relevant only if dbpath specified)
--journal enable journaling (relevant only if
dbpath specified)
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
--objcheck validate object before inserting
(default)
--noobjcheck don't validate object before inserting
--filter arg filter to apply before inserting
--drop drop each collection before import
--oplogReplay replay oplog for point-in-time restore
--oplogLimit arg include oplog entries before the
provided Timestamp (seconds[:ordinal])
during the oplog replay; the ordinal
value is optional
--keepIndexVersion don't upgrade indexes to newest version
--noOptionsRestore don't restore collection options
--noIndexRestore don't restore indexes
--restoreDbUsersAndRoles Restore user and role definitions for
the given database
--w arg (=0) minimum number of replicas per write
mongodump(備份)與mongorestore(恢復)示例:
1、建立mongodb資料庫:
C:\Users\duansf>mongo
MongoDB shell version: 2.6.6
connecting to: test
> use mongodb
switched to db mongodb
> db.mongodb.insert({'name':'duansf','age':'37'})
WriteResult({ "nInserted" : 1 })
> db.mongodb.find()
{ "_id" : ObjectId("56fcf2ee613bba5454cf1d83"), "name" : "duansf", "age" : "37"
}
>
2、mongodump備份mongodb資料庫:
C:\Users\duansf>mongodump -d mongodb -o backup
connected to: 127.0.0.1
2016-03-31T17:51:24.555+0800 DATABASE: mongodb to backup\mongodb
2016-03-31T17:51:24.567+0800 mongodb.system.indexes to backup\mongodb\system.indexes.bson
2016-03-31T17:51:24.604+0800 1 documents
2016-03-31T17:51:24.604+0800 mongodb.mongodb to backup\mongodb\mongodb.bson
2016-03-31T17:51:24.782+0800 1 documents
2016-03-31T17:51:24.784+0800 Metadata for mongodb.mongodb to backup\mongodb\mongodb.metadata.json
-d指定需要備份的資料庫,-o指定備份位置,上述表示備份mongodb資料庫到C:\Users\duansf\backup目錄下
3、mongorestore恢復mongodb資料庫,並重新命名為mongodb2資料庫:
C:\Users\duansf>mongorestore -d mongodb2 --drop backup/mongodb
connected to: 127.0.0.1
2016-03-31T17:56:47.802+0800 backup/mongodb\mongodb.bson
2016-03-31T17:56:47.804+0800 going into namespace [mongodb2.mongodb]
2016-03-31T17:56:47.804+0800 dropping
1 objects found
2016-03-31T17:56:47.808+0800 Creating index: { key: { _id: 1 }, name: "_id_",
ns: "mongodb2.mongodb" }
-d指定要恢復為的資料庫,可以將備份的資料庫恢復到與原來不同名的資料庫中,這裡為mongodb2,--drop表示在恢復前刪除集合(若存在)。
否則,資料就會與現有集合資料合併,可能會覆蓋一些文件。
此時在MongoDB shell下可以檢視到mongodb2資料庫,及其中的mongodb集合和資料文件。
C:\Users\duansf>mongo
MongoDB shell version: 2.6.6
connecting to: test
> show dbs
admin (empty)
chenfeng 0.078GB
dsf 0.078GB
duansf 0.078GB
idex_t 0.078GB
idx_t 0.078GB
local 0.078GB
mongodb 0.078GB
mongodb2 0.078GB --新建立
test 0.203GB
wangshuai 0.078GB
>
3、fsync和鎖方式備份
上面的1,2兩點都不能保證備份時獲取資料的實時性,因為我們在備份的時候可能還有資料在記憶體緩衝區中沒有寫入到磁碟,MongoDB給我們提供了fsync+lock機制就能滿足我們的需求。
fsync(注意,只支援1.3+版本):該命令會強制伺服器將所有緩衝區中的內容寫入磁碟,
讓我們可以實時性獲取資料。
lock(寫入鎖):透過lock給資料庫一個寫入鎖,阻止對資料庫的進一步寫入操作,其他例項的寫入操作全部被阻
塞,直到釋放鎖為止。寫入鎖是讓fsync在備份時發揮作用的關鍵。
1、在shell中強制執行fsync並獲得lock(寫入鎖),在備份之前fsync並加鎖:
fsync並加鎖: db.runCommand({"fsync":1,"lock":1}),該命令用於admin db。
> use admin
switched to db admin
> db.runCommand({"fsync":1,"lock":1})
{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"seeAlso" : "",
"ok" : 1
}
>
該操作強制伺服器將所有緩衝區中的內容寫入磁碟並對資料庫上鎖,不允許執行寫資料操作,一般在執行資料庫備份時有用。
2、db.$cmd.sys.unlock.findOne():釋放鎖,備份好了就要解鎖,該命令用於admin db。
> db.$cmd.sys.unlock.findOne()
{ "ok" : 1, "info" : "unlock completed" }
>
3、db.currentOp():檢視當前鎖狀態
已經解鎖狀態:
> db.currentOp();
{ "inprog" : [ ] }
>
上鎖狀態:
> db.currentOp();
{
"inprog" : [ ],
"fsyncLock" : true,
"info" : "use db.fsyncUnlock() to terminate the fsync write/snapshot loc
k"
}
>
其中,fsyncLock為1表示MongoDB的fsync程式(負責將寫入改變同步到磁碟)不允許其他程式執行寫資料操作
fsync命令能非常靈活的備份,不用停掉伺服器,也不用犧牲備份的實時特性。要付出的代價就是一些寫入操作被暫時阻塞了。
唯一不耽誤讀寫還能保證實時快照的備份方式就是透過“從伺服器”備份。
4、從屬備份
在從伺服器上備份是MongoDB推薦的備份方式
</set
</set
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2073272/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 備份與恢復:polardb資料庫備份與恢復資料庫
- NoSQL 資料庫案例實戰 -- MongoDB資料備份、恢復SQL資料庫MongoDB
- LightDB常用備份恢復命令說明
- Oracle邏輯備份與恢復選項說明Oracle
- 怎樣恢復Mac檔案及資料夾資料?BackupLoupe for mac(資料恢復備份助手)3.5.4Mac資料恢復
- mongodb資料庫備份與恢復(資料庫資料遷移)MongoDB資料庫
- Mysql資料備份與恢復MySql
- Mysql資料庫備份及恢復MySql資料庫
- 磁碟資料恢復及備份工具資料恢復
- 分散式文件儲存資料庫之MongoDB備份與恢復分散式資料庫MongoDB
- MongoDB日常運維-08備份與恢復MongoDB運維
- 如何備份及恢復Linux檔案許可權Linux
- 詳解叢集級備份恢復:物理細粒度備份恢復
- RabbitMQ如何備份與恢復資料MQ
- postgresql備份與恢復資料庫SQL資料庫
- 【北亞資料恢復】MongoDB資料遷移檔案丟失的MongoDB資料恢復案例資料恢復MongoDB
- RMAN備份恢復典型案例——資料檔案存在壞快
- Mongodb的備份恢復與匯出匯入MongoDB
- 資料庫備份與恢復技術資料庫
- RAC備份恢復之Voting備份與恢復
- 【資料庫資料恢復】MongoDB資料庫檔案損壞的資料恢復案例資料庫資料恢復MongoDB
- 資料庫備份恢復資料庫
- MySQL備份與恢復——實操MySql
- 從備份片中恢復某個指定得歸檔或者資料檔案
- 02 . MongoDB複製集,分片集,備份與恢復MongoDB
- 備份與恢復:Polardb資料庫資料基於時間點恢復資料庫
- 讀資料保護:工作負載的可恢復性04備份與檔案負載
- MySQL-19.資料庫備份與恢復MySql資料庫
- 12 使用RMAN備份和恢復檔案
- 實戰:xfs檔案系統的備份和恢復
- mongodb 備份恢復常用命令MongoDB
- XFS檔案系統的備份、恢復、修復
- MySQL備份與恢復——基於Xtrabackup物理備份恢復MySql
- 【資料庫資料恢復】mdb_catalog.wt檔案丟失的MongoDB資料恢復案例資料庫資料恢復MongoDB
- Jenkins備份與恢復Jenkins
- Postgresql 備份與恢復SQL
- MySQL 備份與恢復MySql
- Mysqldump備份說明及資料庫備份指令碼分享-運維筆記MySql資料庫指令碼運維筆記
- Docker Swarm 進階:資料卷備份與恢復DockerSwarm