mongodump & mongorestore

来源:互联网 发布:js tostring 方法 编辑:程序博客网 时间:2024/06/06 18:27
Mongodb自带了mongodump和mongorestore这两个工具来实现对数据的备份和恢复。
mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询,然后将所有查到的文档写入磁盘。但是存在的问题时使用mongodump产生的备份不一定是数据库的实时快照,如果我们在备份时对数据库进行了写入操作,则备份出来的文件可能不完全和Mongodb实时数据相等。另外在备份时可能会对其它客户端性能产生不利的影响

一、mongodump
1、用法

[root@huang ~]# mongodump --helpExport 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

常用参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件

2、示例
[root@slave159 bin]# mongodump --port 27017 -d log -o /mnt/data_27017connected to: 127.0.0.12015-12-04T11:35:56.746+0800 DATABASE: log     to     /mnt/data_27017/log2015-12-04T11:35:56.747+0800     log.system.indexes to /mnt/data_27017/log/system.indexes.bson2015-12-04T11:35:56.747+0800          2 documents2015-12-04T11:35:56.747+0800     log.appLog to /mnt/data_27017/log/appLog.bson2015-12-04T11:35:56.966+0800          5900 documents2015-12-04T11:35:56.966+0800     Metadata for log.appLog to /mnt/data_27017/log/appLog.metadata.json2015-12-04T11:35:56.966+0800     log.settingLog to /mnt/data_27017/log/settingLog.bson2015-12-04T11:35:56.967+0800          133 documents2015-12-04T11:35:56.967+0800     Metadata for log.settingLog to /mnt/data_27017/log/settingLog.metadata.json

二、mongorestore
1、用法
[root@slave159 data_27017]# mongorestore --helpImport 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

常用参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要备份的文件名
-q:指明备份数据的过滤条件
2、示例
[root@slave159 data_27017]# mongorestore --host 192.168.61.64 -d log --drop /mnt/data_27017/logconnected to: 192.168.61.642015-12-04T11:42:06.732+0800 /mnt/data_27017/log/settingLog.bson2015-12-04T11:42:06.732+0800     going into namespace [log.settingLog]2015-12-04T11:42:06.732+0800      dropping133 objects found2015-12-04T11:42:06.734+0800     Creating index: { key: { _id: 1 }, name: "_id_", ns: "log.settingLog" }2015-12-04T11:42:06.760+0800 /mnt/data_27017/log/appLog.bson2015-12-04T11:42:06.760+0800     going into namespace [log.appLog]2015-12-04T11:42:06.760+0800      dropping5900 objects found2015-12-04T11:42:06.871+0800     Creating index: { key: { _id: 1 }, name: "_id_", ns: "log.appLog" }




0 0
原创粉丝点击