mongodb数据库备份

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介:
1.导出数据

mongoexport -d my_mongodb -c choice -o choice.dat

參数说明:

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


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


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


-h server地址


2.导入数据
mongoimport -d my_mongodb -c choice choice .dat


參数说明:


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


-c 指明要导入的表, 本例中为”choice ”


3.整库备份:
mongodump -h dbhost -d dbname -o dbdirectory


-h:MongDB所在server地址,比如:127.0.0.1,也能够指定port号:127.0.0.1:27017
-d:须要备份的数据库实例,比如:test
-o:备份的数据存放位置,比如:c:\data\dump。当然该文件夹须要提前建立,在备份完毕后,系统自己主动在dump文件夹下建立一个test文件夹,这个文件夹里面存放该数据库实例的备份数据。


查看具体帮助信息例如以下
D:\tool\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
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
                                        authentication mechanism
  --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 directly
                                        (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)



4.整库恢复:
mongorestore -h dbhost -d dbname –directoryperdb dbdirectory


-h:MongoDB所在server地址
-d:须要恢复的数据库实例,比如:test,这个名称也能够和备份时候的不一样,比方test2
–directoryperdb:备份数据所在位置,比如:c:\data\dump\test。这里为什么要多加一个test。而不是备份时候的dump
–drop:恢复的时候,先删除当前数据。然后恢复备份的数据。就是说,恢复后。备份后加入改动的数据都会被删除


查看具体帮助信息例如以下
D:\tool\mongodb\bin>mongorestore --help
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)
  --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
  --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 directly
                                        (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
  --w arg (=0)                          minimum number of replicas per write






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5369176.html,如需转载请自行联系原作者

相关文章
|
1天前
|
NoSQL MongoDB 数据库
MongoDB数据恢复—MongoDB数据库文件被破坏的数据恢复案例
服务器数据恢复环境: 一台Windows Server操作系统服务器,服务器上部署MongoDB数据库。 MongoDB数据库故障&检测: 工作人员在未关闭MongoDB数据库服务的情况下,将数据库文件拷贝到其他分区。拷贝完成后将原MongoDB数据库所在分区进行了格式化操作,然后将数据库文件拷回原分区,重新启动MongoDB服务,服务无法启动。
|
4天前
|
NoSQL MongoDB Redis
Python与NoSQL数据库(MongoDB、Redis等)面试问答
【4月更文挑战第16天】本文探讨了Python与NoSQL数据库(如MongoDB、Redis)在面试中的常见问题,包括连接与操作数据库、错误处理、高级特性和缓存策略。重点介绍了使用`pymongo`和`redis`库进行CRUD操作、异常捕获以及数据一致性管理。通过理解这些问题、易错点及避免策略,并结合代码示例,开发者能在面试中展现其技术实力和实践经验。
37 8
Python与NoSQL数据库(MongoDB、Redis等)面试问答
|
1月前
|
NoSQL 网络协议 MongoDB
Windows公网远程连接MongoDB数据库【无公网IP】
Windows公网远程连接MongoDB数据库【无公网IP】
|
1月前
|
存储 NoSQL 关系型数据库
一篇文章带你搞懂非关系型数据库MongoDB
一篇文章带你搞懂非关系型数据库MongoDB
57 0
|
1月前
|
人工智能 NoSQL MongoDB
|
2月前
|
SQL NoSQL Java
文档型数据库MongoDB
文档型数据库MongoDB
|
2月前
|
JSON NoSQL MongoDB
MongoDB详解(五)——MongoDB数据库简单使用
MongoDB详解(五)——MongoDB数据库简单使用
106 1
|
2月前
|
存储 NoSQL Linux
MongoDB详解(四)——MongoDB数据库安装
MongoDB详解(四)——MongoDB数据库安装
67 2
|
2月前
|
NoSQL 关系型数据库 MySQL
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
不同系统下进行MySQL安装、MongoDB安装、Redis安装【2月更文挑战第5天】
444 5
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
|
3月前
|
存储 JSON NoSQL
【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)
【1月更文挑战第26天】【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)