当前位置: 首页 > 工具软件 > BSON > 使用案例 >

mongodb 二进制 BSON 恢复 mongorestore

纪晨
2023-12-01

概要

该mongorestore程序将数据从由创建的二进制数据库转储mongodump或标准输入(从版本 3.0.0 开始)加载到mongod或 mongos实例中。

您还可以从目录中恢复特定的一个或多个集合 。例如,以下操作从/data/backup/mongodbbackup/test/ 目录中对应的数据文件中恢复单个集合:


# 创建索引 
rs1:PRIMARY> db.test.createIndex({name:1})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1655303837, 2),
		"signature" : {
			"hash" : BinData(0,"7XVuTQlwSsTB+aP9iIDsv8aM3CY="),
			"keyId" : NumberLong("7107407083665883139")
		}
	},
	"operationTime" : Timestamp(1655303837, 2)
}

# 插入数据
rs1:PRIMARY> db.test.insert({"name":"test"});
WriteResult({ "nInserted" : 1 })
rs1:PRIMARY> 
rs1:PRIMARY> 
rs1:PRIMARY> 
rs1:PRIMARY> db.test.find();
{ "_id" : ObjectId("62a9ef291a1fbb7fc8f23c59"), "name" : "test" }

# 备份导出
root@ubuntu-18:/data/backup# ls -lth /data/backup/mongodbbackup/test/
total 8.0K
-rw-r--r-- 1 root root  37 Jun 15 22:13 test.bson
-rw-r--r-- 1 root root 157 Jun 15 22:13 test.metadata.json


# 删除集合
rs1:PRIMARY> db.test.drop(); 
true
rs1:PRIMARY> 
rs1:PRIMARY> db.test.find();
rs1:PRIMARY> 
rs1:PRIMARY> 
rs1:PRIMARY> db.test.getIndexes();
[ ]

# 恢复单个集合
root@ubuntu-18:/data/backup/mongodbbackup/test# mongorestore  --host=localhost --port=27017 -u admin  -p '******' --db=test -c test  --authenticationDatabase=admin /data/backup/mongodbbackup/test/test.bson 
2022-06-15T22:47:09.430+0800	checking for collection data in /data/backup/mongodbbackup/test/test.bson
2022-06-15T22:47:09.430+0800	reading metadata for test.test from /data/backup/mongodbbackup/test/test.metadata.json
2022-06-15T22:47:09.452+0800	restoring test.test from /data/backup/mongodbbackup/test/test.bson
2022-06-15T22:47:09.513+0800	restoring indexes for collection test.test from metadata
2022-06-15T22:47:09.564+0800	finished restoring test.test (1 document, 0 failures)
2022-06-15T22:47:09.564+0800	1 document(s) restored successfully. 0 document(s) failed to restore.


# 检查恢复
rs1:PRIMARY> show tables; 
test
rs1:PRIMARY> 
rs1:PRIMARY> 
rs1:PRIMARY> db.test.find();
{ "_id" : ObjectId("62a9ef8e1a1fbb7fc8f23c5a"), "name" : "test" }
rs1:PRIMARY> 
rs1:PRIMARY> 
rs1:PRIMARY> db.test.getIndexes(); 
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "test.test"
	},
	{
		"v" : 2,
		"key" : {
			"name" : 1
		},
		"name" : "name_1",
		"ns" : "test.test"
	}
]

恢复多个集合,指定目录/data/backup/mongodbbackup/test, 不用指定集合,需要指定数据库,如下所示:

# 查看当前集合
rs1:PRIMARY> show tables;
test
test2
rs1:PRIMARY> db.test.find();
{ "_id" : ObjectId("62a9f2861a1fbb7fc8f23c5c"), "name" : "test2", "age" : 18 }
{ "_id" : ObjectId("62a9ef8e1a1fbb7fc8f23c5a"), "name" : "test" }
rs1:PRIMARY> 
rs1:PRIMARY> db.test2.find();
{ "_id" : ObjectId("62a9f44c1a1fbb7fc8f23c5d"), "name" : "test2" }
 
 # 备份导出
 root@ubuntu-18:/data/backup/mongodbbackup/test# mongodump  --host=localhost --port=27017 -u admin  -p '******' --db=test --authenticationDatabase=admin --out=/data/backup/mongodbbackup
