How to enable automated backup of MongoDB hosted on Atlas?

Vivek Navadia
3 min readJul 5, 2023

--

MongoDB Automated Backups

Here’s a step-by-step guide on how to backup MongoDB hosted on MongoDB Atlas M0 and store the dump in an EC2 instance

Step 1: Set up an EC2 Instance

  • Launch an EC2 instance on AWS with the desired operating system.
  • Ensure that the EC2 instance has internet connectivity and the necessary permissions to interact with MongoDB Atlas.

Step 2: Install MongoDB and mongodump on the EC2 Instance

  • Connect to the EC2 instance using SSH.
  • Install MongoDB by following the official MongoDB documentation specific to your EC2 instance’s operating system.
  • Verify that mongodump is installed by running the command mongodump --version.

Step 3: Obtain Connection Details from MongoDB Atlas

  • Log in to your MongoDB Atlas account.
  • Go to the “Clusters” page and select the desired M0 cluster.
  • Click on the “CONNECT” button and choose “Connect your application.”
  • Note down the connection string and replace <PASSWORD> with the actual password for your MongoDB user.

Step 4: Create a Backup Script on the EC2 Instance

  • Create a shell script (e.g., mongodb_backup.sh) on the EC2 instance with the following contents:
#!/bin/bash

# Set the date and time variables
DATE=$(date +"%Y%m%d")
TIME=$(date +"%H%M%S")

# Set the backup directory path
BACKUP_DIR="/path/to/backup/directory"

# Create the backup directory if it doesn't exist
mkdir -p $BACKUP_DIR

# Run mongodump to create the backup
mongodump --uri="mongodb+srv://<USERNAME>:<PASSWORD>@<ATLAS_CLUSTER_URI>/?retryWrites=true&w=majority" --gzip --archive=$BACKUP_DIR/backup_${DATE}_${TIME}.archive
  • Replace <USERNAME>, <PASSWORD>, and <ATLAS_CLUSTER_URI> with your MongoDB Atlas cluster credentials and connection URI.
  • Replace /path/to/backup/directory with the desired directory path on the EC2 instance to store the backup files.

Step 5: Schedule the Backup Job

  • Use a scheduling tool like cron to schedule the execution of the backup script.
  • Run crontab -e to edit the crontab file.
  • Add the following line to schedule the backup job to run every midnight:
0 0 * * * /bin/bash /path/to/mongodb_backup.sh
  • Save the file and exit.

Step 6: Verify the Backup

  • Wait for the scheduled time, and the script will run automatically.
  • Check the specified backup directory on the EC2 instance to ensure the backup file is created.

Step 7: Install and Configure AWS CLI

  • Connect to your EC2 instance using SSH.
  • Install the AWS Command Line Interface (CLI) by following the AWS CLI installation documentation specific to your EC2 instance’s operating system.
  • Once installed, configure the AWS CLI by running the following command and providing your AWS Access Key ID, Secret Access Key, default region, and output format:
aws configure

Step 8: Upload the File to S3

  • Use the aws s3 cp command to upload the file to your S3 bucket. The basic syntax is as follows:
aws s3 cp /path/to/local/file s3://mongo-db-backups/filename-$(date +\%Y\%m\%d_\%H\%M\%S).extension
  • Replace /path/to/local/file with the actual path to the file on your EC2 instance. Replace filename with the desired name for the file. Replace extension with the appropriate file extension. The $(date +\%Y\%m\%d_\%H\%M\%S) portion generates the date and time suffix in the format of YYYYMMDD_HHMMSS.
  • Example command to upload a file with a date and time suffix:
aws s3 cp /home/ec2-user/myfile.txt s3://mongo-db-backups/myfile-$(date +\%Y\%m\%d_\%H\%M\%S).txt
  • This uploads the myfile.txt file from the /home/ec2-user/ directory on the EC2 instance to the S3 bucket 'mongo-db-backups' with a file name that includes the date and time suffix.

Step 9: Verify the Upload

  • After running the upload command, you can check the S3 bucket ‘mongo-db-backups’ to confirm that the file with the specified name and date/time suffix is uploaded successfully.

By following these steps, you can schedule a daily backup of your MongoDB hosted on MongoDB Atlas M0 and store the dump in an EC2 instance. The backup process will run automatically every midnight, ensuring regular backups of your MongoDB data. later you can upload a file from your EC2 instance to an S3 bucket named ‘mongo-db-backups’ with a file name that includes a date and time suffix. The AWS CLI command takes care of generating the appropriate suffix based on the current date and time. Make sure to replace the placeholders with the actual values for your specific setup.

--

--

Vivek Navadia

Engineering Manager@digicorp, A Master of Computers and Bachelor of Physics by Education, loves cooking and photography. Gymaholic and Marathoner.