Mongodb入门--头歌实验MongoDB 实验——数据备份和恢复

在实际的应用场景中,经常需要对业务数据进行备份以做容灾准备, MongoDB 提供了备份和恢复的功能,分别是 MongoDB 下的

一、数据备份

任务描述

本关任务:按照编程要求备份数据库。

相关知识

为了完成本关任务,你需要掌握: 1.掌握 mongodump 备份工具的参数含义; 2.如何使用 mongodump 备份数据。

mongodump 备份工具

mongodump 的参数与 mongoexport(数据导出)的参数基本一致:

参数 参数说明
-h 指明数据库宿主机的IP
-u 指明数据库的用户名
-p 指明数据库的密码
-d 指明数据库的名字
-c 指明collection的名字
-o 指明到要导出的文件名
-q 指明导出数据的过滤条件
--authenticationDatabase 验证数据的名称
--gzip 备份时压缩
--oplog use oplog for taking a point-in-time snapshot
使用 mongodump 备份数据

备份工具同导入导出工具类似,都是在命令行进行操作,无需进入客户端。

  • 全库备份(如果数据库未设置用户和密码,可以省略 -uroot -proot 参数)

mongodump -h 127.0.0.1:27300 -uroot -proot --authenticationDatabase admin  -o /home/mongod
#备份本地27300端口中root用户的所有数据库到/home/mongod目录下
  • 单个数据库备份
mongodump -h 127.0.0.1:27300 -uroot -proot --authenticationDatabase admin  -d test -o /home/mongod/test
#备份本地27300端口中root用户的test数据库到/home/mongod/test目录下
  • 集合备份

    mongodump -h 127.0.0.1:27300 -uroot -proot --authenticationDatabase admin -d test -c haha -o /home/mongod/test/haha
    #备份27300端口中root用户的test数据库的haha集合到/home/mongod/test/haha目录下
  • 压缩备份库

mongodump -h 127.0.0.1:27300 -uroot -proot --authenticationDatabase admin  -d test -o /home/mongod/test1 --gzip
#压缩备份本地27300端口中root用户的test数据库到/home/mongod/test1目录下
  • 压缩备份集合

    mongodump -h 127.0.0.1:27300 -uroot -proot --authenticationDatabase admin -d test -c haha -o /home/mongod/test1/haha --gzip
    #压缩备份27300端口中root用户的test数据库的haha集合到/home/mongod/test1/haha目录下
编程要求

根据提示,在右侧命令行进行操作(以下均在默认端口为27017的客户端进行,无用户和密码;以下**/opt下的路径均不存在,需要自己先行创建**):

  • 分别将 /home/example 目录下的 person.json 和 student.csv 导入到 MongoDB 的 test1 数据库的 person 集合、test2 数据库的 student 集合;

  • 将所有数据库备份到 /opt/mongodb 目录下;

  • 将 test1 数据库备份到 /opt/mongodb_1 目录下;

  • 将 person 集合备份到 /opt/collection_1 目录下;

  • 将 student 集合压缩备份到 /opt/collection_2 目录下;

  • 将 test2 数据库压缩备份到 /opt/mongodb_2 目录下。

1.创建以上目录

root@evassh-13661150:~# mkdir /opt/mongodb
root@evassh-13661150:~# mkdir /opt/mongodb_1
root@evassh-13661150:~# mkdir /opt/mongodb_2
root@evassh-13661150:~# mkdir /opt/collection_1
root@evassh-13661150:~# mkdir /opt/collection_2

2. 分别将 /home/example 目录下的 person.json 和 student.csv 导入到 MongoDB 的 test1 数据库的 person 集合、test2 数据库的 student 集合;

root@evassh-13661150:~# mongoimport -d test1 -c person --type json --file /home/example/person.json
2022-12-18T15:32:43.037+0000    connected to: localhost
2022-12-18T15:32:43.052+0000    imported 8 documents
root@evassh-13661150:~# mongoimport -d test2 -c student --type csv --headerline --ignoreBlanks --file /home/example/student.csv
2022-12-18T15:33:36.403+0000    connected to: localhost
2022-12-18T15:33:36.416+0000    imported 8 documents

mongoimport -d test1 -c person --type json --file /home/example/person.json

mongoimport -d test2 -c student --type csv --headerline --ignoreBlanks --file /home/example/student.csv

3.其他五步(按顺序)