2022-06-15T23:02:48.319+0800	writing test.test to 
2022-06-15T23:02:48.319+0800	writing test.test2 to 
2022-06-15T23:02:48.320+0800	done dumping test.test (2 documents)
2022-06-15T23:02:48.335+0800	done dumping test.test2 (1 document)
root@ubuntu-18:/data/backup/mongodbbackup/test# 
root@ubuntu-18:/data/backup/mongodbbackup/test# 
root@ubuntu-18:/data/backup/mongodbbackup/test# ls -tlh 
total 16K
-rw-r--r-- 1 root root  38 Jun 15 23:02 test2.bson
-rw-r--r-- 1 root root 158 Jun 15 23:02 test2.metadata.json
-rw-r--r-- 1 root root  88 Jun 15 23:02 test.bson
-rw-r--r-- 1 root root 254 Jun 15 23:02 test.metadata.json

# 删除集合
rs1:PRIMARY> db.test.drop();
true
rs1:PRIMARY> 
rs1:PRIMARY> db.test2.drop();
true

# 恢复2个集合
root@ubuntu-18:/data/backup/mongodbbackup/test# mongorestore  --host=localhost -u admin  -p '******' --db=test --authenticationDatabase=admin /data/backup/mongodbbackup/test/
2022-06-15T23:04:23.095+0800	WARNING: ignoring unsupported URI parameter 'replicaset'
2022-06-15T23:04:23.113+0800	the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2022-06-15T23:04:23.113+0800	building a list of collections to restore from /data/backup/mongodbbackup/test dir
2022-06-15T23:04:23.113+0800	reading metadata for test.test from /data/backup/mongodbbackup/test/test.metadata.json
2022-06-15T23:04:23.113+0800	reading metadata for test.test2 from /data/backup/mongodbbackup/test/test2.metadata.json
2022-06-15T23:04:23.131+0800	restoring test.test2 from /data/backup/mongodbbackup/test/test2.bson
2022-06-15T23:04:23.151+0800	restoring test.test from /data/backup/mongodbbackup/test/test.bson
2022-06-15T23:04:23.178+0800	no indexes to restore
2022-06-15T23:04:23.178+0800	finished restoring test.test2 (1 document, 0 failures)
2022-06-15T23:04:23.179+0800	restoring indexes for collection test.test from metadata
2022-06-15T23:04:23.215+0800	finished restoring test.test (2 documents, 0 failures)
2022-06-15T23:04:23.215+0800	3 document(s) restored successfully. 0 document(s) failed to restore.

–drop
在从转储备份还原集合之前,从目标数据库中删除集合。–drop不会删除不在备份中的集合。

当还原包括admin数据库时,mongorestore将 --drop删除所有用户凭据并将其替换为转储文件中定义的用户。因此,在 authorization启用的系统中,mongorestore必须能够对现有用户和转储文件中定义的用户进行身份验证。如果mongorestore无法对转储文件中定义的用户进行身份验证,则恢复过程将失败,留下一个空数据库。

如果在还原过程中删除并重新创建集合,则新创建的集合具有不同的 UUID,除非–drop与–preserveUUID.

 # 插入新数据 
 rs1:PRIMARY> db.test.find();
{ "_id" : ObjectId("62a9ef8e1a1fbb7fc8f23c5a"), "name" : "test" }
{ "_id" : ObjectId("62a9f2861a1fbb7fc8f23c5c"), "name" : "test2", "age" : 18 }
rs1:PRIMARY> 
rs1:PRIMARY> 
rs1:PRIMARY> db.test.insertOne({"name":"this is new"});
{
	"acknowledged" : true,
	"insertedId" : ObjectId("62a9fb6e1a1fbb7fc8f23c5f")
}
rs1:PRIMARY> 
rs1:PRIMARY> db.test.find();
{ "_id" : ObjectId("62a9ef8e1a1fbb7fc8f23c5a"), "name" : "test" }
{ "_id" : ObjectId("62a9f2861a1fbb7fc8f23c5c"), "name" : "test2", "age" : 18 }
{ "_id" : ObjectId("62a9fb6e1a1fbb7fc8f23c5f"), "name" : "this is new" }
rs1:PRIMARY> 

