MongoDB的备份与恢复
0. 目录
- 整库备份
- 整库恢复
- 单collection备份
- 单collection恢复
1. 备份
MongoDB提供了备份工具,mongodump.exe,在bin目录下,其用法如下:
mongodump.exe -h localhost -d database_name -o d:\mongodump
-h:MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017
-d:需要备份的数据库实例,例如:database_name
-o:备份的数据存放位置,例如:d:\mongodump,当然该目录需要提前建立,在备份完成后,
系统自动在dump目录下建立一个database_name目录,这个目录里面存放该数据库实例的备份数据。
mongodump说明:
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)
  --quiet                               silence all non error diagnostic
                                        messages
  --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
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
                                        authentication mechanism
  --gssapiServiceName arg (=mongodb)    Service name to use when authenticating
                                        using GSSAPI/Kerberos
  --gssapiHostName arg                  Remote host name to use for purpose of
                                        GSSAPI/Kerberos authentication
  --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                      each db is in a separate directory
                                        (relevant only if dbpath specified)
  --journal                             enable journaling (relevant only if
                                        dbpath specified)
  -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)
  --dumpDbUsersAndRoles                 Dump user and role definitions for the
                                        given database
2. 恢复
mongorestore.exe -h localhost -d database_name –directoryperdb d:\mongodump\database_name
-h:MongoDB所在服务器地址
-d:需要恢复的数据库实例,例如:database_name,当然这个名称也可以和备份时候的不一样,比如test2
–directoryperdb:备份数据所在位置,例如:d:\mongodump\database_name
–drop:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用.
mongorestore说明:
Import BSON files into MongoDB.
usage: 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)
  --quiet                               silence all non error diagnostic
                                        messages
  --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
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
                                        authentication mechanism
  --gssapiServiceName arg (=mongodb)    Service name to use when authenticating
                                        using GSSAPI/Kerberos
  --gssapiHostName arg                  Remote host name to use for purpose of
                                        GSSAPI/Kerberos authentication
  --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                      each db is in a separate directory
                                        (relevant only if dbpath specified)
  --journal                             enable journaling (relevant only if
                                        dbpath specified)
  -d [ --db ] arg                       database to use
  -c [ --collection ] arg               collection to use (some commands)
  --objcheck                            validate object before inserting
                                        (default)
  --noobjcheck                          don't 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
  --oplogLimit arg                      include oplog entries before the
                                        provided Timestamp (seconds[:ordinal])
                                        during the oplog replay; the ordinal
                                        value is optional
  --keepIndexVersion                    don't upgrade indexes to newest version
  --noOptionsRestore                    don't restore collection options
  --noIndexRestore                      don't restore indexes
  --restoreDbUsersAndRoles              Restore user and role definitions for
                                        the given database
  --w arg (=0)                          minimum number of replicas per write
3. 单collection备份
mongoexport -h dbhost -d dbname -c collectionname -f collectionKey -o dbdirectory
-h: MongoDB所在服务器地址
-d: 需要恢复的数据库实例
-c: 需要恢复的集合
-f: 需要导出的字段(省略为所有字段)
-o: 表示导出的文件名
mongoexport说明
Export MongoDB data to CSV, TSV or JSON files.
Options:
  --help                                produce help message
  -v [ --verbose ]                      be more verbose (include multiple times
                                        for more verbosity e.g. -vvvvv)
  --quiet                               silence all non error diagnostic
                                        messages
  --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
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
                                        authentication mechanism
  --gssapiServiceName arg (=mongodb)    Service name to use when authenticating
                                        using GSSAPI/Kerberos
  --gssapiHostName arg                  Remote host name to use for purpose of
                                        GSSAPI/Kerberos authentication
  --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                      each db is in a separate directory
                                        (relevant only if dbpath specified)
  --journal                             enable journaling (relevant only if
                                        dbpath specified)
  -d [ --db ] arg                       database to use
  -c [ --collection ] arg               collection to use (some commands)
  -f [ --fields ] arg                   comma separated list of field names
                                        e.g. -f name,age
  --fieldFile arg                       file with field names - 1 per line
  -q [ --query ] arg                    query filter, as a JSON string, e.g.,
                                        '{x:{$gt:1}}'
  --csv                                 export to csv instead of json
  -o [ --out ] arg                      output file; if not specified, stdout
                                        is used
  --jsonArray                           output to a json array rather than one
                                        object per line
  -k [ --slaveOk ] arg (=1)             use secondaries for export if
                                        available, default true
  --forceTableScan                      force a table scan (do not use
                                        $snapshot)
  --skip arg (=0)                       documents to skip, default 0
  --limit arg (=0)                      limit the numbers of documents
                                        returned, default all
  --sort arg                            sort order, as a JSON string, e.g.,
                                        '{x:1}'
4. 单collection恢复
mongoimport -d dbhost -c collectionname –type csv –headerline –file
-type: 指明要导入的文件格式
-headerline: 批明不导入第一行,因为第一行是列名
-file: 指明要导入的文件路径
mongoimport说明
Import CSV, TSV or JSON data into MongoDB.
When importing JSON documents, each document must be a separate line of the input file.
Example:
  mongoimport --host myhost --db my_cms --collection docs < mydocfile.json
