手记

mongo-07-备份和恢复

生产环境中,我们一般会使用副本集来进行冗余备份、容灾恢复,但一般地,也都会为了数据安全留一手,使用定时任务对MongoDB的数据进行备份,万一所有Mongo节点宕机后,可以用以恢复。

我这里使用的是 mongo4.4.3 版本,默认安装不带mongoDB的备份工具 mongodump 和恢复工具 mongorerstore

MongoDB 工具下载:[MongoDB DataBase Tools] www.mongodb.com/try/download/database-tools

我下载的是 mongodb-database-tools-rhel70-x86_64-100.2.1.tgz,将下载后的工具复制到Mongo安装目录下的bin目录下。

[root@docker01 mongodb-4.4.3]# tar -zxvf mongodb-database-tools-rhel70-x86_64-100.2.1.tgz 
mongodb-database-tools-rhel70-x86_64-100.2.1/LICENSE.md
mongodb-database-tools-rhel70-x86_64-100.2.1/README.md
mongodb-database-tools-rhel70-x86_64-100.2.1/THIRD-PARTY-NOTICES
mongodb-database-tools-rhel70-x86_64-100.2.1/bin/bsondump
mongodb-database-tools-rhel70-x86_64-100.2.1/bin/mongodump
mongodb-database-tools-rhel70-x86_64-100.2.1/bin/mongoexport
mongodb-database-tools-rhel70-x86_64-100.2.1/bin/mongofiles
mongodb-database-tools-rhel70-x86_64-100.2.1/bin/mongoimport
mongodb-database-tools-rhel70-x86_64-100.2.1/bin/mongorestore
mongodb-database-tools-rhel70-x86_64-100.2.1/bin/mongostat
mongodb-database-tools-rhel70-x86_64-100.2.1/bin/mongotop
[root@docker01 mongodb-4.4.3]# \cp mongodb-database-tools-rhel70-x86_64-100.2.1/bin/* /usr/local/mongodb-4.4.3/bin/
[root@docker01 mongodb-4.4.3]# ll /usr/local/mongodb-4.4.3/bin/
总用量 392368
-rwxr-xr-x. 1 root root 21622192 1月  18 17:04 bsondump
-rwxr-xr-x. 1 root root     7684 12月 22 07:57 install_compass
-rwxr-xr-x. 1 root root 53430808 12月 22 07:49 mongo
-rwxr-xr-x. 1 root root 82273160 12月 22 07:49 mongod
-rwxr-xr-x. 1 root root 26009408 1月  18 17:04 mongodump
-rwxr-xr-x. 1 root root 25660408 1月  18 17:04 mongoexport
-rwxr-xr-x. 1 root root 25651128 1月  18 17:04 mongofiles
-rwxr-xr-x. 1 root root 25859384 1月  18 17:04 mongoimport
-rwxr-xr-x. 1 root root 26360632 1月  18 17:04 mongorestore
-rwxr-xr-x. 1 root root 64424544 12月 22 07:49 mongos
-rwxr-xr-x. 1 root root 25436472 1月  18 17:04 mongostat
-rwxr-xr-x. 1 root root 25028520 1月  18 17:04 mongotop
[root@docker01 mongodb-4.4.3]# 

1. mongodump 数据备份,也会备份索引

mongodump -h dbhost --port=<port> -d dbname --out=<directory-path> -u username -p password --authenticationDatabase=<database-name>

-h 或者 --host : Mongo服务器地址
--port=<port> : 指定端口号
-d : 备份的数据库名称
--out=<directory-path> : 指定存放备份数据文件的目录

如果MongoDB启动服务时开始了auth安全模式,还需要加上下面三个认证参数
-u : 指定username
-p : 指定password
--authenticationDatabase=<database-name> : 保存用户凭据的数据库,也就是在哪个数据库上创建的用户

现在 test 数据库上只有一个 user 集合,user集合的数据如下:

> use test
> show collections
user
> db.user.find()
{ "_id" : "1", "money" : 1500, "name" : "刘一一", "gender" : "女" }
{ "_id" : "2", "money" : 1000, "name" : "陈二", "gender" : "女" }
{ "_id" : "3", "name" : "张三", "sal" : 1000, "gender" : "女" }
{ "_id" : "4", "money" : 800, "name" : "李四", "gender" : "女" }
{ "_id" : "5", "money" : 1000, "name" : "王五", "sal" : 500, "gender" : "女" }
{ "_id" : "6", "name" : "赵六", "money" : 1000 }
{ "_id" : "7", "money" : 1000, "name" : "孙七", "gender" : "女" }
{ "_id" : ObjectId("5fff6a78025e5d9ea86e18cc"), "money" : 1000, "name" : "周八", "gender" : "女" }
{ "_id" : { "age" : 18, "gender" : "男" }, "money" : 1000, "name" : "吴九", "gender" : "女" }
{ "_id" : ObjectId("5fff95783f6d5ed2fcce4133"), "money" : 2000, "name" : "ZHANG", "addr" : [ "三亚", "北京" ], "gender" : "女" }
{ "_id" : ObjectId("60045f5719414c7b5be4c122"), "name" : "富二代", "money" : 1500 }
{ "_id" : ObjectId("6004611c19414c7b5be4c13f"), "name" : "打工人", "money" : 800 }
{ "_id" : ObjectId("6004c82e21bca04a14f52bee"), "money" : 1100 }
{ "_id" : ObjectId("6004ce5521bca04a14f52bf0"), "money" : 1200 }
{ "_id" : ObjectId("6004ce8f21bca04a14f52bf1"), "money" : 1300, "name" : null }
> 

