0Pricing
MongoDB Academy · 강의

전체 백업을 위한 mongodump와 mongorestore

학습자는 데이터베이스의 전체 BSON 덤프를 만들고 mongorestore로 다른 클러스터에 복원합니다.

전체 백업을 위한 mongodump와 mongorestore은(는) CoddyKit의 무료 MongoDB Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 MongoDB Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. MongoDB Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Why Use mongodump for Backups?

mongodump creates a binary BSON snapshot of a database or collection. Unlike mongoexport, which produces human-readable JSON, mongodump preserves all BSON type information exactly—ObjectIds, dates, decimals, binary data—with zero loss of fidelity. This makes it the correct tool for full database backups and migrations between MongoDB clusters, while mongoexport is better for selective data sharing.

Basic mongodump Usage

Running mongodump without arguments dumps all databases from the local MongoDB instance to a dump/ directory. Specify --uri, --db, and optionally --collection to narrow the scope. The output directory contains one subdirectory per database, each with .bson and .metadata.json files per collection.

# Dump the entire 'myapp' database
mongodump \
  --uri 'mongodb://localhost:27017' \
  --db myapp \
  --out /backups/myapp-2024-01-15

# Output structure:
# /backups/myapp-2024-01-15/
#   myapp/
#     users.bson
#     users.metadata.json
#     products.bson
#     products.metadata.json

Dumping a Single Collection

Use --collection to dump only one collection from a database. This produces a single .bson file and its corresponding .metadata.json (which stores index definitions). Targeted collection dumps are useful for extracting a single collection for migration without backing up the entire database.

# Dump only the 'orders' collection
mongodump \
  --uri 'mongodb://localhost:27017' \
  --db myapp \
  --collection orders \
  --out /backups/orders-backup

# Result: /backups/orders-backup/myapp/orders.bson
#         /backups/orders-backup/myapp/orders.metadata.json

Compressing the Dump

Large BSON dumps can consume significant disk space. Use --gzip to compress the output files as they are written. Each .bson file becomes a .bson.gz file, typically achieving 5-10x compression for typical document collections. Compression runs in the mongodump process and requires a matching --gzip flag in mongorestore to decompress.

# Create a compressed backup
mongodump \
  --uri 'mongodb://localhost:27017' \
  --db myapp \
  --gzip \
  --out /backups/myapp-compressed-2024-01-15

# Files are now .bson.gz:
# /backups/myapp-compressed-2024-01-15/myapp/users.bson.gz
# /backups/myapp-compressed-2024-01-15/myapp/orders.bson.gz

Archiving to a Single File

Use --archive to write the entire dump to a single archive file instead of a directory tree. You can also pipe it to gzip or aws s3 cp for one-step compressed backup and upload. This is the most compact and portable dump format for automating backups in CI or cron jobs.

# Archive + compress to a single file
mongodump \
  --uri 'mongodb://localhost:27017' \
  --db myapp \
  --archive=/backups/myapp-2024-01-15.archive.gz \
  --gzip

# Or pipe to gzip
mongodump \
  --uri 'mongodb://localhost:27017' \
  --db myapp \
  --archive | gzip > /backups/myapp-2024-01-15.gz

Basic mongorestore Usage

mongorestore reads a directory or archive created by mongodump and inserts the documents into a target MongoDB cluster. It recreates collections with their index definitions (from the .metadata.json files) after the data is loaded. Specify the target cluster with --uri and the source directory or archive with --dir or --archive.

# Restore the full myapp database from a directory backup
mongorestore \
  --uri 'mongodb://restore-target:27017' \
  --dir /backups/myapp-2024-01-15

# Restore a gzip-compressed directory backup
mongorestore \
  --uri 'mongodb://restore-target:27017' \
  --gzip \
  --dir /backups/myapp-compressed-2024-01-15

Restoring to a Different Database Name

