Backup and restore
mongodump copies data out and mongorestore puts it back, but a restore is only real after you test it.
- Run mongodump to export a database to disk.
- Run mongorestore to load that export back into a database.
- Explain why a changing database makes point-in-time backups imperfect.
- Test a restore into a scratch database before trusting a backup.
In plain English
mongodump is a command-line tool that reads a database and writes its data to a folder you can store safely. mongorestore reads that folder and writes the data back into a database, so the two tools are a matching pair. A point-in-time backup is tricky because a database that keeps changing can be captured mid-write, so you should think about when the backup was taken and whether you can tolerate a small gap. The only way to trust a backup is to restore it into a test environment and check that your data is actually there. Backups are not a plan; a tested restore is the plan.
Step by step
- 1Run mongodump with --db and --out to export a database to a folder.
- 2Check that the folder now contains BSON and metadata files.
- 3Create a scratch database and run mongorestore to load the backup into it.
- 4Query the scratch database to confirm the documents came back.
- 5Schedule regular backups and store them somewhere separate from the database.
mongodump --db=library --out=/backups/library
mongorestore --db=library /backups/libraryCommon mistakes
- Taking a backup while writes are in flight can capture a document in a state that is not a clean point in time.
- Treating a backup that has never been restored as safe is a mistake; a restore test is the real proof.
- Restoring over a live database without a plan can overwrite or mix with existing data.
- Storing backups on the same server as the database means one disk failure loses both.
Verify a restore
After restoring into a scratch database, count the documents in the books collection and find one known title.
use libraryWhat is the strongest guarantee that your backup actually works?
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Lock in what you just learned.
Mark this lesson complete to save your place and move one step closer to the certificate.