In this guide, we'll walk you through the process of creating a backup of your MongoDB database using the mongodump utility program. MongoDB provides various options for creating backups to ensure the safety of your data.
Using the mongodump
Utility
Mongodump
is a versatile tool designed for creating database backups efficiently. It allows you to capture a snapshot of your MongoDB data, which can later be restored if needed.
Step 1: Choose Your Backup Option
You have several options for running mongodump
to create a backup. Select the one that best suits your needs.
Option 1: Backup from a Specific MongoDB Host
This option is suitable when you want to create a backup from a specific MongoDB host.
mongodump --host="hostname:port" \
--username="username" --password="password" \
--authenticationDatabase "admin" \
--db="db name" \
--forceTableScan -v --gzip --out {backup directory location}
Option 2: Backup Using a MongoDB URI
Use this option if you have a MongoDB URI that includes all the necessary connection information.
mongodump --uri="mongodb://username:password@host:port/?authSource=admin" \
--db="db name" \
--forceTableScan -v --gzip --out {backup directory location}
Here's an example of how the command looks:
Option 3: Backup for MongoDB Atlas
If you are using MongoDB Atlas, you can create a backup as follows:
mongodump "mongodb+srv://{CONNECTION_STRING}" \
--username {user_name} --db="{DATABASE}" --forceTableScan -v \
--out {backup directory location}
Here's an example of how the command looks:
mongodump "mongodb+srv://myusername:mypassword@mycluster.mongodb.net/test" \
--username myusername --db="mydatabase" --forceTableScan -v \
--out /backup
Step 2: Perform the Backup
Execute the chosen mongodump
command from your terminal. The command will connect to the MongoDB instance and create a backup of the specified database.
Step 3: Verify the Backup
Once the backup process is complete, you can verify its success by checking the specified backup directory.
Step 4: Compress and Download the Backup
To save storage space and facilitate transfer, it's a good practice to compress the backup directory. After compression, you can download the backup to your local machine or a designated storage location.