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 ( <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)
[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.metadata.json
-rw-r--r-- 1 root root  128 Jul  3 00:41 system.indexes.bson
-rw-r--r-- 1 root root  205 Jul  3 00:41 user.bson
-rw-r--r-- 1 root root   91 Jul  3 00:41 user.metadata.json
[root@Mongodb test]# 

备份 出来的有带有数据的bson文件,还有index的bson文件

mongorestore是Mongodb从备份中恢复数据的工具,它主要用来获取mongodump的输出结果,并将备份的数据插入到运行的Mongodb中。

mongorestore用法;

[root@Mongodb ~]# 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
You have new mail in /var/spool/mail/root
[root@Mongodb ~]#

以下进行恢复过程记录;

[root@Mongodb-B mongo]# scp -P 22 -r root@192.168.155.198:/data/db/dump  /usr/local/      ------拷贝备份的目录文件到另一台新mongodb服务器上进行恢复
root@192.168.155.198's password: 
code.bson                                     100% 1036     1.0KB/s   00:00    
system.indexes.bson                           100%  128     0.1KB/s   00:00    
user.bson                                     100%  205     0.2KB/s   00:00    
code.metadata.json                            100%   91     0.1KB/s   00:00    
user.metadata.json                            100%   91     0.1KB/s   00:00    
[root@Mongodb-B mongo]# cd /usr/local/
[root@Mongodb-B local]# ls
bin   etc    include  libexec  mongodb-linux-i686-2.4.4.tgz  share
dump  games  lib      mongo    sbin                          src
[root@Mongodb-B local]# cd dump/
[root@Mongodb-B dump]# ls
test
[root@Mongodb-B dump]# ll     ------------看到拷贝过来的备份目录内容
total 8
drwxr-xr-x 2 root root 4096 Jul  3 01:07 test
[root@Mongodb-B dump]# cd test/
[root@Mongodb-B test]# ls
code.bson           system.indexes.bson  user.metadata.json
code.metadata.json  user.bson
[root@Mongodb-B test]# ll            -----------看到拷贝过来的备份目录内容
total 40
-rw-r--r-- 1 root root 1036 Jul  3 01:07 code.bson
-rw-r--r-- 1 root root   91 Jul  3 01:07 code.metadata.json
-rw-r--r-- 1 root root  128 Jul  3 01:07 system.indexes.bson
-rw-r--r-- 1 root root  205 Jul  3 01:07 user.bson
-rw-r--r-- 1 root root   91 Jul  3 01:07 user.metadata.json
[root@Mongodb-B test]#

[root@Mongodb-B test]# /Apps/mongo/bin/mongorestore -d test --drop /usr/local/dump/test/    -------执行mongorestore命令,目前只是恢复单独一个test库
connected to: 127.0.0.1
Wed Jul  3 01:20:30.574 /usr/local/dump/test/code.bson
Wed Jul  3 01:20:30.574         going into namespace [test.code]
Wed Jul  3 01:20:30.574          dropping
28 objects found
Wed Jul  3 01:20:30.576         Creating index: { key: { _id: 1 }, ns: "test.code", name: "_id_" }
Wed Jul  3 01:20:30.796 /usr/local/dump/test/user.bson
Wed Jul  3 01:20:30.796         going into namespace [test.user]
Wed Jul  3 01:20:30.796          dropping
5 objects found
Wed Jul  3 01:20:30.798         Creating index: { key: { _id: 1 }, ns: "test.user", name: "_id_" }
[root@Mongodb-B test]# /Apps/mongo/bin/mongo
MongoDB shell version: 2.4.4
connecting to: test
Server has startup warnings: 
Wed Jul  3 01:05:59.682 [initandlisten] 
Wed Jul  3 01:05:59.682 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Wed Jul  3 01:05:59.682 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Wed Jul  3 01:05:59.682 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Wed Jul  3 01:05:59.682 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Wed Jul  3 01:05:59.682 [initandlisten] 
> show dbs;
admin   (empty)
local   0.03125GB
test    0.0625GB
> use test;
switched to db test
> show tables;
code
system.indexes
user
> db.user.find();     ----校验信息的完整性;
{ "_id" : ObjectId("51cc51275e0cd84ee2e58d72"), "name" : "songxixi" }
{ "_id" : ObjectId("51cc512a5e0cd84ee2e58d73"), "name" : "songxixi" }
{ "_id" : ObjectId("51cc512a5e0cd84ee2e58d74"), "name" : "songxixi" }
{ "_id" : ObjectId("51cc512b5e0cd84ee2e58d75"), "name" : "songxixi" }
{ "_id" : ObjectId("51cc512b5e0cd84ee2e58d76"), "name" : "songxixi" }
> db.code.find()   ----校验信息的完整性;
{ "_id" : ObjectId("51cc521b93fcb9607a1d4d2a"), "name" : "song" }
{ "_id" : ObjectId("51cc521c93fcb9607a1d4d2b"), "name" : "song" }
{ "_id" : ObjectId("51cc521c93fcb9607a1d4d2c"), "name" : "song" }
{ "_id" : ObjectId("51cc521d93fcb9607a1d4d2d"), "name" : "song" }
{ "_id" : ObjectId("51cc521d93fcb9607a1d4d2e"), "name" : "song" }
{ "_id" : ObjectId("51cc521e93fcb9607a1d4d2f"), "name" : "song" }
{ "_id" : ObjectId("51cc521e93fcb9607a1d4d30"), "name" : "song" }
{ "_id" : ObjectId("51cc521f93fcb9607a1d4d31"), "name" : "song" }
{ "_id" : ObjectId("51cc522093fcb9607a1d4d32"), "name" : "song" }
{ "_id" : ObjectId("51cc522093fcb9607a1d4d33"), "name" : "song" }
{ "_id" : ObjectId("51cc522193fcb9607a1d4d34"), "name" : "song" }
{ "_id" : ObjectId("51cc522293fcb9607a1d4d35"), "name" : "song" }
{ "_id" : ObjectId("51cc522393fcb9607a1d4d36"), "name" : "song" }
{ "_id" : ObjectId("51cc522393fcb9607a1d4d37"), "name" : "song" }
{ "_id" : ObjectId("51cc522493fcb9607a1d4d38"), "name" : "song" }
{ "_id" : ObjectId("51cc522593fcb9607a1d4d39"), "name" : "song" }
{ "_id" : ObjectId("51cc522593fcb9607a1d4d3a"), "name" : "song" }
{ "_id" : ObjectId("51cc522693fcb9607a1d4d3b"), "name" : "song" }
{ "_id" : ObjectId("51cc522793fcb9607a1d4d3c"), "name" : "song" }
{ "_id" : ObjectId("51cc522893fcb9607a1d4d3d"), "name" : "song" }
Type "it" for more




> db.system.indexes.find()         ------校验索引的完整性,有时候迁移数据索引容易丢失,索引是进行校验的重点项目;重建索引在大数据下回很漫长
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.code", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.user", "name" : "_id_" }

经以上校验,数据和索引信息完整无误,迁移成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值