主頁(yè) > 知識(shí)庫(kù) > Windows或Linux系統(tǒng)中備份和恢復(fù)MongoDB數(shù)據(jù)的教程

Windows或Linux系統(tǒng)中備份和恢復(fù)MongoDB數(shù)據(jù)的教程

熱門(mén)標(biāo)簽:網(wǎng)站排名優(yōu)化 AI電銷(xiāo) 呼叫中心市場(chǎng)需求 地方門(mén)戶網(wǎng)站 服務(wù)外包 百度競(jìng)價(jià)排名 Linux服務(wù)器 鐵路電話系統(tǒng)

windows下面mongodb數(shù)據(jù)庫(kù)備份和恢復(fù)

我可以講數(shù)據(jù)備份到c:\data\dump目錄下面,首先創(chuàng)建這個(gè)路徑。然后進(jìn)入到mongodb的bin目錄下面

我的是:

C:\Program Files\mongodb\bin

備份腳本是:

//備份
mongodump -h 127.0.0.1:27017 -d test -o c:\data\dump

恢復(fù)腳本是:

//恢復(fù)
 mongorestore -h 127.0.0.1:27017 -d test --directoryperdb c:\data\dump\test

解釋一下用到的命令

  • -h:MongoDB所在服務(wù)器地址
  • -d:需要恢復(fù)的數(shù)據(jù)庫(kù)實(shí)例,例如:test,當(dāng)然這個(gè)名稱也可以和備份時(shí)候的不一樣,比如test2
  • -o:備份的數(shù)據(jù)存放位置,例如:c:\data\dump,當(dāng)然該目錄需要提前建立,在備份完成后,系統(tǒng)自動(dòng)在dump目錄下建立一個(gè)test目錄,這個(gè)目錄里面存放該數(shù)據(jù)庫(kù)實(shí)例的備份數(shù)據(jù)。
  • --directoryperdb:備份數(shù)據(jù)所在位置,例如:c:\data\dump\test,這里為什么要多加一個(gè)test,而不是備份時(shí)候的dump,讀者自己查看提示吧!
  • --drop:恢復(fù)的時(shí)候,先刪除當(dāng)前數(shù)據(jù),然后恢復(fù)備份的數(shù)據(jù)。就是說(shuō),恢復(fù)后,備份后添加修改的數(shù)據(jù)都會(huì)被刪除,慎用哦!

原始解釋:

 -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 ( 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
 --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 directly
                    (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
 --w arg (=0)             minimum number of replicas per write

linux下面mongodb數(shù)據(jù)庫(kù)備份和恢復(fù)

linux下面我們可以創(chuàng)建一個(gè)自動(dòng)備份腳本,可以設(shè)置定時(shí)任務(wù),也可以手動(dòng)備份。我是手動(dòng)備份的。

首先創(chuàng)建一個(gè)sh命令,我是放在home下面的。

vim /home/mongoBeiFen.sh

輸入如下內(nèi)容:

#!/bin/bash
shijie=`date +%Y%m%d%H`
backmongodbFile=mongodb$shijie.tar.gz
cd /home/mongoDbback/
/usr/local/mongo/bin/mongodump -h 127.0.0.1 --port 27017 -u mongo -p 123456 -d my_mongodb -o my_mongodb_dump/
tar czf $backmongodbFile my_mongodb_dump/
rm my_mongodb_dump -rf 

解釋:

存放備份的文件夾是/home/mongoDbback/

-u是數(shù)據(jù)庫(kù)名用戶名 -p是密碼 -d是數(shù)據(jù)庫(kù)名 具體和window差不多,大家可以看下上面windows的解釋。

備份的時(shí)候只要運(yùn)行一下

./mongoBeiFen.sh  

就可以了。

數(shù)據(jù)庫(kù)恢復(fù):

/usr/local/mongo/bin/mongorestore -d my_mongodb my_mongodb_dump/my_mongodb/* 

指向每個(gè)文件

復(fù)制代碼 代碼如下:

/usr/local/mongo/bin/mongorestore -h 127.0.0.1 --port 27017 -- drop --directoryperdb  my_mongodb_dump/my_mongodb

指向一個(gè)目錄
有問(wèn)題的話可以嘗試window的寫(xiě)法:

復(fù)制代碼 代碼如下:

/usr/local/mongo/bin/mongorestore -h 127.0.0.1:27017 -d test --drop  --directoryperdb  my_mongodb_dump/my_mongodb

您可能感興趣的文章:
  • mongoDB4.2.8備份恢復(fù)與導(dǎo)出導(dǎo)入(推薦)
  • 分布式文檔存儲(chǔ)數(shù)據(jù)庫(kù)之MongoDB備份與恢復(fù)的實(shí)踐詳解
  • 使用centos系統(tǒng)中的crontab命令對(duì)mongodb定時(shí)備份恢復(fù)
  • Mongodb實(shí)現(xiàn)定時(shí)備份與恢復(fù)的方法教程
  • MongoDB 導(dǎo)出導(dǎo)入備份恢復(fù)數(shù)據(jù)詳解及實(shí)例
  • MongoDB使用自帶的命令行工具進(jìn)行備份和恢復(fù)的教程
  • Mongodb數(shù)據(jù)庫(kù)的備份與恢復(fù)操作實(shí)例
  • MongoDB整庫(kù)備份與還原以及單個(gè)collection備份、恢復(fù)方法
  • MongoDB的備份與恢復(fù)

標(biāo)簽:湖南 黃山 銅川 蘭州 衡水 崇左 仙桃 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Windows或Linux系統(tǒng)中備份和恢復(fù)MongoDB數(shù)據(jù)的教程》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266