By default, mongorestore restores to the same database name as in the dump. Use --nsFrom and --nsTo (namespace from/to) to restore to a different database or collection name. This is essential when cloning production data into a staging database with a different name.

# Restore 'myapp' from dump into 'myapp_staging'
mongorestore \
  --uri 'mongodb://localhost:27017' \
  --nsFrom 'myapp.*' \
  --nsTo 'myapp_staging.*' \
  --dir /backups/myapp-2024-01-15

The --drop Flag in Restore

Like mongoimport, mongorestore has a --drop flag that drops each collection before restoring it. Without --drop, mongorestore merges restored documents with any existing data—useful for partial restores. With --drop, you get a clean replacement of the collection, which is safer for full database restores to avoid stale data mixing with the backup.

# Drop and fully replace all collections during restore
mongorestore \
  --uri 'mongodb://localhost:27017' \
  --db myapp \
  --drop \
  --dir /backups/myapp-2024-01-15/myapp

Parallel Restore With --numParallelCollections

For databases with many collections, mongorestore can restore multiple collections in parallel. Use --numParallelCollections (default: 4) to increase parallelism on servers with high I/O capacity. More parallel restores reduce total restore time but increase memory and CPU usage on both the client and server.

# Restore 8 collections in parallel
mongorestore \
  --uri 'mongodb://restore-target:27017' \
  --dir /backups/myapp-2024-01-15 \
  --numParallelCollections 8

Backup Automation With a Cron Job

Automate daily backups by running a shell script from a cron job. The script creates a timestamped backup directory, runs mongodump, compresses the result, and optionally uploads it to S3 or another remote storage. Always verify the exit code of mongodump to detect failed backups before the previous backup is overwritten.

#!/bin/bash
DATE=$(date +%Y%m%d)
BACKUP_DIR="/backups/myapp-$DATE"

mongodump \
  --uri "$MONGO_URI" \
  --db myapp \
  --gzip \
  --out "$BACKUP_DIR"

if [ $? -eq 0 ]; then
  echo "Backup successful: $BACKUP_DIR"
else
  echo "Backup FAILED!"
  exit 1
fi

Point-in-Time Backups in Atlas

MongoDB Atlas provides automated Point-in-Time (PIT) backups that capture snapshots every 6 hours and allow restore to any second within the retention window (up to 35 days on M10+ tiers). Atlas backups do not use mongodump—they leverage filesystem-level snapshots at the storage engine level for zero-impact backups. For self-hosted MongoDB, mongodump is the primary backup mechanism, but it is a logical backup and cannot capture in-flight transactions.

Quick Check

Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.

Lesson Recap

In this lesson you learned: mongodump creates binary BSON backups preserving all type information, mongorestore reads those dumps and recreates collections with their indexes, and --drop, --nsFrom/--nsTo, and --gzip are essential flags for production backup workflows. Next up we write Node.js seed scripts to programmatically load data into MongoDB during development.

자주 묻는 질문

“전체 백업을 위한 mongodump와 mongorestore” 강의는 무료인가요?

네 — “전체 백업을 위한 mongodump와 mongorestore” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 MongoDB Academy 강의 전체를 잠금 해제할 수 있습니다. MongoDB Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“전체 백업을 위한 mongodump와 mongorestore”에서 뭘 배우나요?

학습자는 데이터베이스의 전체 BSON 덤프를 만들고 mongorestore로 다른 클러스터에 복원합니다. 브라우저에서 직접 실행하는 실습 코드로 MongoDB Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

MongoDB Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 MongoDB Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“전체 백업을 위한 mongodump와 mongorestore” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 MongoDB Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 MongoDB Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. mongoimport: JSON 및 CSV 파일 불러오기
  2. mongoexport: 컬렉션을 파일로 내보내기
  3. 전체 백업을 위한 mongodump와 mongorestore
  4. Node.js 스크립트로 데이터 시드하기
← MongoDB Academy(으)로 돌아가기