mongo-备份恢复
在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: { key: { _id: 1 }, ns: "testDB.test_tianyc012", name: "_id_" }
Wed Jan 09 15:12:25 Creating index: { key: { HashId: 1 }, ns: "testDB.test_tianyc012", name: "HashId_1" }
Wed Jan 09 15:12:28 Creating index: { key: { Time: 1 }, ns: "testDB.test_tianyc012", name: "Time_1" }
Wed Jan 09 15:12:30 Creating index: { key: { DeviceId: 1 }, 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 '{name:\"chen1\"}' --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版本中不可用。