您的位置:首页 > 数据库 > Mongodb

MongoDB 数据库操作(八)-数据备份和恢复

2013-04-09 15:34 513 查看
1. 备份

可以用mongodump 来做MongoDB 的库或表级别的备份。

备份数据库:

./mongodump -d my_mongodb
这个操作默认将会保持数据到当前目录下的dump文件夹内。使用这个命令会有相应的提示信息:

./mongodump -d test
connected to: 127.0.0.1
Tue Apr  2 09:11:45 DATABASE: test       to     dump/test
Tue Apr  2 09:11:45     test.system.js to dump/test/system.js.bson
Tue Apr  2 09:11:45              3 objects
Tue Apr  2 09:11:45     Metadata for test.system.js to dump/test/system.js.metadata.json
Tue Apr  2 09:11:45     test.fs.files to dump/test/fs.files.bson
Tue Apr  2 09:11:45              1 objects
Tue Apr  2 09:11:45     Metadata for test.fs.files to dump/test/fs.files.metadata.json
Tue Apr  2 09:11:45     test.fs.chunks to dump/test/fs.chunks.bson
Tue Apr  2 09:11:45              1 objects
Tue Apr  2 09:11:45     Metadata for test.fs.chunks to dump/test/fs.chunks.metadata.json
Tue Apr  2 09:11:45     test.result to dump/test/result.bson
Tue Apr  2 09:11:45              4 objects
Tue Apr  2 09:11:45     Metadata for test.result to dump/test/result.metadata.json
Tue Apr  2 09:11:45     test.things to dump/test/things.bson
Tue Apr  2 09:11:45              14 objects
Tue Apr  2 09:11:45     Metadata for test.things to dump/test/things.metadata.json
Tue Apr  2 09:11:45     test.things2 to dump/test/things2.bson
Tue Apr  2 09:11:45              14 objects
Tue Apr  2 09:11:45     Metadata for test.things2 to dump/test/things2.metadata.json
一般备份会产生两个文件:一个是元数据的json文件,存储格式信息,另一个事Bson文件,存储数据内容。

还可以加入-o参数来指定备份位置:

./mongodump -d my_mongodb -o my_mongodb_dump


2.数据恢复

先将数据库删掉:

> db.getName();
test
> db.dropDatabase();
{ "dropped" : "test", "ok" : 1 }


进行恢复:

./mongorestore -d t                                                                       est dump/*
connected to: 127.0.0.1
Tue Apr  2 09:20:12 dump/test/system.js.bson
Tue Apr  2 09:20:12     going into namespace [test.system.js]
3 objects found
Tue Apr  2 09:20:12     Creating index: { key: { _id: 1 }, ns: "test.system.js",                                                                        name: "_id_" }
Tue Apr  2 09:20:12 dump/test/things2.bson
Tue Apr  2 09:20:12     going into namespace [test.things2]
14 objects found
Tue Apr  2 09:20:12     Creating index: { key: { _id: 1 }, ns: "test.things2", n                                                                       ame: "_id_" }
Tue Apr  2 09:20:12 dump/test/fs.files.bson
Tue Apr  2 09:20:12     going into namespace [test.fs.files]
1 objects found
Tue Apr  2 09:20:12     Creating index: { key: { _id: 1 }, ns: "test.fs.files",                                                                        name: "_id_" }
Tue Apr  2 09:20:12     Creating index: { key: { filename: 1 }, ns: "test.fs.fil                                                                       es", name: "filename_1" }
Tue Apr  2 09:20:12 dump/test/result.bson
Tue Apr  2 09:20:12     going into namespace [test.result]
4 objects found
Tue Apr  2 09:20:12     Creating index: { key: { _id: 1 }, ns: "test.result", na                                                                       me: "_id_" }
Tue Apr  2 09:20:12 dump/test/things.bson
Tue Apr  2 09:20:12     going into namespace [test.things]
14 objects found
Tue Apr  2 09:20:12     Creating index: { key: { _id: 1 }, ns: "test.things", na                                                                       me: "_id_" }
Tue Apr  2 09:20:12 dump/test/fs.chunks.bson
Tue Apr  2 09:20:12     going into namespace [test.fs.chunks]
1 objects found
Tue Apr  2 09:20:12     Creating index: { key: { _id: 1 }, ns: "test.fs.chunks",                                                                        name: "_id_" }
Tue Apr  2 09:20:12     Creating index: { key: { files_id: 1, n: 1 }, ns: "test.                                                                       fs.chunks", name: "files_id_1_n_1" }


查看结果:
> show dbs
Cache   6.0126953125GB
admin   (empty)
local   (empty)
test    0.203125GB
> db.getName();
test
> show collections;
fs.chunks
fs.files
result
system.indexes
system.js
things
things2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