User集合的索引:

> db.user.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_"
	},
	{
		"v" : 2,
		"unique" : true,
		"key" : {
			"name" : 1
		},
		"name" : "name_1",
		"sparse" : true
	}
]
> 

备份 test 数据库到 /var/lib/mongodb/backup 目录下

[root@docker01 backup]# /usr/local/mongodb-4.4.3/bin/mongodump -h localhost --port=27017 -d test --out=/var/lib/mongodb/backup -u testUser -p testUser --authenticationDatabase=admin
2021-01-18T17:52:26.513+0800	writing test.user to /var/lib/mongodb/backup/test/user.bson
2021-01-18T17:52:26.514+0800	done dumping test.user (15 documents)
[root@docker01 backup]# ll /var/lib/mongodb/backup/
总用量 0
drwxr-xr-x. 2 root root 49 1月  18 17:52 test
[root@docker01 backup]# ll /var/lib/mongodb/backup/test/
总用量 8
-rw-r--r--. 1 root root 947 1月  18 17:52 user.bson
-rw-r--r--. 1 root root 259 1月  18 17:52 user.metadata.json
[root@docker01 backup]# 

2. mongorerstore 数据恢复,也会恢复索引

mongorestore -h dbhost --port=<port> -d dbname --dir=<directory-name> -u username -p password --authenticationDatabase=<database-name>
-h 或者 --host : Mongo服务器地址
--port=<port> : 指定端口号
-d :需要恢复的数据库名称
--dir=<directory-name> :备份数据所在位置
--drop :恢复时先删除当前数据,然后恢复备份数据(恢复成功后,上一次备份后添加修改的数据都会被删除,慎用)

如果MongoDB启动服务时开始了auth安全模式,还需要加上下面三个认证参数
-u : 指定username
-p : 指定password
--authenticationDatabase=<database-name> : 保存用户凭据的数据库,也就是在哪个数据库上创建的用户

删除test数据库的User集合,drop操作同时也会删除集合的索引

> db.user.drop()
> db.user.getIndexes()
[ ]
> show collections
> 

使用 /var/lib/mongodb/backup 目录下 test 数据库的备份数据进行数据恢复,同时也会恢复索引

[root@docker01 backup]# /usr/local/mongodb-4.4.3/bin/mongorestore -h localhost --port=27017 -d test --dir=/var/lib/mongodb/backup/test -u testUser -p testUser --authenticationDatabase=admin
2021-01-18T17:58:54.729+0800	The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-01-18T17:58:54.729+0800	building a list of collections to restore from /var/lib/mongodb/backup/test dir
2021-01-18T17:58:54.730+0800	reading metadata for test.user from /var/lib/mongodb/backup/test/user.metadata.json
2021-01-18T17:58:54.748+0800	restoring test.user from /var/lib/mongodb/backup/test/user.bson
2021-01-18T17:58:54.758+0800	restoring indexes for collection test.user from metadata
2021-01-18T17:58:54.793+0800	finished restoring test.user (15 documents, 0 failures)
2021-01-18T17:58:54.793+0800	15 document(s) restored successfully. 0 document(s) failed to restore.
[root@docker01 backup]# 

查看数据恢复后的user集合中的文档数据和索引

> show collections
user
> db.user.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_"
	},
	{
		"v" : 2,
		"unique" : true,
		"key" : {
			"name" : 1
		},
		"name" : "name_1",
		"sparse" : true
	}
]
> db.user.find()
{ "_id" : "1", "money" : 1500, "name" : "刘一一", "gender" : "女" }
{ "_id" : "2", "money" : 1000, "name" : "陈二", "gender" : "女" }
{ "_id" : "3", "name" : "张三", "sal" : 1000, "gender" : "女" }
{ "_id" : "4", "money" : 800, "name" : "李四", "gender" : "女" }
{ "_id" : "5", "money" : 1000, "name" : "王五", "sal" : 500, "gender" : "女" }
{ "_id" : "6", "name" : "赵六", "money" : 1000 }
{ "_id" : "7", "money" : 1000, "name" : "孙七", "gender" : "女" }
{ "_id" : ObjectId("5fff6a78025e5d9ea86e18cc"), "money" : 1000, "name" : "周八", "gender" : "女" }
{ "_id" : { "age" : 18, "gender" : "男" }, "money" : 1000, "name" : "吴九", "gender" : "女" }
{ "_id" : ObjectId("5fff95783f6d5ed2fcce4133"), "money" : 2000, "name" : "ZHANG", "addr" : [ "三亚", "北京" ], "gender" : "女" }
{ "_id" : ObjectId("60045f5719414c7b5be4c122"), "name" : "富二代", "money" : 1500 }
{ "_id" : ObjectId("6004611c19414c7b5be4c13f"), "name" : "打工人", "money" : 800 }
{ "_id" : ObjectId("6004c82e21bca04a14f52bee"), "money" : 1100 }
{ "_id" : ObjectId("6004ce5521bca04a14f52bf0"), "money" : 1200 }
{ "_id" : ObjectId("6004ce8f21bca04a14f52bf1"), "money" : 1300, "name" : null }
> 
0人推荐
随时随地看视频
慕课网APP