Backing Up MongoDB Data: A Comprehensive Guide to mongodump and mongoexport

Chetan Gupta - Jun 28 - - Dev Community

When managing MongoDB databases, having reliable backup and export strategies is crucial to safeguard your data and facilitate data analysis, migration, or sharing. In this blog post, we will explore two essential MongoDB tools: mongodump and mongoexport. We’ll cover how to use these tools effectively to back up and export your MongoDB data.

Introduction to MongoDB Tools
MongoDB provides several tools to manage and interact with your databases. Among these tools, mongodump and mongoexport are specifically designed for backing up and exporting data.

  • mongodump: This utility creates a binary backup of your MongoDB database.
  • mongoexport: This utility exports data from a MongoDB collection to a JSON or CSV file.

Installing MongoDB Tools
Before using these tools, ensure you have the MongoDB tools installed. You can install them using a package manager or by downloading them from the MongoDB Download Center.

For Ubuntu users, install MongoDB tools with the following command:

sudo apt-get install mongodb-org-tools
Using mongodump for Backups
Enter fullscreen mode Exit fullscreen mode

Basic mongodump Usage
mongodump creates a binary export (BSON) of the data in your MongoDB database. Here’s a basic example of how to use it:

mongodump --db myDatabase --out /path/to/backup
Enter fullscreen mode Exit fullscreen mode

This command will back up the myDatabase database to the specified directory.

Filtering Data with mongodump
You can filter the data to back up using the --query option. For example, to back up documents created between 2024-04-01 1:00 AM and 2024-04-01 2:00 AM, use the following command:

mongodump --db myDatabase --collection myCollection --query '{"creationDate": {"$gte": {"$date": "2024-04-01T01:00:00Z"}, "$lt": {"$date": "2024-04-01T02:00:00Z"}}}' --out /path/to/backup
Enter fullscreen mode Exit fullscreen mode

Specifying Read Preference
To ensure the backup is taken from a secondary node, you can specify the read preference:

mongodump --db myDatabase --collection myCollection --query '{"creationDate": {"$gte": {"$date": "2024-04-01T01:00:00Z"}, "$lt": {"$date": "2024-04-01T02:00:00Z"}}}' --readPreference '{"mode": "secondary"}' --out /path/to/backup
Enter fullscreen mode Exit fullscreen mode

Automating mongodump with a Script
Here’s a shell script that automates the mongodump process for a specific time range:

#!/bin/bash

# Define the start and end times
start_time="2024-04-01T01:00:00Z"
end_time="2024-04-01T02:00:00Z"

# Perform the mongodump with the query filter using the indexed column
mongodump --db myDatabase --collection myCollection --query "{\"creationDate\": {\"\$gte\": {\"\$date\": \"$start_time\"}, \"\$lt\": {\"\$date\": \"$end_time\"}}}" --readPreference '{"mode": "secondary"}' --out /path/to/backup

echo "Backup completed for documents from 2024-04-01 1:00 AM to 2:00 AM using indexed column creationDate."
Enter fullscreen mode Exit fullscreen mode

Save this script as backup_specific_hour.sh, make it executable, and run it:

chmod +x backup_specific_hour.sh
./backup_specific_hour.sh
Enter fullscreen mode Exit fullscreen mode

Using mongoexport for Data Export

Basic mongoexport Usage
mongoexport is used to export data from a MongoDB collection to a JSON or CSV file. Here’s a basic example of exporting data to a JSON file:

mongoexport --db myDatabase --collection myCollection --out output.json --jsonArray
Enter fullscreen mode Exit fullscreen mode

Filtering Data with mongoexport
You can filter data to export by using the --query option. For example, to export only documents with a specific status:

mongoexport --db myDatabase --collection myCollection --out output.json --jsonArray --query '{"status": "active"}'
Enter fullscreen mode Exit fullscreen mode

Specifying Fields
You can specify which fields to export using the --fields option:

mongoexport --db myDatabase --collection myCollection --out output.json --jsonArray --fields "field1,field2,field3"
Enter fullscreen mode Exit fullscreen mode

Automating mongoexport with a Script
Here’s a shell script that automates the mongoexport process:

#!/bin/bash

# Define database, collection, and output file names
db_name="myDatabase"
collection_name="myCollection"
output_file="output.json"

# Export data from MongoDB collection to JSON file
mongoexport --db "$db_name" --collection "$collection_name" --out "$output_file" --jsonArray

echo "Export completed: $collection_name from $db_name to $output_file"
Enter fullscreen mode Exit fullscreen mode

Save this script as export_mongo_to_json.sh, make it executable, and run it:

chmod +x export_mongo_to_json.sh
./export_mongo_to_json.sh
Enter fullscreen mode Exit fullscreen mode

Conclusion
Both mongodump and mongoexport are powerful tools for managing MongoDB data. Whether you need to back up your entire database or export specific data for analysis, these utilities provide the flexibility and control required for effective data management. By automating these processes with scripts, you can streamline your workflows and ensure your data is always safe and accessible.

. .