# 恢复之前的备份,备份集中只有2行记录,使用--drop恢复前先删除原集合
root@ubuntu-18:/data/backup/mongodbbackup/test# mongorestore --drop --host=localhost -u admin  -p '******' --db=test -c test --authenticationDatabase=admin /data/backup/mongodbbackup/test/test.bson 
2022-06-15T23:33:42.510+0800	checking for collection data in /data/backup/mongodbbackup/test/test.bson
2022-06-15T23:33:42.515+0800	reading metadata for test.test from /data/backup/mongodbbackup/test/test.metadata.json
2022-06-15T23:33:42.531+0800	restoring test.test from /data/backup/mongodbbackup/test/test.bson
2022-06-15T23:33:42.592+0800	restoring indexes for collection test.test from metadata
2022-06-15T23:33:42.615+0800	finished restoring test.test (2 documents, 0 failures)
2022-06-15T23:33:42.615+0800	2 document(s) restored successfully. 0 document(s) failed to restore.

# 检查只有2行记录了
rs1:PRIMARY> db.test.find();
{ "_id" : ObjectId("62a9ef8e1a1fbb7fc8f23c5a"), "name" : "test" }
{ "_id" : ObjectId("62a9f2861a1fbb7fc8f23c5c"), "name" : "test2", "age" : 18 }
rs1:PRIMARY> 

–gzip
3.2 版中的新功能。

从由创建的压缩文件或数据流恢复 mongodump --gzip

要从包含压缩文件的转储目录中恢复, mongorestore请使用该–gzip选项运行。

要从压缩存档文件中恢复,mongorestore请同时使用 –archive 选项–gzip 和–archive选项运行。

# 先压缩备份一下
root@ubuntu-18:/data/backup/mongodbbackup/test# mongodump --gzip  --host=localhost -u admin  -p '******' --db=test -c test --authenticationDatabase=admin --out=/data/backup/mongodbbackup
2022-06-15T23:38:43.428+0800	writing test.test to 
2022-06-15T23:38:43.429+0800	done dumping test.test (2 documents)
root@ubuntu-18:/data/backup/mongodbbackup/test# 
root@ubuntu-18:/data/backup/mongodbbackup/test# 
root@ubuntu-18:/data/backup/mongodbbackup/test# ls -tlh 
total 8.0K
-rw-r--r-- 1 root root  86 Jun 15 23:38 test.bson.gz
-rw-r--r-- 1 root root 171 Jun 15 23:38 test.metadata.json.gz 


# 删除集合
rs1:PRIMARY> db.test.find();
{ "_id" : ObjectId("62a9ef8e1a1fbb7fc8f23c5a"), "name" : "test" }
{ "_id" : ObjectId("62a9f2861a1fbb7fc8f23c5c"), "name" : "test2", "age" : 18 }
rs1:PRIMARY> 
rs1:PRIMARY> 
rs1:PRIMARY> db.test.drop()
true

# 从压缩备份中恢复,带上--gzip参数
root@ubuntu-18:/data/backup/mongodbbackup/test# mongorestore --gzip --host=localhost -u admin  -p '******' --db=test -c test --authenticationDatabase=admin /data/backup/mongodbbackup/test/test.bson.gz 
2022-06-15T23:40:33.663+0800	checking for collection data in /data/backup/mongodbbackup/test/test.bson.gz
2022-06-15T23:40:33.664+0800	reading metadata for test.test from /data/backup/mongodbbackup/test/test.metadata.json.gz
2022-06-15T23:40:33.680+0800	restoring test.test from /data/backup/mongodbbackup/test/test.bson.gz
2022-06-15T23:40:33.741+0800	restoring indexes for collection test.test from metadata
2022-06-15T23:40:33.772+0800	finished restoring test.test (2 documents, 0 failures)
2022-06-15T23:40:33.772+0800	2 document(s) restored successfully. 0 document(s) failed to restore.


 类似资料: