Skip to content

データベースをバックアップ・リストアする方法

Naoki KODAMA edited this page Dec 28, 2013 · 1 revision

バックアップ

devhub_db をバックアップする。以下のコマンドで dump フォルダが生成され、devhub_db のダンプ結果が保存される。 あとはこいつを tar.gz にして保存しておく。

$ mongodump --db devhub_db
connected to: 127.0.0.1
Sat Dec 28 17:54:17.113 DATABASE: devhub_db	 to 	dump/devhub_db
Sat Dec 28 17:54:17.120 	devhub_db.system.indexes to dump/devhub_db/system.indexes.bson
Sat Dec 28 17:54:17.127 		 4 objects
Sat Dec 28 17:54:17.127 	devhub_db.chat_log to dump/devhub_db/chat_log.bson
Sat Dec 28 17:54:17.160 		 1050 objects
Sat Dec 28 17:54:17.160 	Metadata for devhub_db.chat_log to dump/devhub_db/chat_log.metadata.json
Sat Dec 28 17:54:17.161 	devhub_db.latest_text to dump/devhub_db/latest_text.bson
Sat Dec 28 17:54:17.184 		 12 objects
Sat Dec 28 17:54:17.184 	Metadata for devhub_db.latest_text to dump/devhub_db/latest_text.metadata.json
Sat Dec 28 17:54:17.185 	devhub_db.active_number to dump/devhub_db/active_number.bson
Sat Dec 28 17:54:17.197 		 1 objects
Sat Dec 28 17:54:17.198 	Metadata for devhub_db.active_number to dump/devhub_db/active_number.metadata.json
Sat Dec 28 17:54:17.198 	devhub_db.text_log to dump/devhub_db/text_log.bson
Sat Dec 28 17:54:17.228 		 205 objects
Sat Dec 28 17:54:17.228 	Metadata for devhub_db.text_log to dump/devhub_db/text_log.metadata.json

$ ls
Procfile     README.md    app.js       dump         lib          memo         node_modules package.json post_memo.js static       views
$ cd dump/
$ ls
devhub_db

リストア

リストア対象のデータベースをドロップする(注意:データが全て消えるので慎重に!)

$ mongo
MongoDB shell version: 2.4.3
connecting to: test
> use devhub_db
switched to db devhub_db
> db.dropDatabase()
{ "dropped" : "devhub_db", "ok" : 1 }
> 

リストアする

$ mongorestore --db devhub_db dump/devhub_db
connected to: 127.0.0.1
Sat Dec 28 18:00:09.609 dump/devhub_db/active_number.bson
Sat Dec 28 18:00:09.609 	going into namespace [devhub_db.active_number]
1 objects found
Sat Dec 28 18:00:09.610 	Creating index: { key: { _id: 1 }, ns: "devhub_db.active_number", name: "_id_" }
Sat Dec 28 18:00:09.842 dump/devhub_db/chat_log.bson
Sat Dec 28 18:00:09.842 	going into namespace [devhub_db.chat_log]
1050 objects found
Sat Dec 28 18:00:09.854 	Creating index: { key: { _id: 1 }, ns: "devhub_db.chat_log", name: "_id_" }
Sat Dec 28 18:00:09.896 dump/devhub_db/latest_text.bson
Sat Dec 28 18:00:09.896 	going into namespace [devhub_db.latest_text]
12 objects found
Sat Dec 28 18:00:09.898 	Creating index: { key: { _id: 1 }, ns: "devhub_db.latest_text", name: "_id_" }
Sat Dec 28 18:00:09.899 dump/devhub_db/text_log.bson
Sat Dec 28 18:00:09.899 	going into namespace [devhub_db.text_log]
205 objects found
Sat Dec 28 18:00:09.904 	Creating index: { key: { _id: 1 }, ns: "devhub_db.text_log", name: "_id_" }

成功