Mongodb备份恢复(mongodump,mongorestore方式)

一:备份


1.1 备份所有数据库

[root@ser6-52 mongodbbackup.bak]# mongodump -u superuser -p 123456  --authenticationDatabase admin -o /backup/mongodb/full_bak_201507021651.bak
connected to: 127.0.0.1
2015-07-02T16:51:08.291+0800 all dbs
2015-07-02T16:51:08.291+0800 DATABASE: admin	 to 	/backup/mongodb/full_bak_201507021651.bak/admin
2015-07-02T16:51:08.292+0800 	admin.system.indexes to /backup/mongodb/full_bak_201507021651.bak/admin/system.indexes.bson
2015-07-02T16:51:08.292+0800 	  3 documents
2015-07-02T16:51:08.292+0800 	admin.system.version to /backup/mongodb/full_bak_201507021651.bak/admin/system.version.bson
2015-07-02T16:51:08.293+0800 	  1 documents
2015-07-02T16:51:08.293+0800 	Metadata for admin.system.version to /backup/mongodb/full_bak_201507021651.bak/admin/system.version.metadata.json
2015-07-02T16:51:08.293+0800 	admin.system.users to /backup/mongodb/full_bak_201507021651.bak/admin/system.users.bson
2015-07-02T16:51:08.294+0800 	  2 documents
2015-07-02T16:51:08.294+0800 	Metadata for admin.system.users to /backup/mongodb/full_bak_201507021651.bak/admin/system.users.metadata.json
2015-07-02T16:51:08.294+0800 DATABASE: myTest	 to 	/backup/mongodb/full_bak_201507021651.bak/myTest
2015-07-02T16:51:08.294+0800 	myTest.system.indexes to /backup/mongodb/full_bak_201507021651.bak/myTest/system.indexes.bson
2015-07-02T16:51:08.295+0800 	  2 documents
2015-07-02T16:51:08.295+0800 	myTest.c to /backup/mongodb/full_bak_201507021651.bak/myTest/c.bson
2015-07-02T16:51:08.298+0800 	  2000 documents
2015-07-02T16:51:08.299+0800 	Metadata for myTest.c to /backup/mongodb/full_bak_201507021651.bak/myTest/c.metadata.json
2015-07-02T16:51:08.299+0800 	myTest.d to /backup/mongodb/full_bak_201507021651.bak/myTest/d.bson
2015-07-02T16:51:08.299+0800 	  11 documents
2015-07-02T16:51:08.299+0800 	Metadata for myTest.d to /backup/mongodb/full_bak_201507021651.bak/myTest/d.metadata.json
 

 

1.2 备份单个数据库


[root@ser6-52 myTest]# mongodump -d myTest -u superuser -p 123456  --authenticationDatabase admin -o /backup/mongodb/myTest_bak_201507021653.bak
connected to: 127.0.0.1
2015-07-02T16:53:35.183+0800 DATABASE: myTest	 to 	/backup/mongodb/myTest_bak_201507021653.bak/myTest
2015-07-02T16:53:35.184+0800 	myTest.system.indexes to /backup/mongodb/myTest_bak_201507021653.bak/myTest/system.indexes.bson
2015-07-02T16:53:35.185+0800 	  2 documents
2015-07-02T16:53:35.185+0800 	myTest.c to /backup/mongodb/myTest_bak_201507021653.bak/myTest/c.bson
2015-07-02T16:53:35.189+0800 	  2000 documents
2015-07-02T16:53:35.189+0800 	Metadata for myTest.c to /backup/mongodb/myTest_bak_201507021653.bak/myTest/c.metadata.json
2015-07-02T16:53:35.189+0800 	myTest.d to /backup/mongodb/myTest_bak_201507021653.bak/myTest/d.bson
2015-07-02T16:53:35.194+0800 	  11 documents
2015-07-02T16:53:35.194+0800 	Metadata for myTest.d to /backup/mongodb/myTest_bak_201507021653.bak/myTest/d.metadata.json
 


1.3 备份某个表


