mongodump dan mongorestore untuk Pencadangan Lengkap
Peserta didik akan membuat dump BSON lengkap dari sebuah basis data dan memulihkannya ke kluster lain dengan mongorestore.
mongodump dan mongorestore untuk Pencadangan Lengkap adalah pelajaran MongoDB Academy gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar MongoDB Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus MongoDB Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.jsonDumping 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.jsonCompressing 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.gzArchiving 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.gzBasic 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-15Restoring 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-15The --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/myappParallel 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 8Backup 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
fiPoint-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.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “mongodump dan mongorestore untuk Pencadangan Lengkap” gratis?
Ya — teks lengkap “mongodump dan mongorestore untuk Pencadangan Lengkap” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus MongoDB Academy, upgrade ke CoddyKit PRO. Kursus MongoDB Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “mongodump dan mongorestore untuk Pencadangan Lengkap”?
Peserta didik akan membuat dump BSON lengkap dari sebuah basis data dan memulihkannya ke kluster lain dengan mongorestore. Kamu berlatih MongoDB Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai MongoDB Academy?
Tidak diperlukan pengalaman sebelumnya. MongoDB Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.
Berapa lama pelajaran “mongodump dan mongorestore untuk Pencadangan Lengkap” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran MongoDB Academy ini?
Ya. Setiap pelajaran MongoDB Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- mongoimport: Memuat Berkas JSON dan CSV
- mongoexport: Mengekspor Koleksi ke Berkas
- mongodump dan mongorestore untuk Pencadangan Lengkap
- Menyiapkan Data dengan Skrip Node.js