mongodb是目前最流行的nosql資料庫,其自身也提供了備份與恢復命令 。具體程式為mongodump和mongorestore 。
一、mongodump備份
mongodump的具體用法可以檢視幫助:
error: required parameter is missing in 'host'
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)
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( /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
--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 if dbpath specified, each db is in a separate
directory
--journal enable journaling
-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)
幫助資訊上已經寫的很明了了 ,具體匯出備份命令為:
mongodump -h dbhost -d dbname -o dbdirectory
-h 表示mongodb server位址,
-d 表示需要備份的資料名
-o 為備份資料存放的路徑
如果設定了使用者名稱密碼還要使用-u和-p引數 ,如果想要匯出單獨庫下的乙個表,再增加-c引數 。具體用法同mysqldump有類似之處 。
注:如現在匯出的庫名為361way ,匯出的路徑為/opt ,則匯出後會在/opt目錄下有乙個361way命名的目錄 ,裡面是由表名命名的json和bson檔案 ,具體組成類似於表結構和表資料 。而匯出的內容和mysql 略有不同,mysqldump匯出的是乙個sql檔案而不是目錄 。
二、mongorestore恢復
mongorestore具體用法類以於mongodump,不過引數上略有差異,具體用法為:
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)
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( /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
--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 if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
--objcheck validate object before inserting
--drop drop each collection before import
--oplogreplay replay oplog for point-in-time restore
--oploglimit arg exclude oplog entries newer than provided timestamp
(epoch[:ordinal])
--keepindexversion don't upgrade indexes to newest version
--nooptionsrestore don't restore collection options
--noindexrestore don't restore indexes
--w arg (=1) minimum number of replicas per write
一般用法為:
mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
--directoryperdb:備份資料所在位置 ,例如上例中,我們將361way庫備份到了 /opt 下,這裡恢復的時候使用的就是/opt/361way ,此處是和備份略有區別的 。
--drop:恢復的時候,先刪除當前資料,然後恢復備份的資料
三、與其他備份恢復工具的對比
mongodb自帶的備份工具還有bsondump、mongoexport,恢復工具還有mongoimport ,幾者之間的具體區別是:
bsondump、mongoexport、mongodump備份工具的對比:
1、bsondump可以指定備份的格式為json和debug模式,這個命令雖然附帶,但很少用到 ;
2、mongoexport 可以匯出json或csv格式的檔案,可以指定查詢過濾器或指定輸出的域,不過此工具匯出的json,csv可能對某些資料型別不相容,因此可能不能全部資料匯出,mongodump就可以全部相容 ;
3、mongodump支援過濾 ,而且在匯出速度和壓縮率方面mongodump是最快最好的 。所以,若無csv或debug等特殊格式的備份需求,一般都使用 mongodump 作為備份工具 。
mongorestore與mongoimport 恢復工具的對比:
1、mongoimport 可以接受json,csv,tsv格式的檔案,每行為乙個物件 。同mongoexport一樣,其在恢復過程中同樣存在相容性的問題,所以有恢復不完整的概率 ;
2、mongorestore,速度較慢,比mongoimport慢2.5倍左右,但是根據mongodump匯出的資料,可以完整匯入資料。在restore過程中,索引根據之前dump的結果重新創造。
MongoDB 如何實現備份壓縮
資料庫的備份是災難恢復的最後一道屏障,不管什麼型別的資料庫都需要設定資料庫備份,mongodb也不例外。mongodb 3.0 後 資料庫可以採用wiredtiger儲存引擎後 3.2 版本預設 在此環境下通過mongodump 備份後,產生的備份檔案要遠大於資料儲存檔案的大小。此外,一般mongo...
MongoDB指令碼備份
shell指令碼直接採用mongodump進行備份,直接上 bin bash sourcepath usr bin destpath mongoback nowtime date y m d databasename hello collectionname world username test ...
mongodb備份恢復
參考 其中我在恢復的時候,寫成 mongorestore h 目標ip 目標埠 d index center drop c program files mongodb server 4.0 bin dump index center 命令是在mongodb安裝目錄下bin資料夾下執行的,上面語句紅色...