Skip to main content
Automate MongoDB Atlas Backup Free

Automate MongoDB Atlas Backup Free

· 4 min read

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
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

Setting Up MongoDB Atlas​

  1. Log in to your MongoDB Atlas account
  2. Navigate to your cluster
  3. Click "Connect" and choose "Connect your application"
  4. Copy the connection string
  5. 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:

  1. First navigate to the folder where you want to create your script.
  2. Then, lets create a script named mongodb_backup.sh. You can use touch mongodb_backup.sh to create the file.
  3. Then nano mongodb_backup.sh to open the file. Or if you have vim, vim mongodb_backup.sh to open the file.
  4. 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:

  1. Save the script as mongodb_backup.sh
    • If you are using nano, you can save the file by pressing CTRL + O and then CTRL + X
    • If you are using vim, you can save the file by pressing :wq
  2. Make it executable:
chmod +x mongodb_backup.sh
  1. Open your crontab:
crontab -e

If it shows error specify your editor:

EDITOR=nano crontab -e

use EDITOR=vim if you are using vim.

note

If you don't have crontab/cronie, you can install it using:

#ArchLinux
sudo pacman -S cronie

#Ubuntu
sudo apt-get install cronie
  1. 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:

  1. 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
  2. Space Issues:

    • Monitor backup sizes
    • Adjust retention period
    • Set up disk space alerts
  3. 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