[root@ser6-52 myTest]# mongodump -u  superuser -p 123456  --authenticationDatabase admin -d myTest -c d -o /backup/mongodb/myTest_d_bak_201507021701.bak
connected to: 127.0.0.1
2015-07-02T17:01:47.554+0800 DATABASE: myTest	 to 	/backup/mongodb/myTest_d_bak_201507021701.bak/myTest
2015-07-02T17:01:47.555+0800 	myTest.d to /backup/mongodb/myTest_d_bak_201507021701.bak/myTest/d.bson
2015-07-02T17:01:47.558+0800 	  11 documents
2015-07-02T17:01:47.558+0800 	Metadata for myTest.d to /backup/mongodb/myTest_d_bak_201507021701.bak/myTest/d.metadata.json
 


二:恢复


2.1 恢复单个表

> db.d.find();
{ "_id" : ObjectId("5594f048a9a970e631c4c20c"), "d" : 0 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20d"), "d" : 1 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20e"), "d" : 2 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20f"), "d" : 3 }
{ "_id" : ObjectId("5594f048a9a970e631c4c210"), "d" : 4 }
{ "_id" : ObjectId("5594f048a9a970e631c4c212"), "d" : 6 }
{ "_id" : ObjectId("5594f048a9a970e631c4c213"), "d" : 7 }
{ "_id" : ObjectId("5594f048a9a970e631c4c214"), "d" : 8 }
{ "_id" : ObjectId("5594f048a9a970e631c4c215"), "d" : 9 }
{ "_id" : ObjectId("5594f0bea9a970e631c4c216"), "d" : 99 }
{ "_id" : ObjectId("5594f119a9a970e631c4c217"), "d" : 199 }
> db.d.find().count();
11
 
> db.d.drop();
true
 
[root@ser6-52 myTest]# mongorestore -u  superuser -p 123456  --authenticationDatabase admin -d myTest -c d /backup/mongodb/myTest_d_bak_201507021701.bak/myTest/d.bson 
connected to: 127.0.0.1
2015-07-02T17:23:20.593+0800 /backup/mongodb/myTest_d_bak_201507021701.bak/myTest/d.bson
2015-07-02T17:23:20.593+0800 	going into namespace [myTest.d]
11 objects found
2015-07-02T17:23:20.594+0800 	Creating index: { key: { _id: 1 }, name: "_id_", ns: "myTest.d" }
 
 
> db.d.find().count();
11
 
 


 

2.2 恢复单个数据库

[root@ser6-52 myTest]# mongorestore -u  superuser -p 123456  --authenticationDatabase admin   /backup/mongodb/myTest_bak_201507021653.bak
connected to: 127.0.0.1
2015-07-02T17:41:00.378+0800 /backup/mongodb/myTest_bak_201507021653.bak/myTest/d.bson
2015-07-02T17:41:00.378+0800 	going into namespace [myTest.d]
Restoring to myTest.d without dropping. Restored data will be inserted without raising errors; check your server log
11 objects found
2015-07-02T17:41:00.378+0800 	Creating index: { key: { _id: 1 }, name: "_id_", ns: "myTest.d" }
2015-07-02T17:41:00.383+0800 /backup/mongodb/myTest_bak_201507021653.bak/myTest/c.bson
2015-07-02T17:41:00.383+0800 	going into namespace [myTest.c]
Restoring to myTest.c without dropping. Restored data will be inserted without raising errors; check your server log
2000 objects found
2015-07-02T17:41:00.390+0800 	Creating index: { key: { _id: 1 }, name: "_id_", ns: "myTest.c" }

--这里恢复数据库的时候没有指定-d 数据库名称,否则报错(我看网上有的指定了)。我的mongodb版本是2.6.9,也许是个bug吧。

 

2.3 恢复所有数据库

