通过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 ...
随机推荐
- PowerDesigner Comment与Name相互替换
从name替换comment Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl ' the curren ...
- windows下gitbook与开源中国码云关联,以及如何gitbook转pdf
gitbook能够很方便的和github关联,实现团队协作的效果.可是github私有库需要付费.但是开源中国码云能够建私有库,于是考虑将gitbook关联码云,折腾了一番后,能够可视化的关联,后面就 ...
- oracle goldengate的两种用法
此文已由作者赵欣授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 自从oracle收购来了goldengate这款产品并以后对它做了一系列改进后,有非常多的用户使用它做数据迁移 ...
- sonar严重性与颜色对应关系
- Logstash 收集 IIS 日志
日志样例 查看 IIS 日志配置,选择格式为 W3C(默认字段设置)保存生效. 2016-02-25 01:27:04 112.74.74.124 GET /goods/list/0/1.html - ...
- [ActionScript 3.0] SharedObject的用法简介
package com.models { import flash.net.SharedObject; /** * @author * @E-mail * @create 2015-6-12 下午2: ...
- luogu P1080国王游戏
贪心加高精 传送门:QWQ 先考虑两个人 a0 b0 p1 a1 b1 p2 a2 b2 那么满足:\(\huge ans1=\max(\frac{a0}{b1} , \frac{a0a1}{b2}) ...
- maven项目报错
[root@kube-master iff]# kubectl logs iff-dm-3029278244-9qrp6 -n iffjava.lang.IllegalArgumentExceptio ...
- 《UltraFast设计法实践》系列目录
最近准备开始潜心学习快速和高效的时序收敛设计了,突然想就把整个学习过程做成一个博客系列吧,虽然想想就很激动(技术狗就这么点出息--),但希望坚持下来. 这篇做个目录或者索引,不断向其中添加学习内容. ...
- P2472 [SCOI2007]蜥蜴
传送门 求无法逃离的蜥蜴总数的最小值就是求最多逃离的蜥蜴总数 所以显然考虑最大流,一个流量的路径就相当于一只蜥蜴逃离的路径 发现每个位置有一个最大经过次数,所以把每个位置拆成两个点$x,y$,$x$ ...