root@evassh-13661150:~# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -o /opt/mongodb
2022-12-18T15:35:24.521+0000    writing admin.system.version to 
2022-12-18T15:35:24.522+0000    done dumping admin.system.version (1 document)
2022-12-18T15:35:24.522+0000    writing test1.person to 
2022-12-18T15:35:24.522+0000    writing test2.student to 
2022-12-18T15:35:24.523+0000    done dumping test1.person (8 documents)
2022-12-18T15:35:24.523+0000    done dumping test2.student (8 documents)
 
 
root@evassh-13661150:~# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d test1 -o /opt/mongodb_1
2022-12-18T15:36:20.989+0000    writing test1.person to 
2022-12-18T15:36:20.989+0000    done dumping test1.person (8 documents)
 
 
root@evassh-13661150:~# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin  -d test1 -c person -o /opt/collection_1
2022-12-18T15:37:27.552+0000    writing test1.person to 
2022-12-18T15:37:27.553+0000    done dumping test1.person (8 documents)
 
 
root@evassh-13661150:~# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d test2 -c student -o /opt/collection_2 --gzip
2022-12-18T15:38:16.975+0000    writing test2.student to 
2022-12-18T15:38:16.977+0000    done dumping test2.student (8 documents)
 
 
root@evassh-13661150:~# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d test2 -o /opt/mongodb_2 --gzip
2022-12-18T15:38:52.390+0000    writing test2.student to 
2022-12-18T15:38:52.391+0000    done dumping test2.student (8 documents)

二、数据恢复

任务描述

本关任务:按照编程要求恢复数据。

相关知识

为了完成本关任务,你需要掌握: 1.掌握 mongorestore 恢复工具的参数含义; 2.如何使用 mongorestore 恢复数据。

mongorestore 恢复工具
参数	参数说明
-h	指明数据库宿主机的IP
-u	指明数据库的用户名
-p	指明数据库的密码
-d	指明数据库的名字
-c	指明collection的名字
-o	指明到要导出的文件名
-q	指明导出数据的过滤条件
--authenticationDatabase	验证数据的名称
--gzip	备份时压缩
--oplog	use oplog for taking a point-in-time snapshot
--drop	恢复的时候把之前的集合drop掉
使用 mongorestore 恢复数据
  • 全库备份中恢复单库(基于之前的全库备份)

mongorestore -h 127.0.0.1:27017 -uroot -proot --authenticationDatabase admin -d test --drop  /home/mongod
#从/home/mongod目录下恢复全部数据库的数据到本地27300端口中root用户中(基于第一关的备份,下同)
  • 恢复 test 库
mongorestore -h 127.0.0.1:27017 -uroot -proot --authenticationDatabase admin -d test /home/mongod/test
#从/home/mongod/test目录下恢复名为test的单个数据库的数据到本地27300端口中root用户中的test数据库
  • 恢复 test 库下的 haha 集合
mongorestore -h 127.0.0.1:27017 -uroot -proot --authenticationDatabase admin -d test -c haha /home/mongod/test/haha/haha.bson
#从/home/mongod/test/haha目录下恢复集合的数据到本地27300端口中root用户的test数据库的haha集合中

--drop 参数实践恢复

# 恢复单库
mongorestore -h 127.0.0.1:27017 -uroot -proot --authenticationDatabase admin -d test --drop /home/mongod/test
# 恢复单表
mongorestore -h 127.0.0.1:27017 -uroot -proot --authenticationDatabase admin -d test -c vast --drop /home/mongod/test/haha/haha.bson
编程要求

根据提示,在右侧命令行进行操作,将第一关备份的数据按以下要求恢复(以下均在默认端口为27017的客户端进行,无用户和密码):

  • 将 /opt/mongodb 目录下的数据恢复到 MongoDB 中;

  • 将 /opt/mongodb_1 目录下的数据恢复到 mytest1 数据库中;

  • 将 /opt/collection_1 目录下的数据恢复到 mytest2 数据库的 person 集合中;

  • 将 /opt/collection_2 目录下的数据恢复到 mytest3 数据库的 student 集合中,并删除之前备份的表;

  • 将 /opt/mongodb_2 目录下的数据恢复到 mytest4 的数据库中,并删除之前的备份的数据库。