Options:
  --help                                produce help message
  -v [ --verbose ]                      be more verbose (include multiple times
                                        for more verbosity e.g. -vvvvv)
  --quiet                               silence all non error diagnostic
                                        messages
  --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
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
                                        authentication mechanism
  --gssapiServiceName arg (=mongodb)    Service name to use when authenticating
                                        using GSSAPI/Kerberos
  --gssapiHostName arg                  Remote host name to use for purpose of
                                        GSSAPI/Kerberos authentication
  --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                      each db is in a separate directory
                                        (relevant only if dbpath specified)
  --journal                             enable journaling (relevant only if
                                        dbpath specified)
  -d [ --db ] arg                       database to use
  -c [ --collection ] arg               collection to use (some commands)
  -f [ --fields ] arg                   comma separated list of field names
                                        e.g. -f name,age
  --fieldFile arg                       file with field names - 1 per line
  --ignoreBlanks                        if given, empty fields in csv and tsv
                                        will be ignored
  --type arg                            type of file to import.  default: json
                                        (json,csv,tsv)
  --file arg                            file to import from; if not specified
                                        stdin is used
  --drop                                drop collection first
  --headerline                          first line in input file is a header
                                        (CSV and TSV only)
  --upsert                              insert or update objects that already
                                        exist
  --upsertFields arg                    comma-separated fields for the query
                                        part of the upsert. You should make
                                        sure this is indexed
  --stopOnError                         stop importing at first error rather
                                        than continuing
  --jsonArray                           load a json array, not one item per
                                        line. Currently limited to 16MB.
MongoDB的备份与恢复的更多相关文章
- MongoDB数据备份与恢复
		测试环境:windows 一. 导出数据F:\DbSoft\soft\master\bin>mongoexport /h 127.0.0.1 /port 50000 /d testdb /c t ... 
- MongoDB 数据库备份与恢复
		1.MongoDB数据库备份 1.语法: mongodump -h dbhost -d dbname -o dbdirectory 参数说明: ... 
- linux下的mongodb的备份与恢复
		mongodb的备份有两种方式: 1.直接拷贝数据目录下的一切文件 2.使用mongodump方式 3.主从复制:http://www.cnblogs.com/huangxincheng/archiv ... 
- mongodb 数据备份与恢复
		备份 语法 mongodump -h dbhost -d dbname -o dbdirectory -h:服务器地址,也可以指定端口号 -d:需要备份的数据库名称 -o:备份的数据存放位置,此目录中 ... 
- 分布式文档存储数据库之MongoDB备份与恢复
		前文我们聊了下mongodb的访问控制以及用户创建和角色分配,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13974656.html:今天我们来了解下mong ... 
- MongoDB资料汇总
		MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. 它的特点是高性能.易部署.易使用,存储数据非常方便.主要功能特性有: 面向集合存 ... 
- mongodb基础学习7-备份与恢复
		下面来讲讲mongodb的备份与恢复 备份可以备份为二进制格式,如果是用于数据交换,可以备份成json或cvs格式 导入/导出可以操作的是本地的mongodb服务器,也可以是远程的. 所以,都有如下通 ... 
- mongodb3的使用
		1.在windows下载安装mongodb 将下载好的zip压缩文件解压并重命名为mongo-3.0.6,并在根目录下新建文件夹data用于存放数据 2.启动mongod守护进程 使用命令mongod ... 
- Mongo小结
		开启mongo的姿势: cd /usr/local/mongo/bin(mongo的文件目录的bin目录下) sudo ./mongod (开启mongo服务) 再新开一个终端 cd /usr/loc ... 
随机推荐
- Android L 使用ART能提高多少性能?
			点击打开链接 刚刚结束的 Google I/O 大会上,Android 下一代操作系统「L」带来不少惊喜.新系统运行更快.更省电. 然而开发者对这个新系统也有颇多疑问,比如新的运行模式 ART 对开发 ... 
- ASCII 字符代码表
- DirectSound播放PCM(可播放实时采集的音频数据)
			前言 该篇整理的原始来源为http://blog.csdn.net/leixiaohua1020/article/details/40540147.非常感谢该博主的无私奉献,写了不少关于不同多媒体库的 ... 
- UVa 11997 (优先队列 多路归并) K Smallest Sums
			考虑一个简单的问题,两个长度为n的有序数组A和B,从每个数组中各选出一个数相加,共n2中情况,求最小的n个数. 将这n2个数拆成n个有序表: A1+B1≤A1+B2≤... A2+B1≤A2+B2≤. ... 
- LA 3516 (计数 DP) Exploring Pyramids
			设d(i, j)为连续子序列[i, j]构成数的个数,因为遍历从根节点出发最终要回溯到根节点,所以边界情况是:d(i, i) = 1; 如果s[i] != s[j], d(i, j) = 0 假设第一 ... 
- UVa 11572 Unique snowflakes【滑动窗口】
			题意:给出 n个数,找到尽量长的一个序列,使得该序列中没有重复的元素 看的紫书,滑动窗口来做的 当右端碰到有相同的数的时候,左端向前滑动一个数 模拟一个样例好理解些 #include<iostr ... 
- BZOJ1049: [HAOI2006]数字序列
			题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1049 题解: ydc的题解:http://pan.baidu.com/share/link?u ... 
- Websocket和PHP Socket编程
			本来是搜一些html5 websocket资料看的,结果被引去看了php的socket编程.下面是一些简单的例子,在命令行运行php脚本就行 [命令行运行PHP]PHP中有一个php.exe文件,可以 ... 
- 用RSA加密实现Web登录密码加密传输
			通常我们做一个Web应用程序的时候都需要登录,登录就要输入用户名和登录密码,并且,用户名和登录密码都是明文传输的,这样就有可能在中途被别人拦截,尤其是在网吧等场合. 这里顺带一个小插曲,我以前有家公司 ... 
- 纯css3写的仿真图书翻页效果
			对css3研究越深入,越觉得惊艳.css3说不上是万能的,但是它能实现的效果也超出了我的想象.它的高效率和动画效果的流畅性很多情况下能替代js的作用.个人习惯css3能实现的效果就不会用js,虽然在国 ... 
