DBAtlas · 数据库运维图谱

用 mongodump/mongorestore 进行归档备份并还原单个集合

MongoDB备份

命令

# Single database, one compressed archive file (password is prompted):
mongodump --uri="mongodb://admin@mongo1:27017/?authSource=admin" --db=shop --gzip --archive=/backup/shop_$(date +%F).archive.gz
# Dry run before restoring the archive under a different name:
mongorestore --uri="mongodb://admin@mongo1:27017/?authSource=admin" --gzip --archive=/backup/shop_2026-10-08.archive.gz --nsFrom="shop.*" --nsTo="shop_restore.*" --dryRun
# Restore a single collection from the archive under a new name:
mongorestore --uri="mongodb://admin@mongo1:27017/?authSource=admin" --gzip --archive=/backup/shop_2026-10-08.archive.gz --nsInclude="shop.musteri" --nsFrom="shop.*" --nsTo="shop_restore.*" --numInsertionWorkersPerCollection=4
# Restore under the same name, dropping the existing collection:
mongorestore --uri="mongodb://admin@mongo1:27017/?authSource=admin" --gzip --archive=/backup/shop_2026-10-08.archive.gz --nsInclude="shop.musteri" --drop

作用

将数据库备份到单个 gzip 归档文件中;从归档中还原整个数据库或单个集合,并可选择使用不同的名称(nsFrom/nsTo)。

适用场景

用于逻辑备份、在环境之间迁移数据,或恢复被误删或损坏的单个集合。

注意

mongodump 是逻辑备份:在大数据集上很慢,还原时要重建索引。在运行中的系统上,不带 --oplog 的转储不具备时间点一致性。--drop 会在还原开始前删除目标集合;先用 nsTo 以不同名称还原并验证。还原写入 primary,可能导致复制延迟。

在 DBAtlas 中打开 →

相关内容