root@evassh-13661150:~# mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin --drop /opt/mongodb
2022-12-18T15:48:05.720+0000    preparing collections to restore from
2022-12-18T15:48:05.788+0000    reading metadata for test1.person from /opt/mongodb/test1/person.metadata.json
2022-12-18T15:48:05.793+0000    reading metadata for test2.student from /opt/mongodb/test2/student.metadata.json
2022-12-18T15:48:05.815+0000    restoring test1.person from /opt/mongodb/test1/person.bson
2022-12-18T15:48:05.815+0000    restoring test2.student from /opt/mongodb/test2/student.bson
2022-12-18T15:48:05.818+0000    no indexes to restore
2022-12-18T15:48:05.818+0000    finished restoring test1.person (8 documents)
2022-12-18T15:48:05.818+0000    no indexes to restore
2022-12-18T15:48:05.818+0000    finished restoring test2.student (8 documents)
2022-12-18T15:48:05.818+0000    done
 
 
root@evassh-13661150:~# mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest1 /opt/mongodb_1/test1
2022-12-18T15:48:36.693+0000    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-12-18T15:48:36.693+0000    building a list of collections to restore from /opt/mongodb_1/test1 dir
2022-12-18T15:48:36.694+0000    reading metadata for mytest1.person from /opt/mongodb_1/test1/person.metadata.json
2022-12-18T15:48:36.706+0000    restoring mytest1.person from /opt/mongodb_1/test1/person.bson
2022-12-18T15:48:36.708+0000    no indexes to restore
2022-12-18T15:48:36.708+0000    finished restoring mytest1.person (8 documents)
2022-12-18T15:48:36.708+0000    done
 
 
root@evassh-13661150:~# mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest2 -c person /opt/collection_1/test1/person.bson
2022-12-18T15:48:45.065+0000    checking for collection data in /opt/collection_1/test1/person.bson
2022-12-18T15:48:45.065+0000    reading metadata for mytest2.person from /opt/collection_1/test1/person.metadata.json
2022-12-18T15:48:45.077+0000    restoring mytest2.person from /opt/collection_1/test1/person.bson
2022-12-18T15:48:45.140+0000    no indexes to restore
2022-12-18T15:48:45.140+0000    finished restoring mytest2.person (8 documents)
2022-12-18T15:48:45.140+0000    done
 
 
root@evassh-13661150:~# mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest3 -c student --gzip --drop /opt/collection_2/test2/student.bson.gz
2022-12-18T15:48:52.830+0000    checking for collection data in /opt/collection_2/test2/student.bson.gz
2022-12-18T15:48:52.830+0000    reading metadata for mytest3.student from /opt/collection_2/test2/student.metadata.json.gz
2022-12-18T15:48:52.843+0000    restoring mytest3.student from /opt/collection_2/test2/student.bson.gz
2022-12-18T15:48:52.906+0000    no indexes to restore
2022-12-18T15:48:52.907+0000    finished restoring mytest3.student (8 documents)
2022-12-18T15:48:52.907+0000    done
 
 
root@evassh-13661150:~# mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest4  --gzip --drop /opt/mongodb_2/test2/student.bson.gz
2022-12-18T15:49:00.810+0000    checking for collection data in /opt/mongodb_2/test2/student.bson.gz
2022-12-18T15:49:00.810+0000    reading metadata for mytest4.student from /opt/mongodb_2/test2/student.metadata.json.gz
2022-12-18T15:49:00.822+0000    restoring mytest4.student from /opt/mongodb_2/test2/student.bson.gz
2022-12-18T15:49:00.885+0000    no indexes to restore
2022-12-18T15:49:00.885+0000    finished restoring mytest4.student (8 documents)
2022-12-18T15:49:00.885+0000    done

相关推荐

  1. mongodb-数据备份恢复

    2024-04-13 09:16:03       26 阅读
  2. 数据库备份恢复

    2024-04-13 09:16:03       30 阅读
  3. MongoDB入门实践

    2024-04-13 09:16:03       28 阅读
  4. mongodb备份恢复

    2024-04-13 09:16:03       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 09:16:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 09:16:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 09:16:03       87 阅读
  4. Python语言-面向对象

    2024-04-13 09:16:03       96 阅读

热门阅读

  1. 二维数组得学习

    2024-04-13 09:16:03       134 阅读
  2. Scala详解(2)

    2024-04-13 09:16:03       77 阅读
  3. 数据库DMP格式备份文件

    2024-04-13 09:16:03       122 阅读
  4. 3d max快捷键命令大全

    2024-04-13 09:16:03       42 阅读
  5. 用自动LOD简化3D网格【Babylon.js】

    2024-04-13 09:16:03       43 阅读
  6. Python如何使用不同的库来导出PDF和XLSX

    2024-04-13 09:16:03       43 阅读
  7. C语言--第二章之位运算符

    2024-04-13 09:16:03       36 阅读
  8. 熟练使用Nacos、GateWay、OpenFeign、Sentinel常用组件

    2024-04-13 09:16:03       46 阅读
  9. 微服务下使用sentinel进行服务熔断

    2024-04-13 09:16:03       42 阅读