Skip to content

MongoDB - Create Backup

Creating backups is a critical administrative task for any database. This chapter explores modern methods for backing up and restoring your MongoDB data, from command-line tools for local development to managed cloud solutions for production.

Modern Approach: MongoDB Atlas (Cloud Backup)

Section titled “Modern Approach: MongoDB Atlas (Cloud Backup)”

For production environments, the recommended best practice is to use a managed service like MongoDB Atlas. Atlas provides automated, continuous backups with point-in-time recovery. This eliminates the manual effort and potential for error associated with scripting your own backup solutions.

  • Fully Managed: Backups are scheduled and managed automatically.
  • Continuous & Point-in-Time Recovery: Restore your database to any specific moment within a retention period.
  • Queryable Snapshots: You can directly query your backup snapshots without a full restore, which is useful for data exploration or auditing.
  • Geographically Distributed: Backups are stored in the cloud region of your choice, ensuring disaster recovery.

If you are running a production application, migrating to Atlas for its operational benefits, including backups, is strongly advised.

Local/Manual Approach: mongodump and mongorestore

Section titled “Local/Manual Approach: mongodump and mongorestore”

For local development, migrating data between environments, or situations where a managed service isn’t an option, MongoDB provides the mongodump and mongorestore command-line tools. These are part of the MongoDB Database Tools, which may need to be installed separately from the MongoDB Server.

mongodump exports the content of a database into a BSON file format, creating a directory with the exported data. The modern and most reliable way to connect is by using a connection string URI.

mongodump --uri="<connection-string>" --out="<output-directory>"

This command backs up the inventory database from a local MongoDB instance to a directory named backup_dir.

# This command will create a directory named 'backup_dir' in the current location.
mongodump --uri="mongodb://localhost:27017/inventory" --out="backup_dir"
# Sample Output:
# 2023-07-06T10:30:00.123+00:00 writing inventory.products to backup_dir/inventory/products.bson
# 2023-07-06T10:30:00.125+00:00 done dumping inventory.products (1500 documents)
# 2023-07-06T10:30:00.125+00:00 see backup_dir/inventory/products.metadata.json for collection metadata

mongorestore can read the output from mongodump and restore it to a running MongoDB instance.

To restore, you point mongorestore to the backup directory created by mongodump.

mongorestore --uri="<connection-string>" <backup-directory>

This command restores the data from backup_dir into a new database called inventory_restored on the local MongoDB instance.

# The --nsFrom and --nsTo options allow remapping the database/collection names during restore.
mongorestore --uri="mongodb://localhost:27017" --nsFrom="inventory.*" --nsTo="inventory_restored.*" backup_dir
# Sample Output:
# 2023-07-06T10:35:00.200+00:00 the --db and --collection flags are deprecated for this use-case; please use --nsInclude instead
# 2023-07-06T10:35:00.250+00:00 preparing collections to restore
# 2023-07-06T10:35:00.300+00:00 reading metadata for inventory_restored.products from backup_dir/inventory/products.metadata.json
# 2023-07-06T10:35:00.500+00:00 restoring inventory_restored.products from backup_dir/inventory/products.bson
# 2023-07-06T10:35:01.000+00:00 finished restoring inventory_restored.products (1500 documents)
# 2023-07-06T10:35:01.000+00:00 1500 document(s) restored successfully. 0 document(s) failed to restore.
  • Use --archive and --gzip: For large databases, you can avoid writing files to a temporary directory by using the --archive flag to stream the output. Combine it with --gzip to compress the backup on the fly.
  • Example: mongodump --uri="..." --archive --gzip > my_backup.gz
  • Restore from Archive: mongorestore --uri="..." --archive --gzip < my_backup.gz
  • Regularly Test Backups: A backup is useless if it can’t be restored. Regularly practice restoring your backups to a staging environment to ensure their integrity.
  • Include Authentication: Always use a connection string URI that includes the username and password for authenticated databases to ensure the tools have the correct permissions.