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
相关推荐
mongodump和mongorestore的人为方式,您的MongoDB Atlas集群 安装 npm install --save mongodb-atlas-backup 设置与使用 import MongoBackup from 'mongodb-atlas-backup' // Create an instance of the database ...
mongodb备份恢复 mongodump mongorestore
1.1.1 导出工具mongoexport 1.1.2 导入工具mongoimport 1.2 mongodump/mongorestore实践 1.2.2 m
mongobar是用于创建和管理MongoDB备份的python shell脚本。 在内部,它是MongoDB mongodump和mongorestore命令的包装。 安装 pip install mongobar 用法 在终端中运行mongobar 救命 通过-h --help命令访问帮助。 ...
周二有同学问,MONGODB怎么备份,怎么数据迁移,正好最近要做一个项目...一般小型系统使用mongodump 和 mongorestore 来进行系统的备份和恢复mongodump可以用来转储整个数据库、集合或查询结果。mongodump可以通过转储
mongodb数据备份和还原主要分为二种,一种是针对于库的mongodump和mongorestore,一种是针对库中表的mongoexport和mongoimport。
windows下面mongodb数据库备份和恢复 我可以讲数据备份到c:\data\dump目录下面,首先创建这个路径。然后进入到mongodb的bin目录下面 我的是: C:\Program Files\mongodb\bin 备份脚本是: //备份 mongodump -h ...
数据库备份 – mongodump 备份本地所有MongoDB数据库: 代码如下: # mongodump -h 127.0.0.1 –port 27017 -o /root/db/alldb 备份远程指定数据库: 代码如下: # mongodump -h 192.168.1.233 –port 27018 -d ...
1168.4 备份和修复 1168.4.1 数据文件备份 1178.4.2 mongodump和mongorestore 1178.4.3 fsync和锁 1188.4.4 从属备份 1198.4.5 修复 119第9章 复制 1219.1 主从复制 1219.1.1 选项 1229.1.2 添加...
mongodump / mongorestore-将MongoDB备份以.BSON格式转储到磁盘,或将其还原到实时数据库 mongostat-监视实时MongoDB服务器,副本集或分片群集 mongofiles-读取,写入,删除或更新文件 mongotop-监视mongo服务器上的...
1158.3.3 其他安全考虑 1168.4 备份和修复 1168.4.1 数据文件备份 1178.4.2 mongodump 和mongorestore 1178.4.3 fsync 和锁 1188.4.4 从属备份 1198.4.5 修复 119第9 章 复制 1219.1 主从复制 ...
主要介绍了MongoDB使用自带的命令行工具进行备份和恢复的教程,我们只需要在命令行界面中用简单的命令操作mongorestore和mongodump工具就可以实现,需要的朋友可以参考下
8.8 数据备份和修复 8.8.1 数据文件备份 8.8.2 数据备份mongodump 8.8.3 数据恢复mongorestore 8.8.4 fsync和锁 8.8.5 从属备份 8.8.6 修复 8.9 本章小结 第4篇 性能篇 第9章 索引 9.1 ...
mongorestore.exe -d "test" D:\Cloud\NoSql\mongodb2\data\backup\test //恢复数据库test mongorestore.exe -d test -c t_sysusers D:\Cloud\NoSql\mongodb2\data\backup\test\t_sysusers.bson //恢复数据库test中...
当前版本 0.1.2 处于 Beta 版 - 更安全的版本使用 0.0.9 一个简化备份和复制 mongo dbs 的小宝石。 您可以作为脚本运行(例如用于 cron 作业,或在交互模式下): 它会保存您的数据库网址,因此只需点击几下即可完成...
8.8 数据备份和修复 8.8.1 数据文件备份 8.8.2 数据备份mongodump 8.8.3 数据恢复mongorestore 8.8.4 fsync和锁 8.8.5 从属备份 8.8.6 修复 8.9 本章小结 第4篇 性能篇 第9章 索引 9.1 ...
MongoDB对于关系型数据库里的表,但是集合中没有列、行和关系概念,这体现了模式自由的特点。 MySQL MongoDB 说明 mysqld mongod 服务器守护进程 mysql mongo 客户端工具 mysqldump mongodump ...
服务器未来开放兴趣#backup mongodb数据库命令mongodump -d nse_futures_bank_nifty -o / home / raj / server-future-open-interest / backup / nse_futures_bank_nifty #restore mongodb数据库命令mongorestore / ...