MongoDBバックアップ

mongodbのバックアップツールを使ってみた。

データを入れる

mongo shellはJavascriptのshellらしい。

root@johan:~# /usr/local/mongodb/bin/mongo
MongoDB shell version: 2.0.2
connecting to: test
> use mydb
switched to db mydb
> db.mycoll.remove()
> var chars = "abcdefghijklmnopqrstuvwxyz";
> for (var i = 0; i < chars.length; i++) {
... var char = chars.substr(i, 1);
... var doc = {char:char, code:char.charCodeAt(0)};
... db.mycoll.insert(doc);
... }
> db.mycoll.find()
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e2"), "char" : "a", "code" : 97 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e3"), "char" : "b", "code" : 98 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e4"), "char" : "c", "code" : 99 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e5"), "char" : "d", "code" : 100 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e6"), "char" : "e", "code" : 101 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e7"), "char" : "f", "code" : 102 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e8"), "char" : "g", "code" : 103 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e9"), "char" : "h", "code" : 104 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1ea"), "char" : "i", "code" : 105 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1eb"), "char" : "j", "code" : 106 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1ec"), "char" : "k", "code" : 107 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1ed"), "char" : "l", "code" : 108 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1ee"), "char" : "m", "code" : 109 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1ef"), "char" : "n", "code" : 110 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f0"), "char" : "o", "code" : 111 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f1"), "char" : "p", "code" : 112 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f2"), "char" : "q", "code" : 113 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f3"), "char" : "r", "code" : 114 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f4"), "char" : "s", "code" : 115 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f5"), "char" : "t", "code" : 116 }
has more
> it
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f6"), "char" : "u", "code" : 117 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f7"), "char" : "v", "code" : 118 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f8"), "char" : "w", "code" : 119 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f9"), "char" : "x", "code" : 120 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1fa"), "char" : "y", "code" : 121 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1fb"), "char" : "z", "code" : 122 }
> exit
bye

mongodump

hostのみ指定すると全DB、全Collectionをdumpする。
localhost:27017(default)の場合はmongodumpのみでいいみたい。
mongodumpが完了するとcurrent directoryにdumpというディレクトリができる。

root@johan:~# /usr/local/mongodb/bin/mongodump --host localhost
connected to: localhost
all dbs
DATABASE: mydb	 to 	dump/mydb
	mydb.system.indexes to dump/mydb/system.indexes.bson
		 1 objects
	mydb.mycoll to dump/mydb/mycoll.bson
		 26 objects
DATABASE: admin	 to 	dump/admin
	admin.system.users to dump/admin/system.users.bson
		 1 objects
	admin.system.indexes to dump/admin/system.indexes.bson
		 1 objects
DATABASE: *	 to 	dump/*
root@johan:~# ls -l ./dump
合計 12
drwxr-xr-x 2 root root 4096 2012-02-21 22:13 *
drwxr-xr-x 2 root root 4096 2012-02-21 22:13 admin
drwxr-xr-x 2 root root 4096 2012-02-21 22:13 mydb

データを消す

mycollの全てのCollectionを消しておく。

root@johan:~# /usr/local/mongodb/bin/mongo
MongoDB shell version: 2.0.2
connecting to: test
> use mydb
switched to db mydb
> db.mycoll.remove()
> db.mycoll.find()
> exit
bye

mongorestore

root@johan:~# /usr/local/mongodb/bin/mongorestore --db mydb --drop dump/mydb
connected to: 127.0.0.1
Tue Feb 21 22:20:25 dump/mydb/mycoll.bson
Tue Feb 21 22:20:25 	 going into namespace [mydb.mycoll]
Tue Feb 21 22:20:25 	 dropping
26 objects found
Tue Feb 21 22:20:25 dump/mydb/system.indexes.bson
Tue Feb 21 22:20:25 	 going into namespace [mydb.system.indexes]
Tue Feb 21 22:20:25 	 dropping
Tue Feb 21 22:20:25 { key: { _id: 1 }, ns: "mydb.mycoll", name: "_id_" }
1 objects found

確認

root@johan:~# /usr/local/mongodb/bin/mongo
MongoDB shell version: 2.0.2
connecting to: test
> use mydb
switched to db mydb
> db.mycoll.find()
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e2"), "char" : "a", "code" : 97 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e3"), "char" : "b", "code" : 98 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e4"), "char" : "c", "code" : 99 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e5"), "char" : "d", "code" : 100 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e6"), "char" : "e", "code" : 101 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e7"), "char" : "f", "code" : 102 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e8"), "char" : "g", "code" : 103 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1e9"), "char" : "h", "code" : 104 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1ea"), "char" : "i", "code" : 105 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1eb"), "char" : "j", "code" : 106 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1ec"), "char" : "k", "code" : 107 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1ed"), "char" : "l", "code" : 108 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1ee"), "char" : "m", "code" : 109 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1ef"), "char" : "n", "code" : 110 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f0"), "char" : "o", "code" : 111 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f1"), "char" : "p", "code" : 112 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f2"), "char" : "q", "code" : 113 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f3"), "char" : "r", "code" : 114 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f4"), "char" : "s", "code" : 115 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f5"), "char" : "t", "code" : 116 }
has more
> it
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f6"), "char" : "u", "code" : 117 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f7"), "char" : "v", "code" : 118 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f8"), "char" : "w", "code" : 119 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1f9"), "char" : "x", "code" : 120 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1fa"), "char" : "y", "code" : 121 }
{ "_id" : ObjectId("4f439c9f0ab5adc73e48d1fb"), "char" : "z", "code" : 122 }
> exit
bye