mongodb备份恢复数据库

1、数据库备份还原

[root@ecs-14741540 ~]# mongodump  -u testwang -p testwang --authenticationDatabase=testwang   --gzip  --archive=testwang.gz --db=testwang  
2022-08-30T11:54:34.621+0800    writing testwang.testwang3 to archive 'testwang.gz'
2022-08-30T11:54:34.629+0800    done dumping testwang.testwang3 (1 document)
2022-08-30T11:54:34.634+0800    writing testwang.testwang2 to archive 'testwang.gz'
2022-08-30T11:54:34.680+0800    done dumping testwang.testwang2 (3 documents)

--超级管理员
[root@ecs-14741540 ~]# mongodump  -u admin -p Admin --authenticationDatabase=testwang   --gzip  --archive=testwang.gz --db=testwang  
2022-08-30T11:56:44.227+0800    Failed: can't create session: could not connect to server: connection() error occurred during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.

当加入权限验证时,备份的时候要用对应有权限的账号备份,不能用超级管理员的账号备份,恢复时可以用超级管理员备份

恢复:

[root@ecs-14741540 ~]# mongorestore -u admin -p Admin --gzip --archive=testwang.gz --nsInclude="testwang.*" 
2022-08-30T12:03:48.879+0800    preparing collections to restore from
2022-08-30T12:03:48.901+0800    reading metadata for testwang.testwang3 from archive 'testwang.gz'
2022-08-30T12:03:48.901+0800    reading metadata for testwang.testwang2 from archive 'testwang.gz'
2022-08-30T12:03:48.941+0800    restoring testwang.testwang3 from archive 'testwang.gz'
2022-08-30T12:03:48.953+0800    finished restoring testwang.testwang3 (1 document, 0 failures)
2022-08-30T12:03:48.991+0800    restoring testwang.testwang2 from archive 'testwang.gz'
2022-08-30T12:03:49.005+0800    finished restoring testwang.testwang2 (3 documents, 0 failures)
2022-08-30T12:03:49.005+0800    no indexes to restore for collection testwang.testwang3
2022-08-30T12:03:49.005+0800    no indexes to restore for collection testwang.testwang2
2022-08-30T12:03:49.005+0800    4 document(s) restored successfully. 0 document(s) failed to restore.

以–gzip形式备份的,恢复时也应该用gzip的方式恢复,不用将备份文件单独解压

2、集合备份还原

备份

mongodump --archive=/opt/mongobackup/testrecord.20220715.archive --db=testdb --collection=testrecord--username=admin --password=Admin  --authenticationDatabase=testdb

还原:

mongorestore --archive=/opt/mongobackup/testrecord.20220715.archive  --nsInclude='testdb.testrecord'  --nsFrom='testdb.testrecord' --nsTo='testdb.testrecord20220726'   --username=admin --password=Admin --authenticationDatabase=testdb

还原时还可将集合的名字改成另一个

特别注意在WINDOWS下,nsInclude 后面的内容不能有引号,否则会失败

mongorestore --archive=testrecord.20220715.archive  --nsInclude='testdb.testrecord'  --失败
mongorestore --archive=testrecord.20220715.archive  --nsInclude=testdb.testrecord  --成功,不能要引号
mongorestore --archive=testrecord.20220715.archive  --nsInclude=testdb.testrecord --nsFrom=testdb.testrecord  --nsTo=testdb.testrecord 20220727    --成功,不能要引号

你可能感兴趣的:(mongodb,数据库)