mongodb資料庫資料匯入匯出備份教程

來源:互聯網
上載者:User

windows下面mongodbDatabase Backup和恢複

我可以講資料備份到c:\data\dump目錄下面,首先建立這個路徑。然後進入到mongodb的bin目錄下面

我的是:

C:\Program Files\mongodb\bin

備份指令碼是:

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

恢複指令碼是:

//恢複
 mongorestore -h 127.0.0.1:27017 -d test --directoryperdb  c:\data\dump\test

解釋一下用到的命令

-h:MongoDB所在伺服器位址
-d:需要恢複的資料庫執行個體,例如:test,當然這個名稱也可以和備份時候的不一樣,比如test2
-o:備份的資料存放位置,例如:c:\data\dump,當然該目錄需要提前建立,在備份完成後,系統自動在dump目錄下建立一個test目錄,這個目錄裡面存放該資料庫執行個體的備份資料。
--directoryperdb:備份資料所在位置,例如:c:\data\dump\test,這裡為什麼要多加一個test,而不是備份時候的dump,讀者自己查看提示吧!
--drop:恢複的時候,先刪除當前資料,然後恢複備份的資料。就是說,恢複後,備份後添加修改的資料都會被刪除,慎用哦!

原始解釋:

  -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下面mongodbDatabase Backup和恢複

linux下面我們可以建立一個自動備份指令碼,可以設定定時任務,也可以手動備份。我是手動備份的。

首先建立一個sh命令,我是放在home下面的。

vim /home/mongoBeiFen.sh

輸入如下內容:

#!/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是資料庫名使用者名稱 -p是密碼 -d是資料庫名 具體和window差不多,大家可以看下上面windows的解釋。

備份的時候只要運行一下

./mongoBeiFen.sh  
就可以了。

資料庫恢複:

/usr/local/mongo/bin/mongorestore -d my_mongodb my_mongodb_dump/my_mongodb/*            指向每個檔案
/usr/local/mongo/bin/mongorestore -h 127.0.0.1 --port 27017 -- drop --directoryperdb  my_mongodb_dump/my_mongodb 指向一個目錄
有問題的話可以嘗試window的寫法:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.