mongodb的备份还原案例(一)

2014-11-24 14:33:53 · 作者: · 浏览: 4
mongodb的备份还原案例
Mongodb自带了mongodump和mongorestore这两个工具来实现对数据的备份和恢复。
mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询,然后将所有查到的文档写入磁盘。但是存在的问题时使用mongodump产生的备份不一定是 数据库的实时快照,如果我们在备份时对数据库进行了写入操作,则备份出来的文件可能不完全和Mongodb实时数据相等。另外在备份时可能会对其它客户端性能产生不利的影响。
mongodump用法;
[root@Mongodb ~]# 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 ( /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)
[root@Mongodb ~]# 

参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
以下是测试用例;
[root@Mongodb db]# mongodump  -d test
connected to: 127.0.0.1
Wed Jul  3 00:41:28.975 DATABASE: test   to     dump/test
Wed Jul  3 00:41:29.024         test.system.indexes to dump/test/system.indexes.bson
Wed Jul  3 00:41:29.031                  2 objects
Wed Jul  3 00:41:29.032         test.user to dump/test/user.bson
Wed Jul  3 00:41:29.106                  5 objects
Wed Jul  3 00:41:29.107         Metadata for test.user to dump/test/user.metadata.json
Wed Jul  3 00:41:29.114         test.code to dump/test/code.bson
Wed Jul  3 00:41:29.116                  28 objects
Wed Jul  3 00:41:29.116         Metadata for test.code to dump/test/code.metadata.json
[root@Mongodb db]# 
[root@Mongodb db]# ll
total 164080
-rw------- 1 root root 16777216 Jun 27 23:28 admin.0
-rw------- 1 root root 33554432 Jun 27 23:28 admin.1
-rw------- 1 root root 16777216 Jun 27 23:28 admin.ns
drwxr-xr-x 3 root root     4096 Jul  3 00:41 dump
-rw------- 1 root root 16777216 Jul  3 00:41 local.0
-rw------- 1 root root 16777216 Jul  3 00:41 local.ns
-rwxr-xr-x 1 root root        6 Jul  3 00:41 mongod.lock
-rw------- 1 root root 16777216 Jun 27 22:54 test.0
-rw------- 1 root root 33554432 Jul  1 19:37 test.1
-rw------- 1 root root 16777216 Jun 27 22:54 test.ns
[root@Mongodb db]# cd  dump/
You have new mail in /var/spool/mail/root
[root@Mongodb dump]# ls
test
[root@Mongodb dump]# ll
total 8
drwxr-xr-x 2 root root 4096 Jul  3 00:41 test
[root@Mongodb dump]# cd test/
[root@Mongodb test]# ls
code.bson           system.indexes.bson  user.metadata.json
code.metadata.json  user.bson
[root@Mongodb test]# ll
total 40
-rw-r--r-- 1 root root 1036 Jul  3 00:41 code.bson
-rw-r--r-- 1 root root   91 Jul  3 00:41 code.