The Ultimate MongoDB Database Backup Script

SimpleBackups founder

Laurent Lemaire

Co-founder, SimpleBackups

Last update on December 22, 2023
Originally posted on June 8, 2021

This article is part of “The Ultimate Backup Script” series we are creating to provide you with database backup scripts that not only allow you to create database backups, but also upload the backup dumps to Amazon S3 and automate the process daily.

Table of Contents

MongoDB Backup Illustration

MongoDB Backup process - What are we going to do?

Our goal in this tutorial is to back up your MongoDB database and store it remotely, all on autopilot.
In this example we'll focus on storing the backup on Amazon S3, but you can use any cloud storage provider you want as long as it's S3 compatible.

🧑‍💻 So here are the steps we'll follow:

  1. Create your MongoDB backup and store it locally
  2. Connect to your Amazon S3 account and upload the backup
  3. Automate the process using a shell script and Cron

ℹ️ Key Points to Understand Before We Begin:

Why Choose Amazon S3?
In this tutorial, we've selected Amazon S3 for its widespread use and reliability.
If you prefer a different cloud storage provider, feel free to make that choice. The steps will remain largely similar as long as the chosen provider is compatible with S3.

What is Cron?
Cron is a time-based job scheduling software utility, supporting Unix-based operating systems. Developers utilize Cron to schedule tasks, like executing commands or shell scripts, at specific intervals. These intervals can be set to daily, weekly, or any other frequency according to your needs.

Understanding Chmod
Chmod, short for ‘change mode’, is a command used to set file handling rules. Through the “chmod” system call, an administrator can alter access permissions of file system objects.

How to back up your Mongo database

We'll be using mongodump to create your backup.

In this tutorial we'll keep it simple, but we've also written a detailed guide on mongodump, which I'm sure will be useful to you.

Here is the command to create a backup of your MongoDB database:

mongodump -h localhost:27017 -d my_db_name -o my_backup_dir

Because we want to automate the process, we'll create a shell script db_backup.sh that will run this command for us.

cd ~
mkdir scripts
cd scripts
nano db_backup.sh

Copy and paste the script below to it

#!/bin/bash
DIR=`date +%d-%m-%y`
DEST=~/db_backups/$DIR
mkdir $DEST
mongodump -h localhost:27017 -d my_db_name -o $DEST

Now chmod the script to allow it to for execution

chmod +x ~/scripts/db_backup.sh

Storing your Mongo backup on a remote storage

Now that we have a script that creates a backup of our MongoDB database, we need to upload it to Amazon S3.
Note that you can use any cloud storage provider you want as long as it's S3 compatible.
And if you're looking for a simple way to automate this process, you can use SimpleBackups to do it for you.

First, let's make sure we have the proper tools installed.

Configure the AWS CLI

We'll use the AWS CLI to sync the backup files with Amazon S3.

Before installing the AWS CLI you need to install python-pi. Type the following commands:

apt-get update
apt-get -y install python-pip
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"

Install the AWS CLI

Type the following command:

pip install awscli

Set up AWS key & Secret

Configuration and credential file settings

cd ~
mkdir .aws
nano ~/.aws/config

Paste in key_id and secret_access_key as shown below

[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

Syncing the backup with Amazon S3

Now that AWS CLI is installed and configured, we can sync the backup with Amazon S3.

For this, we'll create a new script db_sync.sh that will sync the backup directory with Amazon S3.
Copy and paste the script below to it

#!/bin/bash
/usr/local/bin/aws s3 sync ~/db_backups s3://my-bucket-name

Now chmod the script to allow it for execution:

chmod +x ~/scripts/db_sync.sh

Schedule your MongoDB backup script

At this stage, we have a script that creates a backup of our MongoDB database and another script that syncs the backup with Amazon S3.

Now it's time to automate the process using Cron.

crontab -e

Paste the below commands at the bottom to automate the process

0 0 * * * ~/scripts/db_backup.sh # take a backup every midnight
0 2 * * * ~/scripts/db_sync.sh # upload the backup at 2am

This way the backup script will run and also sync with Amazon S3 daily.

Conclusion

Here you go! You now have a shell script that can be executed to backup your MongoDB database and sync it with your specified Amazon S3 bucket.


Have you tried simplebackups.com yet?

SimpleBackups will save you a lot of time setting up scripts and ensuring they run without problems. It will alert you when things go wrong, and allows you store your backups on many cloud storage services like Google, DigitalOcean, Wasabi, Dropbox, and more…



Back to blog

Stop worrying about your backups.
Focus on building amazing things!

Free 7-day trial. No credit card required.

Have a question? Need help getting started?
Get in touch via chat or at [email protected]

Customer support with experts
Security & compliance
Service that you'll love using