【MongoDB】全款备份恢复/按库备份恢复/按集合备份恢复

1.全库备份 

[mongod@mysql57 mongodb]$ mongodump -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin -o /mongodb/backup
2024-06-14T10:26:35.038+0800	writing admin.system.users to 
2024-06-14T10:26:35.059+0800	done dumping admin.system.users (2 documents)
2024-06-14T10:26:35.059+0800	writing admin.system.version to 
2024-06-14T10:26:35.081+0800	done dumping admin.system.version (2 documents)
2024-06-14T10:26:35.081+0800	writing test.users to 
2024-06-14T10:26:35.082+0800	writing test.students to 
2024-06-14T10:26:35.116+0800	done dumping test.students (9 documents)
2024-06-14T10:26:35.118+0800	done dumping test.users (21 documents)

--导出内容如下。
[mongod@mysql57 backup]$ ll
总用量 0
drwxrwxr-x 2 mongod mongod 128 6月  14 10:26 admin
drwxrwxr-x 2 mongod mongod 102 6月  14 10:26 test
[mongod@mysql57 backup]$ ll admin/
总用量 16
-rw-rw-r-- 1 mongod mongod 1082 6月  14 10:26 system.users.bson
-rw-rw-r-- 1 mongod mongod  225 6月  14 10:26 system.users.metadata.json
-rw-rw-r-- 1 mongod mongod  104 6月  14 10:26 system.version.bson
-rw-rw-r-- 1 mongod mongod  134 6月  14 10:26 system.version.metadata.json
[mongod@mysql57 backup]$ ll test/
总用量 16
-rw-rw-r-- 1 mongod mongod  819 6月  14 10:26 students.bson
-rw-rw-r-- 1 mongod mongod  127 6月  14 10:26 students.metadata.json
-rw-rw-r-- 1 mongod mongod 1877 6月  14 10:26 users.bson
-rw-rw-r-- 1 mongod mongod  215 6月  14 10:26 users.metadata.json

可以使用--drop选项,解决文档已经存在的情况。  

2.备份指定的数据库 

--备份指定的库。
mongodump -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin -d test -o /mongodb/backup/tmp
[mongod@mysql57 backup]$ mongodump -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin -d test -o /mongodb/backup/tmp
2024-06-14T10:30:24.890+0800	writing test.users to 
2024-06-14T10:30:24.890+0800	writing test.students to 
2024-06-14T10:30:24.916+0800	done dumping test.users (21 documents)
2024-06-14T10:30:24.918+0800	done dumping test.students (9 documents)

--备份结果如下:
[mongod@mysql57 backup]$ ll tmp/
总用量 0
drwxrwxr-x 2 mongod mongod 102 6月  14 10:30 test
[mongod@mysql57 backup]$ ll tmp/test
总用量 16
-rw-rw-r-- 1 mongod mongod  819 6月  14 10:30 students.bson
-rw-rw-r-- 1 mongod mongod  127 6月  14 10:30 students.metadata.json
-rw-rw-r-- 1 mongod mongod 1877 6月  14 10:30 users.bson
-rw-rw-r-- 1 mongod mongod  215 6月  14 10:30 users.metadata.json

 可以使用--drop选项,解决文档已经存在的情况。  

3.恢复指定的数据库 

--mongodb单个库导入。
删除test数据库并导入备份的数据。
> use admin 
switched to db admin
> db.auth("admin","admin")
1
> show dbs; 
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB
> use test 
switched to db test
> show collections; 
students
users
> db.students.drop()
true
> db.users.drop()
true
> 



mongorestore -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin -d test /mongodb/backup/tmp/test
[mongod@mysql57 mongodb]$ mongorestore -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin -d test /mongodb/backup/tmp/test
2024-06-14T10:41:02.127+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
2024-06-14T10:41:02.128+0800	building a list of collections to restore from /mongodb/backup/tmp/test dir
2024-06-14T10:41:02.147+0800	reading metadata for test.users from /mongodb/backup/tmp/test/users.metadata.json
2024-06-14T10:41:02.177+0800	restoring test.users from /mongodb/backup/tmp/test/users.bson
2024-06-14T10:41:02.191+0800	reading metadata for test.students from /mongodb/backup/tmp/test/students.metadata.json
2024-06-14T10:41:02.219+0800	restoring indexes for collection test.users from metadata
2024-06-14T10:41:02.230+0800	restoring test.students from /mongodb/backup/tmp/test/students.bson
2024-06-14T10:41:02.258+0800	finished restoring test.users (21 documents)
2024-06-14T10:41:02.264+0800	no indexes to restore
2024-06-14T10:41:02.264+0800	finished restoring test.students (9 documents)
2024-06-14T10:41:02.264+0800	done