[root@ser6-52 ~]# mongorestore -u  superuser -p 123456  --authenticationDatabase admin   /backup/mongodb/full_bak_201507021651.bak/
connected to: 127.0.0.1
2015-07-03T09:30:20.275+0800 	going into namespace [admin.system.version]
Restoring to admin.system.version without dropping. Restored data will be inserted without raising errors; check your server log
1 objects found
2015-07-03T09:30:20.276+0800 	Creating index: { key: { _id: 1 }, name: "_id_", ns: "admin.system.version" }
2015-07-03T09:30:20.277+0800 /backup/mongodb/full_bak_201507021651.bak/admin/system.users.bson
2015-07-03T09:30:20.277+0800 	going into namespace [admin.system.users]
Restoring to admin.system.users without dropping. Restored data will be inserted without raising errors; check your server log
2 objects found
2015-07-03T09:30:20.282+0800 	Creating index: { key: { _id: 1 }, name: "_id_", ns: "admin.system.users" }
2015-07-03T09:30:20.282+0800 	Creating index: { unique: true, key: { user: 1, db: 1 }, name: "user_1_db_1", ns: "admin.system.users" }
2015-07-03T09:30:20.285+0800 /backup/mongodb/full_bak_201507021651.bak/myTest/d.bson
2015-07-03T09:30:20.285+0800 	going into namespace [myTest.d]
11 objects found
2015-07-03T09:30:20.286+0800 	Creating index: { key: { _id: 1 }, name: "_id_", ns: "myTest.d" }
2015-07-03T09:30:20.288+0800 /backup/mongodb/full_bak_201507021651.bak/myTest/c.bson
2015-07-03T09:30:20.288+0800 	going into namespace [myTest.c]
Restoring to myTest.c without dropping. Restored data will be inserted without raising errors; check your server log
2000 objects found
2015-07-03T09:30:20.295+0800 	Creating index: { key: { _id: 1 }, name: "_id_", ns: "myTest.c" }

 

三:mysqlmongodb简单对比


3.1 对比描述

Mongodb:

mongorestore恢复的时候,默认不是恢复到备份时刻的状态。

如:
①:假如我备份后,新增了几条数据,用备份恢复后,那几条新增的数据还在。
②:如果备份后某条数据被update了,用备份恢复后,数据是update后的结果,不是备份时间点的数据(如原本name='dan',备份后,将dan更新成了nihao,恢复后,数据还是nihao)
估计是恢复时,遇到_id一样的值,就没有做任何处理。

Mysql:

mysqldump是先删表,再建表,插入数据的,默认就能恢复到备份时刻的状态。

 

3.2 示例


3.2.1 mongodb

--备份记录请参考1.3

 

D表备份的数据有:

> db.d.find();
{ "_id" : ObjectId("5594f048a9a970e631c4c20c"), "d" : 0 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20d"), "d" : 1 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20e"), "d" : 2 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20f"), "d" : 3 }
{ "_id" : ObjectId("5594f048a9a970e631c4c210"), "d" : 4 }
{ "_id" : ObjectId("5594f048a9a970e631c4c212"), "d" : 6 }
{ "_id" : ObjectId("5594f048a9a970e631c4c213"), "d" : 7 }
{ "_id" : ObjectId("5594f048a9a970e631c4c214"), "d" : 8 }
{ "_id" : ObjectId("5594f048a9a970e631c4c215"), "d" : 9 }
{ "_id" : ObjectId("5594f0bea9a970e631c4c216"), "d" : 99 }
{ "_id" : ObjectId("5594f119a9a970e631c4c217"), "d" : 199 }
 
#在备份完成后插入一条数据:
d表插入999999后的数据是:
> db.d.find();
{ "_id" : ObjectId("5594f048a9a970e631c4c20c"), "d" : 0 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20d"), "d" : 1 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20e"), "d" : 2 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20f"), "d" : 3 }
{ "_id" : ObjectId("5594f048a9a970e631c4c210"), "d" : 4 }
{ "_id" : ObjectId("5594f048a9a970e631c4c212"), "d" : 6 }
{ "_id" : ObjectId("5594f048a9a970e631c4c213"), "d" : 7 }
{ "_id" : ObjectId("5594f048a9a970e631c4c214"), "d" : 8 }
{ "_id" : ObjectId("5594f048a9a970e631c4c215"), "d" : 9 }
{ "_id" : ObjectId("5594f0bea9a970e631c4c216"), "d" : 99 }
{ "_id" : ObjectId("5594f119a9a970e631c4c217"), "d" : 199 }
{ "_id" : ObjectId("5595ffbcdeae2abeba569b24"), "d" : 999999 }
 
