mongo 備份恢復

2021-09-08 16:37:22 字數 2957 閱讀 2973

在mongo2.2.2版本中,mongodump的collections檔案,會同步生成乙個metadata.json檔案。這與之前的版本不同。

目前檢視到的該json檔案中只包含了索引資訊。該資訊在mongorestore時會使用到,恢復完畢資料後,會建立索引。

但該json檔案中不包含分片資訊,所以如果恢復集群中的表,還是需要先建好分片表,然後再執行mongorestore(執行時不要用drop引數)。

c:\>mongodump -h 192.168.69.205 --port 1234 -d testdb -c test_tianyc01 -o d:\tianyuechao

connected to: 192.168.69.205:20000

wed jan 09 14:53:40 database: testdb to d:/tianyuechao/testdb

wed jan 09 14:53:40 testdb.test_tianyc01 to d:/tianyuechao/testdb/test_tianyc01.bson

wed jan 09 14:53:43 38600/412162 9% (objects)

wed jan 09 14:53:46 112800/412162 27% (objects)

wed jan 09 14:53:49 200300/412162 48% (objects)

wed jan 09 14:53:52 288100/412162 69% (objects)

wed jan 09 14:53:55 375900/412162 91% (objects)

wed jan 09 14:53:56 412162 objects

wed jan 09 14:53:56

metadata for testdb.test_tianyc01 to d:/tianyuechao/testdb/test_tianyc01.metadata.json

c:\>c:\\mongorestore -h 192.168.69.205 --port 1234 -d testdb -c test_tianyc012 --directoryperdb d:\tianyuechao\testdb\test_tianyc01.bson --drop

connected to: 192.168.69.205:20000

wed jan 09 15:11:51 d:/tianyuechao/testdb/test_tianyc01.bson

wed jan 09 15:11:51 going into namespace [testdb.test_tianyc012]

wed jan 09 15:11:51 dropping

wed jan 09 15:11:55 11259408/151462473 7% (bytes)

wed jan 09 15:11:58 26932708/151462473 17% (bytes)

wed jan 09 15:12:01 42511433/151462473 28% (bytes)

wed jan 09 15:12:04 57169089/151462473 37% (bytes)

wed jan 09 15:12:07 64724582/151462473 42% (bytes)

wed jan 09 15:12:10 79515232/151462473 52% (bytes)

wed jan 09 15:12:13 94096364/151462473 62% (bytes)

wed jan 09 15:12:16 109569974/151462473 72% (bytes)

wed jan 09 15:12:19 117736371/151462473 77% (bytes)

wed jan 09 15:12:22 133712964/151462473 88% (bytes)

wed jan 09 15:12:25 149328089/151462473 98% (bytes)

412162 objects found

wed jan 09 15:12:25 creating index: , ns: "testdb.test_tianyc012", name: "_id_" }

wed jan 09 15:12:25 creating index: , ns: "testdb.test_tianyc012", name: "hashid_1" }

wed jan 09 15:12:28 creating index: , ns: "testdb.test_tianyc012", name: "time_1" }

wed jan 09 15:12:30 creating index: , ns: "testdb.test_tianyc012", name: "deviceid_1" }

建議備份和恢復,客戶端和伺服器使用同一版本。雖然測試了2.0.8和2.2.2不同版本也可以。

恢復時還可以加上 filter 引數,進行選擇性恢復(json中的字串記得用反斜槓轉義):

c:\>mongorestore -h 192.168.69.205:1234 -d testdb -c tianyc_test2 -directoryperdb g:\tmp\testdb\csptest.bson --filter '' --drop

connected to: 10.0.0.36:20000

thu jan 10 11:15:54 g:/tmp/testdb/csptest.bson

thu jan 10 11:15:54 going into namespace [testdb.tianyc_test2]

thu jan 10 11:15:54 dropping

thu jan 10 11:15:54 900000 objects found

thu jan 10 11:15:54 3 objects processed

不過經過測試,filter引數只在1.8.3版本中有效,在2.0.8和2.2.2版本中不可用。

mongo自動備份指令碼和恢復

一 建立mongodb備份目錄 mkdir p data mongodb bak mongodb bak now mkdir p data mongodb bak mongodb bak list 二 新建mongodb資料庫備份指令碼 data mongodb bak mongodb bak.sh...

Mongo03 備份與恢復

使用mongoexport命令,將乙個collection匯出為json格式或csv格式的檔案。從mongodb4.2開始,mongoexport就不再隨著mongodb一起安裝了,需要單獨安裝,各平台安裝位址在這裡。要注意,linux和macos系統中,需要將工具拷貝到 path環境變數的目錄,也...

mongo備份恢復(單機和複製集)

邏輯匯出,可以作做跨版本和公升級匯出 mongoexport uroot p port 27021 d test c t1 authenticationdatabase admin o service t1.json 匯出csv 格式 f 後跟列名 j 開啟併發匯入 mongoexport uroo...