
Automate MongoDB Atlas Backup Free
MongoDB Atlas provides a convenient way to manage your databases, but what if you want an extra layer of backup security without paying for premium features? In this guide, we'll create a robust automated backup solution for MongoDB Atlas free tier (M0 Sandbox) using simple tools and scripts.
Prerequisites
Before we begin, ensure you have:
- A MongoDB Atlas account with a free tier (M0) cluster
- MongoDB Database Tools installed on your system
- Basic knowledge of bash scripting
- Access to a Linux environment (or WSL for Windows users)
- At least 512MB of free storage space
Installing MongoDB Database Tools
First, let's install the required MongoDB tools:
You can follow this official guide as well: Install MongoDB Community Edition on Ubuntu
# For Ubuntu/Debian
sudo apt-get install gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
--dearmor
- Ubuntu 24.04(Noble)
- Ubuntu 22.04(Jammy)
- Ubuntu 20.04(Focal)
- RHEL/CentOS
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
# For RHEL/CentOS
sudo yum install -y mongodb-database-tools
Setting Up MongoDB Atlas
- Log in to your MongoDB Atlas account
- Navigate to your cluster
- Click "Connect" and choose "Connect your application"
- Copy the connection string
- Create a dedicated backup user:
- Go to Database Access
- Add new database user
- Set permissions to Atlas Admin privileges or Read And Write To Any Database privileges
Creating the Backup Script
Let's create a comprehensive bash script that will handle our backup process:
- First navigate to the folder where you want to create your script.
- Then, lets create a script named mongodb_backup.sh. You can use
touch mongodb_backup.shto create the file. - Then
nano mongodb_backup.shto open the file. Or if you have vim,vim mongodb_backup.shto open the file. - Copy, make changes and paste the following script into the file:
#!/bin/bash
# Environment variables (replace with your values)
MONGO_URI="mongodb+srv://<username>:<password>@<cluster>.mongodb.net/<database>"
COLLECTION1="users"
COLLECTION2="events"
# Get today's date in YYYY-MM-DD format
today=$(date +"%Y-%m-%d")
# Define the folder path
FOLDER_PATH="/home/<user>/mongobackup/$today"
# Create the folder if it doesn't exist
if [ ! -d "$FO" ]; then
mkdir -p "$FOLDER_PATH"
echo "Folder '$FOLDER_PATH' created successfully."
else
echo "Folder '$FOLDER_PATH' already exists."
fi
# Export COLLECTION1 collection
mongoexport --uri $MONGO_URI --collection $COLLECTION1 --type json --out $FOLDER_PATH/$COLLECTION1.json
# Export COLLECTION2 collection
mongoexport --uri $MONGO_URI --collection $COLLECTION2 --type json --out $FOLDER_PATH/$COLLECTION2.json
Automating with Cron
To schedule regular backups, we'll use cron jobs. Here's how to set it up:
- Save the script as
mongodb_backup.sh- If you are using nano, you can save the file by pressing
CTRL + Oand thenCTRL + X - If you are using vim, you can save the file by pressing
:wq
- If you are using nano, you can save the file by pressing
- Make it executable:
chmod +x mongodb_backup.sh
- Open your crontab:
crontab -e
If it shows error specify your editor:
EDITOR=nano crontab -e
use EDITOR=vim if you are using vim.
If you don't have crontab/cronie, you can install it using:
#ArchLinux
sudo pacman -S cronie
#Ubuntu
sudo apt-get install cronie
- Add this line to run the backup daily at 2 AM:
0 2 * * * /path/to/mongodb_backup.sh
You can change the time to your desired time. You can use crontab.guru to check and create the cron syntax.
Troubleshooting
Common issues and solutions:
-
Connection Errors:
- Verify IP whitelist settings in your mongodb atlas settings. You can add your current IP to the whitelist. If you add 0.0.0.0/0, it will allow all IP addresses.
- Check network connectivity
- Ensure correct credentials
-
Space Issues:
- Monitor backup sizes
- Adjust retention period
- Set up disk space alerts
-
Permission Problems:
- Verify user roles
- Check file permissions
- Validate execution rights
Conclusion
This automated backup solution provides a reliable safety net for your MongoDB Atlas free tier databases. While it doesn't replace MongoDB Atlas's premium backup features, it offers a robust alternative for development and small production environments. Remember to:
- Regularly verify your backups
- Monitor backup logs
- Test the restore process periodically
- Adjust retention and timing based on your needs
By following this guide, you've implemented a professional-grade backup solution that helps protect your valuable data without incurring additional costs.
References
https://gupta-aditya333.medium.com/how-to-backup-mongodb-automatically-3b953cd2c8c8
https://www.mongodb.com/docs/manual/core/backups/
https://www.mongodb.com/docs/cloud-manager/tutorial/automate-backup-restoration-with-api/
https://nimamovic9.medium.com/automate-mongodb-backup-and-restore-using-aws-s3-github-actions-and-node-js-e4b608b52ba