1. 程式人生 > >MongoDB備份-mongodump和恢復-mongorestore

MongoDB備份-mongodump和恢復-mongorestore

備份

mongodump -h dbhost -d dbname -o dbdir

上述語法中:

-h MongDB所在伺服器,可以附帶指定埠號‘:port’

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

-o 備份資料存放的位置

如果以上引數都不指定,預設備份127.0.0.1埠號27017的所有資料庫到當前的dump目錄

C:\Users\Administrator>mongodump
2017-08-06T21:44:20.740+0800    writing admin.system.version to
2017-08-06T21:44:20.822+0800    done dumping admin.system.version (1 document)
2017-08-06T21:44:20.824+0800    writing db1.c1 to
2017-08-06T21:44:20.836+0800    writing db1.c2 to
2017-08-06T21:44:20.844+0800    writing db1.c3 to
2017-08-06T21:44:20.850+0800    done dumping db1.c1 (5 documents)
2017-08-06T21:44:20.852+0800    done dumping db1.c2 (5 documents)
2017-08-06T21:44:20.859+0800    done dumping db1.c3 (0 documents)
C:\Users\Administrator>dir dump
 驅動器 C 中的卷沒有標籤。
 卷的序列號是 94AA-6D4F
 
 C:\Users\Administrator\dump 的目錄
 
2017/08/06  21:44    <DIR>          .
2017/08/06  21:44    <DIR>          ..
2017/08/06  21:44    <DIR>          admin
2017/08/06  21:44    <DIR>          db1
               0 個檔案              0 位元組
               4 個目錄 17,894,645,760 可用位元組

恢復

mongorestore -h hostname:port -d dbname <path>

一些重要的引數

-h[:port] 指定MongoDB所在的伺服器地址,預設為localhost

-d 需要恢復的資料庫

--drop 恢復的時候先刪除當前資料庫,然後恢復備份,慎重操作

<path> 設定備份資料的位置,和下邊的引數有衝突

--dir 指定備份的目錄,和上邊的引數有衝突

C:\Users\Administrator>mongorestore --dir ./dump --drop
2017-08-06T21:51:35.869+0800    preparing collections to restore from
2017-08-06T21:51:35.939+0800    reading metadata for db1.c1 from dump\db1\c1.met
adata.json
2017-08-06T21:51:36.047+0800    reading metadata for db1.c2 from dump\db1\c2.met
adata.json
2017-08-06T21:51:36.125+0800    reading metadata for db1.c3 from dump\db1\c3.met
adata.json
2017-08-06T21:51:36.291+0800    restoring db1.c1 from dump\db1\c1.bson
2017-08-06T21:51:36.435+0800    restoring db1.c2 from dump\db1\c2.bson
2017-08-06T21:51:36.559+0800    restoring db1.c3 from dump\db1\c3.bson
2017-08-06T21:51:36.562+0800    no indexes to restore
2017-08-06T21:51:36.565+0800    finished restoring db1.c2 (5 documents)
2017-08-06T21:51:36.567+0800    no indexes to restore
2017-08-06T21:51:36.568+0800    finished restoring db1.c3 (0 documents)
2017-08-06T21:51:36.569+0800    restoring indexes for collection db1.c1 from met
adata
2017-08-06T21:51:36.831+0800    finished restoring db1.c1 (5 documents)
2017-08-06T21:51:36.833+0800    done

--------------------- 本文來自 朝聞道-夕死可矣 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/jc_benben/article/details/76794704?utm_source=copy