`

NoSQL mongDB整理:整库备份与还原以及单个collection备份、恢复方法

阅读更多

 

文章借鉴(稍有修改):http://www.jb51.net/article/40285.htm

 

mongodump.exe备份的原理是通过一次查询获取当前服务器快照,并将快照写入磁盘中,因此这种方式保存的也不是实时的,因为在获取快照后,服务器还会有数据写入,为了保证备份的安全,同样我们还是可以利用fsync锁使服务器数据暂时写入缓存中。 

高效开源数据库(mongodb)下载地址请见我的另一篇文章:http://ysj5125094.iteye.com/blog/2118168

 

整库备份:

经过测试的方法(导出15服务器上的dbtest库):D:\yusj\mongodb\bin>mongodump -h 192.168.1.15 -d dbtest -o D:/yusj/dump20150213

 

mongodump -h dbhost -d dbname -o dbdirectory
-h:MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017
-d:需要备份的数据库实例,例如:test
-o:备份的数据存放位置,例如:c:\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。

mongodump的官方说明(可通过mongodump --help查看):
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 ( /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)

 

整库恢复:

经过测试的方法(将导出的dbtest库,整库导出到139服务器上并修改库名为dbtest2015):D:\mongodb\bin>mongorestore -h 192.168.1.139 -d dbtest2015 C:/dump20150213/dbtest

 

mongorestore -h dbhost -d dbname –directoryperdb dbdirectory
-h:MongoDB所在服务器地址
-d:需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2
–directoryperdb:备份数据所在位置,例如:c:\data\dump\test,这里为什么要多加一个test,而不是备份时候的dump,读者自己查看提示吧!
–drop:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用哦!

mongorestore的官方说明(可通过mongorestore --help查看):
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 ( /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
 --oplogLimit arg    exclude oplog entries newer than provided timestamp
             (epoch[:ordinal])
 --keepIndexVersion   don't upgrade indexes to newest version
 --noOptionsRestore   don't restore collection options
 --noIndexRestore    don't restore indexes
 --w arg (=1)      minimum number of replicas per write

 

单个collection备份:
mongoexport -h dbhost -d dbname -c collectionname -f collectionKey -o dbdirectory
-h: MongoDB所在服务器地址
-d: 需要恢复的数据库实例
-c: 需要恢复的集合
-f: 需要导出的字段(省略为所有字段)
-o: 表示导出的文件名

mongoexport的官方说明(可通过mongoexport --help查看):
 --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 ( /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)
 -f [ --fields ] arg    comma separated list of field names e.g. -f
              name,age
 --fieldFile arg      file with fields names - 1 per line
 -q [ --query ] arg    query filter, as a JSON string
 --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)

 

单个collection恢复:
mongoimport -h dbhost -d dbname -c collectionname –type csv –headerline –file

-h: MongoDB所在服务器地址
-d: 需要恢复的数据库实例
-c: 需要恢复的集合

-type: 指明要导入的文件格式
-headerline: 批明不导入第一行,因为第一行是列名
-file: 指明要导入的文件路径

mongoimport的官方说明(可通过mongoimport --help查看):
 --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 ( /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)
 -f [ --fields ] arg   comma separated list of field names e.g. -f name,age
 --fieldFile arg     file with fields 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      CSV,TSV only - use first line as headers
 --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.

 

其他导入与导出操作:

1. mongoimport -d my_mongodb -c user user.dat

参数说明:

-d 指明使用的库, 本例中为” my_mongodb”

-c 指明要导出的表, 本例中为”user”

可以看到导入数据的时候会隐式创建表结构

2. mongoexport -d my_mongodb -c user -o user.dat

参数说明:

-d 指明使用的库, 本例中为” my_mongodb”

-c 指明要导出的表, 本例中为”user”

-o 指明要导出的文件名, 本例中为”user.dat”

从上面可以看到导出的方式使用的是JSON 的样式.

 

 

 

0
0
分享到:
评论

相关推荐

    知识领域: 数据库管理 技术关键词: SQL、关系数据库、NoSQL 内容关键词: 数据建模、查询优化、事务处理 用途: 理解数

    技术关键词: SQL、关系数据库、NoSQL 内容关键词: 数据建模、查询优化、事务处理 用途: 理解数据存储和管理的基本原理,确保数据的有效性和安全性 资源描述: "MySQL官方文档"提供了MySQL数据库的详尽文档和教程,...

    数据库备份与恢复

    下面将详细讲解数据库备份与恢复的基础知识、常见方法以及重要性。 一、数据库备份的目的 数据库备份的主要目标是防止数据丢失。这可能由于硬件故障、软件错误、人为误操作或恶意攻击导致。通过定期备份,可以在...

    nosql实验五-HBase数据迁移与数据备份&恢复.docx

    本实验主要介绍了 HBase 数据迁移与数据备份和恢复的方法,包括使用 Sqoop 将 MySQL 数据导入到 HBase、将文本文件批量导入 HBase、使用 Hadoop DistCp 实现 HBase 的冷备份和热备份。 一、使用 Sqoop 将 MySQL ...

    知识领域: 数据库管理 技术关键词: SQL、关系数据库、NoSQL 内容关键词: 数据建模、查询优化、事务处理

    在IT行业中,数据库管理是至关重要的领域,它涵盖了多种技术和工具,如SQL、关系数据库以及NoSQL等。本文将深入探讨这些关键知识点,帮助你掌握高效的数据管理与操作技巧。 首先,我们来看SQL(Structured Query ...

    Professional NoSQL 英文版 Shashank.Tiwari

    CHAPTER 1: NOSQL: WHAT IT IS AND WHY YOU NEED IT 3 CHAPTER 2: HELLO NOSQL: GETTING INITIAL HANDS-ON EXPERIENCE 21 CHAPTER 3: INTERFACING AND INTERACTING WITH NOSQL 43 PART II: LEARNING THE NOSQL ...

    nosql-wrapper:NoSQL包装器

    我创建此对象是为了减少重新创建与NoSQL数据库的连接实例的需要。 基本上,每个NoSQL的所有python依赖关系都将由该库处理。 版: Python 3 资料库 支持的: MongoDB 去做: 卡桑德拉 雷迪斯 里亚克 DynamoDB...

    唐建法-NoSQL之王:一分钟从关系型迁移到MongoDB

    TJ会通过若干互动的演示,而不是干巴巴的宣讲的方式来为大家介绍MongoDB风靡全球,成为NoSQL王者的关键原因。如果您已经是Mongo老司机,不用担心,你也可以从TJ的文档模式设计精粹及一分钟关系型到MongoDB的迁移演示...

    数据备份与恢复.docx

    数据备份与恢复是确保数据安全的重要步骤,特别是在使用 Redis 等 NoSQL 数据库时。Redis 提供了两种持久化选项,分别是 RDB 和 AOF。本文将详细介绍 RDB 和 AOF 的工作原理、优缺点、配置方法及恢复数据的步骤。 ...

    NoSql 技术文档

    2. 数据安全:数据备份、恢复和安全性管理需要额外关注。 3. 缺乏标准:不同NoSQL数据库之间的互操作性较差。 4. 学习曲线:不同的NoSQL数据库有各自的特性和API,学习成本较高。 总之,NoSQL数据库在应对大数据、...

    mysql面试题史上最强汇总:基础知识+性能优化+备份与恢复+ 数据库设计+数据库管理+高可用性与容灾+安全性+ 框架集成等

    3. 备份与恢复:包括数据备份和恢复的方法、备份数据的类型、备份与恢复的策略、日志文件管理等。 4. 数据库设计:包括数据库设计范式、数据库结构设计、关系模型等。 5. 数据库管理:包括用户管理、权限管理、事务...

    NoSQL数据库入门

    7. **NoSQL数据库的管理**:包括数据备份、恢复、监控和优化等,这些都是确保NoSQL数据库高效稳定运行的关键。 8. **开发和调试技巧**:学习如何使用NoSQL数据库的查询语言和工具,如MongoDB的Mongoose库,以及如何...

    基于Mysql和MongoDB的协同备份方案研究与实现.pdf

    "基于Mysql和MongoDB的协同备份方案研究与实现" 本文研究了基于Mysql和MongoDB的协同备份方案,旨在解决两个数据库之间的数据备份问题。通过实践网站备份案例,介绍了基于Mysql和MongoDB的协同备份方案和实现。内容...

    NoSQLBooster for MongoDB

    5. **备份与恢复**:NoSQLBooster支持一键式数据库备份和恢复功能,使得数据安全得到保障,尤其是在面临系统故障或数据丢失的情况下。 6. **脚本编辑器**:对于需要运行JavaScript脚本的场景,NoSQLBooster提供了一...

    数据库开发 维护 性能调优 备份还原 各种复杂问题的解决方案

    数据库开发、维护、性能调优、备份还原以及解决复杂问题,这些都是数据库管理中不可或缺的关键环节。在IT领域,数据库作为存储和管理数据的核心系统,其高效稳定运行对于业务连续性和数据安全性至关重要。以下是对...

    nosql驱动程序:nosql驱动程序

    NoSQL 关系数据库 NoSQL CAP定理,AXP 2015,JWN 2016 C CAP中的一致性实际上意味着线性化,这是一个非常具体(非常强烈)的一致性概念。 特别地,即使C也代表“一致性”,它与ACID中的C无关。 线性化的含义如下...

    NoSQL精粹 中文版+英文

    5. **分布式与水平扩展**:NoSQL数据库设计的目标之一是易于水平扩展,通过添加更多的硬件节点来提升处理能力,而不是像RDBMS那样通过垂直扩展单个服务器。 6. **ACID与BASE**:NoSQL通常放弃了传统的ACID(原子性...

    noSql数据库面试题整理.zip

    非关系型数据库(NoSQL)近年来在大数据处理和分布式系统中扮演了重要角色,与传统的关系型数据库相比,NoSQL数据库提供了更高的可扩展性、灵活性和性能。本资料主要聚焦于MongoDB、Redis和Memcached这三种常见的...

    Mini-NoSQL-Database:NoSQL 数据库作为数据库系统课程的课程项目

    ##README 这是我们 DBMS 课程项目的存储库,我们在其中构建了一个名为 noPandaDB 的 NoSQL 数据库。 我们将构建查询语言以及数据库结构。 我们将主要使用Python进行编码。 ###Description ####查询语言查询语言称为...

    NoSql-Basics:创建项目以了解有关NoSQL的更多信息

    NoSQL(Not Only SQL)是一种非关系型数据库技术,它与传统的SQL数据库有着显著的不同,主要设计用于处理大规模数据分布式存储。在这个项目中,我们将深入探索NoSQL的世界,特别是通过Redis和MongoDB这两种流行的...

Global site tag (gtag.js) - Google Analytics