1. 程式人生 > >centos下mongodb3.2資料庫的備份與恢復初步

centos下mongodb3.2資料庫的備份與恢復初步

資料庫的備份

mongodump -h dbhost -d dbname -o dbdirectory

  • -h MongDB所在伺服器地址,在本地操作時伺服器預設地址是127.0.0.1。
    這裡寫圖片描述

  • -d 需要備份的資料庫例項。

  • -o 備份的資料存放位置,在該位置下會成與你例項為名的資料夾,在資料夾下有collection 的bson和json檔案。
    這裡寫圖片描述

完成後可以輸入mongodump檢視
這裡寫圖片描述

資料庫的恢復

mongorestore -h dbhost -d dbname –dir dbdirectory

  • -h mongodb所在伺服器

  • -d 所需要備份的資料庫例項

  • -dir 資料庫備份的位置

  • – drop 先刪除當前資料庫的資料,然後恢復備份的資料

完成後輸入mongorestore檢視恢復資訊
這裡寫圖片描述

恢復的幾點注意

  • mongorestore can create a new database or add data to an existing database. However, mongorestore performs inserts only and does not perform updates. That is, if restoring documents to an existing database and collection and existing documents have the same value _id field as the to-be-restored documents, mongorestore will not overwrite those documents.(mongorestore可以建立一個新的資料庫或資料新增到現有的資料庫。然而,mongorestore只執行插入和不執行更新。也就是說,如果恢復檔案到現有的資料庫,並收集和現有的檔案具有相同的值_id欄位作為要被恢復的檔案,mongorestore不會覆蓋那些檔案。)

  • mongorestore recreates indexes recorded by mongodump.(mongorestore會建立mongodump錄索引。)

  • The data format used by mongodump from version 2.2 or later is incompatible with earlier versions of mongod. Do not use recent versions of mongodump to back up older data stores.(從2.2版本使用mongodump或更高版本的資料格式是與早期版本的mongod不相容。不要使用mongodump的最新版本的備份舊的資料儲存。)

  • mongorestore does not restore the system.profile collection data; however, if the backup data includes system.profile collection data and the collection does not exist in the target database, mongorestore creates the collection but does not insert any data into the collection.(mongorestore不會恢復system.profile收集資料;然而,如果備份資料包括system.profile集合資料和集合未在目標資料庫中存在,mongorestore建立集合,但不將任何資料插入到集合中。)
    -