MongoDBでコレクション単位でバックアップ/リストアする
コレクション単位でのバックアップ
--collection
でコレクション名を指定すれば、コレクション単位でバックアップしてくれる。
$ mongodump --db test --collection keywords
connected to: 127.0.0.1
Tue Aug 12 12:27:51.343 DATABASE: test to dump/test
Tue Aug 12 12:27:51.512 test.keywords to dump/test/keywords.bson
Tue Aug 12 12:27:51.596 325 objects
Tue Aug 12 12:27:51.597 Metadata for test.keywords to dump/test/keywords.metadata.json
コレクション単位でのリストア
普通は……
リストアもバックアップと同じ書式でできると思うわけですが、
$ mongorestore --db test --collection keywords --drop
connected to: 127.0.0.1
Tue Aug 12 12:29:42.842 ERROR: ERROR: root directory must be a dump of a single database
Tue Aug 12 12:29:42.842 ERROR: when specifying a db name with --db
Import BSON files into MongoDB.
....
エラーになる。
コレクションの.bsonファイルを指定してあげないといけない
今回試したmongodump
コマンドでは以下のファイル構成になっているので、
$ tree .
.
└── dump
└── test
├── keywords.bson
└── keywords.metadata.json
keywords.bsonを指定してあげないといけない。
正しいコマンド
$ mongorestore --db test --collection keywords --drop dump/test/keywords.bson
connected to: 127.0.0.1
Tue Aug 12 12:33:33.276 dump/test/keywords.bson
Tue Aug 12 12:33:33.276 going into namespace [test.keywords]
Tue Aug 12 12:33:33.276 dropping
325 objects found
Tue Aug 12 12:33:33.647 Creating index: { key: { _id: 1 }, ns: "test.keywords", name: "_id_" }
Tue Aug 12 12:33:33.656 Creating index: { key: { name: 1 }, unique: true, ns: "test.keywords", name: "name_1", background: true, safe: null }
DBもコレクションも引数で指定しているんだから、よしなにファイルを探してきてくれればいいのにねぇ……