通过mongodump和mongorestore实现Mongodb备份和恢复
Mongodb自带了mongodump和mongorestore这两个工具来实现对数据的备份和恢复。
mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询,然后将所有查到的文档写入磁盘。但是存在的问题时使用mongodump产生的备份不一定是数据库的实时快照,如果我们在备份时对数据库进行了写入操作,则备份出来的文件可能不完全和Mongodb实时数据相等。另外在备份时可能会对其它客户端性能产生不利的影响。
mongodump用法如下:
[root@localhost mongodb]# ./bin/mongodump --help
Export MongoDB data to BSON files. options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for
sets)
--port arg server port. Can also use --host hostname:port
--ipv6 enable IPv6 support (disabled by default)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server -
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-o [ --out ] arg (=dump) output directory or "-" for stdout
-q [ --query ] arg json query
--oplog Use oplog for point-in-time snapshotting
--repair try to recover a crashed database
--forceTableScan force a table scan (do not use $snapshot)
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
具体使用示例如下:
[root@localhost mongodb]# ./bin/mongodump -d test -o data/backup
connected to: 127.0.0.1
DATABASE: test to data/backup/test
test.system.indexes to data/backup/test/system.indexes.bson
9 objects
test.users to data/backup/test/users.bson
3 objects
test.games to data/backup/test/games.bson
1 objects
test.blog.post to data/backup/test/blog.post.bson
1 objects
test.lists to data/backup/test/lists.bson
1 objects
test.math to data/backup/test/math.bson
1 objects
test.map to data/backup/test/map.bson
8 objects
test.my_collection to data/backup/test/my_collection.bson
0 objects
test.foo to data/backup/test/foo.bson
6 objects
test.system.users to data/backup/test/system.users.bson
1 objects
mongorestore是Mongodb从备份中恢复数据的工具,它主要用来获取mongodump的输出结果,并将备份的数据插入到运行的Mongodb中。
mongorestore命令使用方法如下:
[root@localhost mongodb]# ./bin/mongorestore --help
usage: ./bin/mongorestore [options] [directory or filename to restore from]
options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets)
--port arg server port. Can also use --host hostname:port
--ipv6 enable IPv6 support (disabled by default)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server -
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
--journal enable journaling
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
--objcheck validate object before inserting
--filter arg filter to apply before inserting
--drop drop each collection before import
--oplogReplay replay oplog for point-in-time restore
--keepIndexVersion don't upgrade indexes to newest version
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要备份的文件名
-q:指明备份数据的过滤条件
具体使用示例如下:
[root@localhost mongodb]# ./bin/mongorestore -d test --drop data/backup/test/
connected to: 127.0.0.1
Tue Aug 14 01:18:17 data/backup/test/games.bson
Tue Aug 14 01:18:17 going into namespace [test.games]
Tue Aug 14 01:18:17 dropping
1 objects found
Tue Aug 14 01:18:17 data/backup/test/foo.bson
Tue Aug 14 01:18:17 going into namespace [test.foo]
Tue Aug 14 01:18:17 dropping
6 objects found
Tue Aug 14 01:18:17 data/backup/test/blog.post.bson
Tue Aug 14 01:18:17 going into namespace [test.blog.post]
Tue Aug 14 01:18:17 dropping
1 objects found
Tue Aug 14 01:18:17 data/backup/test/lists.bson
Tue Aug 14 01:18:17 going into namespace [test.lists]
Tue Aug 14 01:18:17 dropping
1 objects found
Tue Aug 14 01:18:17 data/backup/test/map.bson
Tue Aug 14 01:18:17 going into namespace [test.map]
Tue Aug 14 01:18:17 dropping
8 objects found
Tue Aug 14 01:18:17 data/backup/test/math.bson
Tue Aug 14 01:18:17 going into namespace [test.math]
Tue Aug 14 01:18:17 dropping
1 objects found
Tue Aug 14 01:18:17 data/backup/test/system.users.bson
Tue Aug 14 01:18:17 going into namespace [test.system.users]
1 objects found
Tue Aug 14 01:18:17 data/backup/test/my_collection.bson
Tue Aug 14 01:18:17 going into namespace [test.my_collection]
Tue Aug 14 01:18:17 dropping
Tue Aug 14 01:18:17 file data/backup/test/my_collection.bson empty, skipping
Tue Aug 14 01:18:17 data/backup/test/users.bson
Tue Aug 14 01:18:17 going into namespace [test.users]
Tue Aug 14 01:18:17 dropping
3 objects found
Tue Aug 14 01:18:17 data/backup/test/system.indexes.bson
Tue Aug 14 01:18:17 going into namespace [test.system.indexes]
Tue Aug 14 01:18:17 dropping
Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.users", name: "_id_" }
Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.games", name: "_id_" }
Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.blog.post", name: "_id_" }
Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.lists", name: "_id_" }
Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.math", name: "_id_" }
Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.map", name: "_id_" }
Tue Aug 14 01:18:17 { key: { gps: "2d" }, ns: "test.map", name: "gps_", min: -180.0, max: 181.0 }
Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.foo", name: "_id_" }
Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.system.users", name: "_id_" }
9 objects found
备注:还原指定数据库,数据文件需要指定到备份文件下面的子目录,如果不指定子目录,会报以下错误。
[root@localhost Desktop]# mongorestore -u rex -p 123456 -d test /var/lib/mongo/mongobak/
connected to: 127.0.0.1
ERROR: root directory must be a dump of a single database
when specifying a db name with --db
use the --help option for more information
正确做法:[root@localhost Desktop]# mongorestore -u rex -p 123456 -d test /var/lib/mongo/mongobak/test/
另:使用--drop可在恢复前删除集合(若存在),否则数据就会与现有集合数据合并,可能会覆盖一些文档.
[root@localhost Desktop]# mongorestore -u rex -p 123456 -d test --drop /var/lib/mongo/mongobak/test/
参考文章:
http://chenzhou123520.iteye.com/blog/1630993
通过mongodump和mongorestore实现Mongodb备份和恢复的更多相关文章
- MongoDB备份和恢复
mongodump备份数据 该命令可以导出所有数据到指定目录中, 也能通过参数指定备份服务器 mongodump -h dbhost -d dbname -o dbdirectory dbhost: ...
- 使用mongodump及mongorestore备份及恢复数据
mongodump及mongorestore是用于备份和恢复mongodb数据库的两个命令,位于mongodb安装目录的bin文件夹下. mongodump导出的备份文件为二进制格式,每一个文档的对应 ...
- MongoDB初试备份及恢复
MongoDB作为文档数据库,有 1.登录MongoDB官网,地址:https://www.mongodb.com/download-center#community , 根据自己操作系统下载相应版 ...
- MongoDB备份(mongodump)和恢复(mongorestore)
MongoDB提供了备份和恢复的功能,分别是MongoDB下载目录下的mongodump.exe和mongorestore.exe文件 1.备份数据使用下面的命令: >mongodump -h ...
- MongoDB 备份(mongodump)与恢复(mongorestore)
MongoDB 备份(mongodump)与恢复(mongorestore) 备份:使用mongodump命令导出所有数据库到指定目录 参数说明: --host:MongoDB所在服务器IP. -- ...
- MongoDB 备份与还原 mongodump、mongorestore
目录 MongoDB 备份与还原 一. MongoDB 备份 1.mongodump 2 .cp 或者rsync 3.单节点意外关闭后,如何恢复数据 4.查看备份数据 二.MongoDB 还原 1.m ...
- MongoDB备份(mongodump)与恢复(mongorestore)工具实践
mongodump和mongorestore实践 1.mongodump备份工具 mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询,然后将所有查到的文档 ...
- Mongodb备份(mongodump)和恢复(mongorestore)
1.备份: mongodump -d DbName -o /data/backup 2. 恢复: mongorestore -d newDB --drop data/backup/DbName/
- 学习mongo系列(十)MongoDB 备份(mongodump)与恢复(mongorerstore) 监控(mongostat mongotop)
一.备份 在Mongodb中我们使用mongodump命令来备份MongoDB数据.该命令可以导出所有数据到指定目录中. mongodump命令可以通过参数指定导出的数据量级转存的服务器. mongo ...
随机推荐
- Java中String、StringBuffer和StringBuilder之间的区别
String在Java中是字符串常量 例如 String str = "abc"; str = str + 1; System.out.println(str); 结果将是abc1 ...
- 从hive将数据导出到mysql(转)
从hive将数据导出到mysql http://abloz.com 2012.7.20 author:周海汉 在上一篇文章<用sqoop进行mysql和hdfs系统间的数据互导>中,提到s ...
- typescript多维对象数组仿List泛型
定义对象: namespace entity{ export class MyClass{ public value:number; public rect:string; public constr ...
- 关于人脸识别引擎FaceRecognitionDotNet的实例
根据我上篇文章的分享,我提到了FaceRecognitionDotNet,它是python语言开发的一个项目face_recognition移植.结果真是有喜有忧,喜的是很多去关注了,进行了下载,我看 ...
- DEV 之 有些控件不允许拖动。
DEV 之 有些控件不允许拖动. 设置一个参数即可解决问题
- struts2把表单数据封装到实体类里
<form method="post" action=""> <input type="text" name=" ...
- 项目前端打包工具从 NEJ 切换成 webpack
此文已由作者张磊授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 这里不讨论 NEJ 和 webpack 的优劣,仅从技术角度来探寻一下能否实现,以及实现的代价. 前言 上一篇 ...
- vim 在 cmdline 内粘贴的方法
vim 在 cmdline 内粘贴的方法: 先按下 Ctrl-r ,然后选择从哪个剪贴板上粘贴 0 (yank register), 1 to 9 (shifting delete registers ...
- 对比Vector、ArrayList、LinkedList区别
Vector是Java早期提供的线程安全的动态数组.因为同步是又额外开销的,所以如果不需要线程安全,不建议选择.Vector内部用对象数组保存数据,可以根据需要自动的增加容量,当数组已满时,会创建新的 ...
- alicemq 方便的RabbitMQ 可视化工具
尽管RabbitMQ 自带一个管理插件,但是还不是那么强大,alicemq 是一个方便强大的可视化工具 rabbitmq 环境准备 docker-compose 文件 version: "3 ...