#现在不删表进行恢复:
 
[root@ser6-52 ~]# mongorestore -u  superuser -p 123456  --authenticationDatabase admin -d myTest -c d /backup/mongodb/myTest_d_bak_201507021701.bak/myTest/d.bson 
connected to: 127.0.0.1
2015-07-03T11:41:49.188+0800 /backup/mongodb/myTest_d_bak_201507021701.bak/myTest/d.bson
2015-07-03T11:41:49.188+0800 	going into namespace [myTest.d]
Restoring to myTest.d without dropping. Restored data will be inserted without raising errors; check your server log
11 objects found
2015-07-03T11:41:49.189+0800 	Creating index: { key: { _id: 1 }, name: "_id_", ns: "myTest.d" }
 
> db.d.find();
{ "_id" : ObjectId("5594f048a9a970e631c4c20c"), "d" : 0 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20d"), "d" : 1 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20e"), "d" : 2 }
{ "_id" : ObjectId("5594f048a9a970e631c4c20f"), "d" : 3 }
{ "_id" : ObjectId("5594f048a9a970e631c4c210"), "d" : 4 }
{ "_id" : ObjectId("5594f048a9a970e631c4c212"), "d" : 6 }
{ "_id" : ObjectId("5594f048a9a970e631c4c213"), "d" : 7 }
{ "_id" : ObjectId("5594f048a9a970e631c4c214"), "d" : 8 }
{ "_id" : ObjectId("5594f048a9a970e631c4c215"), "d" : 9 }
{ "_id" : ObjectId("5594f0bea9a970e631c4c216"), "d" : 99 }
{ "_id" : ObjectId("5594f119a9a970e631c4c217"), "d" : 199 }
{ "_id" : ObjectId("5595ffbcdeae2abeba569b24"), "d" : 999999 }
> db.d.find().count();
12

恢复后999999依然存在,说明备份后新增的数据在恢复时不会被删除。

如果备份后某条数据被update了,恢复的时候,数据是怎样的呢?

 

#准备测试数据

use myTest;
> db.e.save({"id":"1","name":"dan","age":25});
WriteResult({ "nInserted" : 1 })
> db.e.save({"id":"2","name":"jiao","age":25});
WriteResult({ "nInserted" : 1 })
> db.e.find();
{ "_id" : ObjectId("559627a7deae2abeba569b26"), "id" : "1", "name" : "dan", "age" : 25 }
{ "_id" : ObjectId("559627b4deae2abeba569b27"), "id" : "2", "name" : "jiao", "age" : 25 }

#备份

[root@ser6-52 ~]# mongodump -u superuser -p -d myTest -c e -o /backup/mongodb/myTest_e_bak.bak --authenticationDatabase admin
Enter password: 
connected to: 127.0.0.1
2015-07-03T14:13:54.877+0800 DATABASE: myTest	 to 	/backup/mongodb/myTest_e_bak.bak/myTest
2015-07-03T14:13:54.878+0800 	myTest.e to /backup/mongodb/myTest_e_bak.bak/myTest/e.bson
2015-07-03T14:13:54.879+0800 	  2 documents
2015-07-03T14:13:54.879+0800 	Metadata for myTest.e to /backup/mongodb/myTest_e_bak.bak/myTest/e.metadata.json

#更新其中一条数据:

--id2的记录,namejiao更新为jiaojiao

> db.e.update({"id":"2"},{$set:{"name":"jiaojiao"}},true,false);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.e.find();
{ "_id" : ObjectId("559627a7deae2abeba569b26"), "id" : "1", "name" : "dan", "age" : 25 }
{ "_id" : ObjectId("559627b4deae2abeba569b27"), "id" : "2", "name" : "jiaojiao", "age" : 25 }

#恢复

