-d是指定数据库,-o是输出备份文件,上面将test数据库备份为test_data文件。
[tp0352@server0 bin]$ ./mongorestore --port 9352 -d temple --drop test_data/test/
connected to: 127.0.0.1:9352
2014-06-05T19:05:53.454+0800 test_data/test/people.bson
2014-06-05T19:05:53.454+0800 going into namespace [temple.people]
2014-06-05T19:05:53.454+0800 dropping
2 objects found
2014-06-05T19:05:53.454+0800 Creating index: { key: { _id: 1 }, name: "_id_", ns: "temple.people" }
[tp0352@server0 bin]$ ./mongo --port 9352
MongoDB shell version: 2.6.1
connecting to: 127.0.0.1:9352/test
> show dbs
admin 0.078GB
local 0.078GB
temple 0.078GB
test 0.078GB
这里将上面备份出来的test数据库现在重新导入到temple数据库,--drop代表如果有了temple数据库则将其中的所有集合删除,不指定就会和原来temple中的集合合并。
③.mongoexport和mongoimport
上面讲到mongodump和mongorestore是备份某个数据库的,那么mongoexport和mongoimport就是备份某个数据库中的某个表,同样可以通过--help来查看所有的选项,当然mongoexport也是可以不统计的备份,但是却不一定是最新数据。
[tp0352@server0 bin]$ ./mongoexport --port 9352 -d test -c people -o prson
connected to: 127.0.0.1:9352
exported 2 records
[tp0352@server0 bin]$ cat prson
{ "_id" : { "$oid" : "53903f7af73bb0df22f8e4a6" }, "name" : "Mary", "age" : 10 }
{ "_id" : { "$oid" : "53903ff6959e902814f56d8d" }, "name" : "join", "age" : 20 }
[tp0352@server0 bin]$ ./mongoimport --port 9352 -d temple -c user prson
connected to: 127.0.0.1:9352
2014-06-05T19:14:00.555+0800 imported 2 objects
[tp0352@server0 bin]$ ./mongo --port 9352
MongoDB shell version: 2.6.1
connecting to: 127.0.0.1:9352/test
> use temple
switched to db temple
> show collections
system.indexes
user
> db.user.find()
{ "_id" : ObjectId("53903f7af73bb0df22f8e4a6"), "name" : "Mary", "age" : 10 }
{ "_id" : ObjectId("53903ff6959e902814f56d8d"), "name" : "join", "age" : 20 }
-c表示collection集合,上面将test库中的people集合备份出来为prson文件,然后再导入到temple库中的user集合。
④.fsync和锁
mongodump和mongoexport对都可以不停MongoDB服务器来进行备份,但是却失去了获取实时数据的能力,而fsync命令也能在不停MongoDB的情况下备份实时数据,它的实现其实就是上锁,阻止对数据库的写入操作,然后将缓冲区的数据写入磁盘,备份后再解锁。在这个期间,任何对数据库的写入操作都会阻塞,直到锁被释放。
> db.runCommand({"fsync" : 1, "lock" : 1})
{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
"ok" : 1
}
>
>在这之间进行备份,执行任何insert操作都会阻塞
>
> db.fsyncUnlock()
{ "ok" : 1, "info" : "unlock completed" }
我觉得其实实时也是相对的,将数据库锁住不让写,那么最新的数据还是没备份到。