恢复完成,恢复的文档数量可以看到。
--数据检查,说明恢复没有问题。
> show collections;
students
users
> db.students.find()
{ "_id" : ObjectId("666aa9ffb93ce01433bd1c27"), "Name" : "S1", "Age" : 25, "Gender" : "M", "Class" : "C1", "Score" : 95 }
{ "_id" : ObjectId("666aa9ffb93ce01433bd1c28"), "Name" : "S2", "Age" : 18, "Gender" : "M", "Class" : "C1", "Score" : 86 }
{ "_id" : ObjectId("666aa9ffb93ce01433bd1c29"), "Name" : "S3", "Age" : 19, "Gender" : "M", "Class" : "C1", "Score" : 52 }
{ "_id" : ObjectId("666aa9ffb93ce01433bd1c2a"), "Name" : "S4", "Age" : 30, "Gender" : "M", "Class" : "C1", "Score" : 70 }
{ "_id" : ObjectId("666aa9ffb93ce01433bd1c2b"), "Name" : "S1", "Age" : 20, "Gender" : "M", "Class" : "C1", "Score" : 95 }
{ "_id" : ObjectId("666aa9ffb93ce01433bd1c2c"), "Name" : "S6", "Age" : 21, "Gender" : "M", "Class" : "C1", "Score" : 95 }
{ "_id" : ObjectId("666aa9ffb93ce01433bd1c2d"), "Name" : "S7", "Age" : 22, "Gender" : "F", "Class" : "C1", "Score" : 100 }
{ "_id" : ObjectId("666aa9ffb93ce01433bd1c2e"), "Name" : "S8", "Age" : 23, "Gender" : "F", "Class" : "C1", "Score" : 95 }
{ "_id" : ObjectId("666aaa00b93ce01433bd1c2f"), "Name" : "S9", "Age" : 25, "Gender" : "F", "Class" : "C1", "Score" : 95 }
> db.users.find()
{ "_id" : ObjectId("666a6175a0184f8213fade86"), "FName" : "Test User", "Age" : 45, "Gender" : "F", "Country" : "US" }
{ "_id" : ObjectId("666a617da0184f8213fade87"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade88"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade89"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8a"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8b"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8c"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8d"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8e"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8f"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade90"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade91"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade92"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade93"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade94"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade95"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade96"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade97"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade98"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade99"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "India" }
Type "it" for more

可以使用--drop选项,解决文档已经存在的情况。   

4.仅恢复指定的集合 

--只恢复指定集合。
--恢复test数据库下的users集合。
mongorestore -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin -d test -c users /mongodb/backup/test/users.bson
[mongod@mysql57 mongodb]$ mongorestore -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin -d test -c users /mongodb/backup/test/users.bson
2024-06-14T10:51:26.883+0800	checking for collection data in /mongodb/backup/test/users.bson
2024-06-14T10:51:26.908+0800	reading metadata for test.users from /mongodb/backup/test/users.metadata.json
2024-06-14T10:51:26.908+0800	restoring test.users from /mongodb/backup/test/users.bson
2024-06-14T10:51:26.942+0800	error: multiple errors in bulk operation:
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a6175a0184f8213fade86') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade87') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade88') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade89') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8a') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8b') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8c') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8d') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8e') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8f') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade90') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade91') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade92') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade93') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade94') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade95') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade96') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade97') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade98') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade99') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade9a') }

2024-06-14T10:51:26.971+0800	restoring indexes for collection test.users from metadata
2024-06-14T10:51:26.992+0800	finished restoring test.users (21 documents)
2024-06-14T10:51:26.992+0800	done

--由此监控,如果不删除原来的数据,就会报主键冲突。所以我们删除原来的数据重新导入。
> show collections;
students
users
> db.users.drop()
true
--单个集合恢复完成。
[mongod@mysql57 mongodb]$ mongorestore -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin -d test -c users /mongodb/backup/test/users.bson
2024-06-14T10:55:19.328+0800	checking for collection data in /mongodb/backup/test/users.bson
2024-06-14T10:55:19.352+0800	reading metadata for test.users from /mongodb/backup/test/users.metadata.json
2024-06-14T10:55:19.394+0800	restoring test.users from /mongodb/backup/test/users.bson
2024-06-14T10:55:19.469+0800	restoring indexes for collection test.users from metadata
2024-06-14T10:55:19.506+0800	finished restoring test.users (21 documents)
2024-06-14T10:55:19.506+0800	done
--数据检查。
--发现又恢复成功了。
> show collections;
students
users
> db.users.find()
{ "_id" : ObjectId("666a6175a0184f8213fade86"), "FName" : "Test User", "Age" : 45, "Gender" : "F", "Country" : "US" }
{ "_id" : ObjectId("666a617da0184f8213fade89"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8b"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8e"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8f"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade92"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade96"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade9a"), "Name" : "Test User20", "Age" : 30, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade87"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade88"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8a"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8c"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade8d"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade90"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade91"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade93"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade94"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade95"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade97"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a617da0184f8213fade98"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "India" }
Type "it" for more
> 

可以使用--drop选项,解决文档已经存在的情况。   

5.全库恢复 

--全库恢复。
--由此可见,全款恢复时,先恢复其他数据库,最后恢复admin数据库里面的system.users集合。
mongorestore -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin  /mongodb/backup
[mongod@mysql57 backup]$ mongorestore -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin  /mongodb/backup
2024-06-14T11:03:04.292+0800	preparing collections to restore from
2024-06-14T11:03:04.335+0800	reading metadata for test.students from /mongodb/backup/test/students.metadata.json
2024-06-14T11:03:04.361+0800	reading metadata for test.users from /mongodb/backup/test/users.metadata.json
2024-06-14T11:03:04.373+0800	restoring test.students from /mongodb/backup/test/students.bson
2024-06-14T11:03:04.407+0800	no indexes to restore
2024-06-14T11:03:04.407+0800	finished restoring test.students (9 documents)
2024-06-14T11:03:04.407+0800	restoring test.users from /mongodb/backup/test/users.bson
2024-06-14T11:03:04.434+0800	restoring indexes for collection test.users from metadata
2024-06-14T11:03:04.461+0800	finished restoring test.users (21 documents)
2024-06-14T11:03:04.461+0800	restoring users from /mongodb/backup/admin/system.users.bson
2024-06-14T11:03:04.582+0800	done

可以使用--drop选项,解决文档已经存在的情况。  

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值