[root@ser6-52 ~]# mongorestore -u superuser -p --authenticationDatabase admin -d myTest -c e  /backup/mongodb/myTest_e_bak.bak/myTest/e.bson 
Enter password: 
connected to: 127.0.0.1
2015-07-03T14:19:57.511+0800 /backup/mongodb/myTest_e_bak.bak/myTest/e.bson
2015-07-03T14:19:57.511+0800 	going into namespace [myTest.e]
Restoring to myTest.e without dropping. Restored data will be inserted without raising errors; check your server log
2 objects found
2015-07-03T14:19:57.512+0800 	Creating index: { key: { _id: 1 }, name: "_id_", ns: "myTest.e" }
> db.e.find();
{ "_id" : ObjectId("559627a7deae2abeba569b26"), "id" : "1", "name" : "dan", "age" : 25 }
{ "_id" : ObjectId("559627b4deae2abeba569b27"), "id" : "2", "name" : "jiaojiao", "age" : 25 }

恢复后结果仍然是jiaojiao,不是jiao,说明备份后倘若某条记录被更新了,默认无法恢复到备份点的状态。

这和mysqldump不同。mysql会恢复到备份点的状态。

恢复的时候加上--drop参数,就能将数据恢复到备份时刻的状态了。

3.2.2 mysql

#准备测试数据

mysql> use dba;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
mysql> create table t1(id int);
Query OK, 0 rows affected (1.62 sec)
 
mysql> insert into t1(id) values(1);
Query OK, 1 row affected (1.88 sec)
 
mysql> insert into t1(id) values(2);
Query OK, 1 row affected (0.27 sec)
 
mysql> commit
    -> ;
Query OK, 0 rows affected (0.00 sec)

 

#备份

[root@ser6-51 mysql]# mysqldump -u root -p  dba  t1 > /backup/mysql/dba_t1_bak_201507031338.bak
Enter password: 
 
[root@ser6-51 ~]# cd /backup/mysql
[root@ser6-51 mysql]# ls -ltr
total 4
-rw-r--r-- 1 root root 1254 Jul  3 13:37 dba_t1_bak_201507031338.bak
 

该备份脚本内容:

[root@ser6-51 mysql]# cat dba_t1_bak_201507031338.bak 

-- MySQL dump 10.13  Distrib 5.6.20, for Linux (x86_64)
--
-- Host: localhost    Database: dba
-- ------------------------------------------------------
-- Server version	5.6.20-r5436-log
 
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
 
--
-- Table structure for table `t1`
--
 
DROP TABLE IF EXISTS `t1`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
 
--
-- Dumping data for table `t1`
--
 
LOCK TABLES `t1` WRITE;
/*!40000 ALTER TABLE `t1` DISABLE KEYS */;
INSERT INTO `t1` VALUES (1),(2);
/*!40000 ALTER TABLE `t1` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
 
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
 
-- Dump completed on 2015-07-03 13:49:24
 


 

#插入一条数据

mysql> insert into t1(id) values(3);

Query OK, 1 row affected (0.60 sec)

 

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

 

#恢复

[root@ser6-51 mysql]# mysql -u root -p  dba < /backup/mysql/dba_t1_bak_201507031338.bak 

Enter password: 

 

mysql> select * from t1;

+------+

| id   |

+------+

|    1 |

|    2 |

+------+

2 rows in set (0.00 sec)

 

新插入的id3的记录没有了。

 

此时,我删除其中一条数据:

mysql> delete from t1 where id = 1;

Query OK, 1 row affected (0.33 sec)

 

mysql> commit;

 

恢复:

[root@ser6-51 mysql]# mysql -u root -p  dba < /backup/mysql/dba_t1_bak_201507031338.bak 

Enter password: 

 

 

mysql> select * from t1;

+------+

| id   |

+------+

|    1 |

|    2 |

+------+

2 rows in set (0.04 sec)

 

因为Mysql就是先删表,再插入的,只能恢复到备份时刻那个点的状态。


#我用的是mongodb版本是2.6.9 

 #初学,自己研究的,可能有漏洞,仅供参考,欢迎指正。


你可能感兴趣的:(Mongodb,学习,Mongodb,备份恢复)