MongoDB备份与恢复

环境信息

生产环境

节点名称 节点IP 节点角色
redis51 172.16.1.51 MongoDB分片集群
redis52 172.16.1.52 MongoDB分片集群
redis53 172.16.1.53 MongoDB分片集群

新集群环境

节点名称 节点IP 节点角色
mongo1 172.16.1.41 MongoDB分片集群
mongo2 172.16.1.43 MongoDB分片集群
mongo3 172.16.1.47 MongoDB分片集群

备份数据

查询数据库数据条目

mongos> use xxx;
switched to db xxx
mongos> db.getCollectionNames().forEach(function(c) { print("Collection: " + c + ", Count: " + db.getCollection(c).countDocuments({})); });
Collection: workflow, Count: 1283
Collection: workflow_exe_result, Count: 162603
Collection: workflow_product, Count: 189
Collection: workflow_snapshot, Count: 201

备份数据库

mongodump -u root -p "xxxxxxxx" -h 172.16.1.53 --port 27000 --authenticationDatabase admin --db xxxxx -o xxxxx

恢复数据

数据恢复到数据库

mongorestore --host 172.16.1:27000 --username root --password xxxxxxxx --authenticationDatabase admin --db llm_workflow_backup --dir llm_workflow/

恢复完成检查数据库

mongo --host 192.168.2.21:27000 --username root --password xxxxxxx --authenticationDatabase admin
MongoDB shell version v4.4.11
connecting to: mongodb://192.168.2.21:27000/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2ae6bd6f-2d56-42df-a5d5-cecd72ddc4e0") }
MongoDB server version: 4.4.11
mongos> show dbs;
admin 0.000GB
config 0.003GB
llm_workflow_backup 0.520GB
mongos>

检查数据库表

mongos> use xxxxx
switched to db xxxxx
mongos> show tables;
workflow
workflow_exe_result
workflow_product
workflow_snapshot
mongos>

确认备份数据和恢复数据保持一致

mongos> db.getCollectionNames().forEach(function(c) { print("Collection: " + c + ", Count: " + db.getCollection(c).countDocuments({})); });
Collection: workflow, Count: 1283
Collection: workflow_exe_result, Count: 162607
Collection: workflow_product, Count: 189
Collection: workflow_snapshot, Count: 201
mongos>
文章作者: 慕容峻才
文章链接: https://www.acaiblog.top/MongoDB备份与恢复/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿才的博客
微信打赏
支付宝打赏