前回まででshardingまで出来たので
とりあえずバックアップ系の操作を覚えることにする
mongodumpっていうのもあるみたいだけど
まずはmongoexportとmongoimportを使ってみる
使用するデータは↓(実際は100000程)
mongos> db.sample.find().sort({id:1}).limit(10)
{ "_id" : ObjectId("51944e5d62bd6d4103103b51"), "id" : 1, "name" : "nanashi_1", "created_on" : ISODate("2013-05-16T03:11:25.257Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b52"), "id" : 2, "name" : "nanashi_2", "created_on" : ISODate("2013-05-16T03:11:25.258Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b53"), "id" : 3, "name" : "nanashi_3", "created_on" : ISODate("2013-05-16T03:11:25.258Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b54"), "id" : 4, "name" : "nanashi_4", "created_on" : ISODate("2013-05-16T03:11:25.258Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b55"), "id" : 5, "name" : "nanashi_5", "created_on" : ISODate("2013-05-16T03:11:25.258Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b56"), "id" : 6, "name" : "nanashi_6", "created_on" : ISODate("2013-05-16T03:11:25.258Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b57"), "id" : 7, "name" : "nanashi_7", "created_on" : ISODate("2013-05-16T03:11:25.259Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b58"), "id" : 8, "name" : "nanashi_8", "created_on" : ISODate("2013-05-16T03:11:25.259Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b59"), "id" : 9, "name" : "nanashi_9", "created_on" : ISODate("2013-05-16T03:11:25.259Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b5a"), "id" : 10, "name" : "nanashi_10", "created_on" : ISODate("2013-05-16T03:11:25.259Z") }
まずはjson形式でexportしてみる
# mongoexport -h localhost --port 10000 --db testdb --collection sample -o test.json
connected to: localhost:10000
exported 100000 records
中身確認
# wc -l test.json
100000 test.json
# head test.json
{ "_id" : { "$oid" : "51944e5d62bd6d4103103b51" }, "id" : 1, "name" : "nanashi_1", "created_on" : { "$date" : 1368673885257 } }
{ "_id" : { "$oid" : "51944e5e62bd6d41031054ea" }, "id" : 6554, "name" : "nanashi_6554", "created_on" : { "$date" : 1368673886404 } }
{ "_id" : { "$oid" : "51944e6662bd6d4103113b54" }, "id" : 65540, "name" : "nanashi_65540", "created_on" : { "$date" : 1368673894553 } }
{ "_id" : { "$oid" : "51944e5d62bd6d4103103b52" }, "id" : 2, "name" : "nanashi_2", "created_on" : { "$date" : 1368673885258 } }
{ "_id" : { "$oid" : "51944e5e62bd6d41031054eb" }, "id" : 6555, "name" : "nanashi_6555", "created_on" : { "$date" : 1368673886405 } }
{ "_id" : { "$oid" : "51944e6662bd6d4103113b55" }, "id" : 65541, "name" : "nanashi_65541", "created_on" : { "$date" : 1368673894553 } }
{ "_id" : { "$oid" : "51944e5d62bd6d4103103b53" }, "id" : 3, "name" : "nanashi_3", "created_on" : { "$date" : 1368673885258 } }
{ "_id" : { "$oid" : "51944e5e62bd6d41031054ec" }, "id" : 6556, "name" : "nanashi_6556", "created_on" : { "$date" : 1368673886405 } }
{ "_id" : { "$oid" : "51944e6662bd6d4103113b56" }, "id" : 65542, "name" : "nanashi_65542", "created_on" : { "$date" : 1368673894553 } }
{ "_id" : { "$oid" : "51944e5d62bd6d4103103b54" }, "id" : 4, "name" : "nanashi_4", "created_on" : { "$date" : 1368673885258 } }
問題無さそう
ちなみに「--jsonArray」をつけると一行出力になる
続いてcsv形式でexport
「--csv」これつけただけ
# mongoexport -h localhost --port 10000 --db testdb --csv --collection sample -o test.csv
connected to: localhost:10000
assertion: 9998 you need to specify fields
エラー
はて、、、
helpを見てみるとfield指定のオプションがあるので指定してみる
# mongoexport -h localhost --port 10000 --db testdb --csv --collection sample -o test.csv -f id,name,created_on
connected to: localhost:10000
exported 100000 records
おお取れた
中身確認
# wc -l test.csv
100001 test.csv
# head test.csv
id,name,created_on
1.0,"nanashi_1",2013-05-16T03:11:25Z
6554.0,"nanashi_6554",2013-05-16T03:11:26Z
65540.0,"nanashi_65540",2013-05-16T03:11:34Z
2.0,"nanashi_2",2013-05-16T03:11:25Z
6555.0,"nanashi_6555",2013-05-16T03:11:26Z
65541.0,"nanashi_65541",2013-05-16T03:11:34Z
3.0,"nanashi_3",2013-05-16T03:11:25Z
6556.0,"nanashi_6556",2013-05-16T03:11:26Z
65542.0,"nanashi_65542",2013-05-16T03:11:34Z
ちゃんと入ってる
では続いてimport
まずはjsonの方から
# mongoimport -h localhost --port 10000 --db jsontest --collection sample --file test.json
connected to: localhost:10000
Fri May 17 11:00:56.001 Progress: 7049788/13577790 51%
Fri May 17 11:00:56.001 52000 17333/second
Fri May 17 11:00:58.048 check 9 100000
Fri May 17 11:00:58.389 imported 100000 objects
成功
データ確認
# mongo localhost:10000/jsontest
mongos> db.sample.count()
100000
mongos> db.sample.find().sort({id:1}).limit(10)
{ "_id" : ObjectId("51944e5d62bd6d4103103b51"), "id" : 1, "name" : "nanashi_1", "created_on" : ISODate("2013-05-16T03:11:25.257Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b52"), "id" : 2, "name" : "nanashi_2", "created_on" : ISODate("2013-05-16T03:11:25.258Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b53"), "id" : 3, "name" : "nanashi_3", "created_on" : ISODate("2013-05-16T03:11:25.258Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b54"), "id" : 4, "name" : "nanashi_4", "created_on" : ISODate("2013-05-16T03:11:25.258Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b55"), "id" : 5, "name" : "nanashi_5", "created_on" : ISODate("2013-05-16T03:11:25.258Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b56"), "id" : 6, "name" : "nanashi_6", "created_on" : ISODate("2013-05-16T03:11:25.258Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b57"), "id" : 7, "name" : "nanashi_7", "created_on" : ISODate("2013-05-16T03:11:25.259Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b58"), "id" : 8, "name" : "nanashi_8", "created_on" : ISODate("2013-05-16T03:11:25.259Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b59"), "id" : 9, "name" : "nanashi_9", "created_on" : ISODate("2013-05-16T03:11:25.259Z") }
{ "_id" : ObjectId("51944e5d62bd6d4103103b5a"), "id" : 10, "name" : "nanashi_10", "created_on" : ISODate("2013-05-16T03:11:25.259Z") }
問題なし
続いてcsv形式のimport
# mongoimport -h localhost --port 10000 --db csvtest --collection sample --type csv -f id,name,created_on --file test.csv
connected to: localhost:10000
Fri May 17 11:04:07.025 Progress: 2997262/4477809 66%
Fri May 17 11:04:07.025 67100 22366/second
Fri May 17 11:04:08.483 check 9 100001
Fri May 17 11:04:08.930 imported 100001 objects
ん?10001objects?
# mongo localhost:10000/csvtest
mongos> db.sample.find()
{ "_id" : ObjectId("519590143a6296f2ee5e2e61"), "id" : "id", "name" : "name", "created_on" : "created_on" }
{ "_id" : ObjectId("519590143a6296f2ee5e2e62"), "id" : 1, "name" : "nanashi_1", "created_on" : "2013-05-16T03:11:25Z" }
まさかのフィールド名部分までimport
help確認すると「--headerline」があった
# mongoimport -h localhost --port 10000 --db csvtest --collection sample2 --type csv -f id,name,created_on --file test.csv --headerline
connected to: localhost:10000
Fri May 17 11:07:24.000 Progress: 2907262/4477809 64%
Fri May 17 11:07:24.000 65100 21700/second
Fri May 17 11:07:25.607 check 9 100001
Fri May 17 11:07:25.960 imported 100000 objects
今度は10000objectsになった
# mongo localhost:10000/csvtest
mongos> db.sample2.count()
100000
mongos> db.sample2.find().sort({id:1}).limit(10)
{ "_id" : ObjectId("519590d93a6296f2ee5fb502"), "id" : 1, "name" : "nanashi_1", "created_on" : "2013-05-16T03:11:25Z" }
{ "_id" : ObjectId("519590d93a6296f2ee5fb505"), "id" : 2, "name" : "nanashi_2", "created_on" : "2013-05-16T03:11:25Z" }
{ "_id" : ObjectId("519590d93a6296f2ee5fb508"), "id" : 3, "name" : "nanashi_3", "created_on" : "2013-05-16T03:11:25Z" }
{ "_id" : ObjectId("519590d93a6296f2ee5fb50b"), "id" : 4, "name" : "nanashi_4", "created_on" : "2013-05-16T03:11:25Z" }
{ "_id" : ObjectId("519590d93a6296f2ee5fb50e"), "id" : 5, "name" : "nanashi_5", "created_on" : "2013-05-16T03:11:25Z" }
{ "_id" : ObjectId("519590d93a6296f2ee5fb511"), "id" : 6, "name" : "nanashi_6", "created_on" : "2013-05-16T03:11:25Z" }
{ "_id" : ObjectId("519590d93a6296f2ee5fb514"), "id" : 7, "name" : "nanashi_7", "created_on" : "2013-05-16T03:11:25Z" }
{ "_id" : ObjectId("519590d93a6296f2ee5fb517"), "id" : 8, "name" : "nanashi_8", "created_on" : "2013-05-16T03:11:25Z" }
{ "_id" : ObjectId("519590d93a6296f2ee5fb51a"), "id" : 9, "name" : "nanashi_9", "created_on" : "2013-05-16T03:11:25Z" }
{ "_id" : ObjectId("519590d93a6296f2ee5fb51d"), "id" : 10, "name" : "nanashi_10", "created_on" : "2013-05-16T03:11:25Z" }
今度は問題なし。
とりあえず完了
次はmongodumpやろう