Writing an automated Amazon S3 backup script for your MongoDB database using bash and cron in Ubuntu 16.04
Published: 2019-10-01 19:25:09

<p>While as easy as it may sound to write a simple backup script, there are a few steps that are involved in the process. And might be a tad bit unclear to those who have no prior experience of it. While there may be various methods of taking automated backups of your mongo db, this article aims to outline one such method which I personally prefer myself.
</p>

<h2>Prerequisites</h2>

<ul><li>A <a href="https://hevodata.com/blog/install-mongodb-on-ubuntu/">properly setup MongoDB server</a> on top of Ubuntu. The method outlined in this tutorial was <b>tested on Ubuntu 16.04 LTS</b> though it may work with other versions as well.</li>
<li>In most cases , <b>cron</b> comes bundled with the most Linux distros. But if not, it should be pretty straightforward as running `sudo apt-get install cron` in a terminal window.</li>
<li>In order to backup to Amazon S3 you need to have <b>aws cli</b> installed and configured. Follow the <a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html">installation</a> and <a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html">configuration</a> instructions in the AWS documentation to do so.</li></ul>

<p>
Now that we have everything in place, we can start writing the script!</p><br/>

<h2>Writing the script</h2>

<p>
First things first, you need to <b>setup database user credentials and other necessary variables</b>. For now let’s provide DB credentials as variables for simplicity’s sake. Alternatively you could look into other ways to not expose credentials in your script. Also, it is advised to setup a separate database user with only backup privileges so that their is a minimum security risk involved in any case.</p>

<p>
Go ahead and add the credentials and other necessary variables to your script
</p>

```bash
#!/bin/sh

HOST=127.0.0.1 #change to whatever your host IP is
BUCKET=yourbucket #s3 bucket name
DEST=$HOME/tmp #backup directoryDBUSER=yourusername #the mongo username
DBPWD=yourpassword #the mongo users password
DBNAME=yourdbname #the name of the DB to be backup
TIME=`date +%d-%m-%Y-%T` #the time of the backup
TAR=$DEST/../$DBNAME-$TIME.tar.gz #name and dir of backup archive
set -e #this stops execution of script if anything fails
```

<p>
Most of the above code is commented and is self-explanatory. So let’s go ahead and continue with writing the rest of the script.
</p>

<p>
The next step is to write the `mongodump` command, this may differ depending on your mongo db configuration. If you followed the tutorial linked at the beginning of this article on <a href="https://hevodata.com/blog/install-mongodb-on-ubuntu/">installing and configuring mongodb</a>, it should look something like the code below (The echo statements are for logging purposes which is needed later. Logs are pretty crucial specially in a production environment, if anything fails these come in handy to find out where things went wrong!)
</p>

```bash
echo "Backing up $HOST/$DBNAME to s3://$BUCKET/ on $TIME";
mongodump -h $HOST -d $DBNAME -u $DBUSER -p $DBPWD -o $DEST --authenticationDatabase your_auth_db_name
echo "Backup performed successfully";
```

<p>Next step is to archive the dump folder generated by `mongodump`. We will use the TAR variable we setup previously for this.
</p>

```bash
echo "Archiving the backup"tar -zcvf $TAR -C $DEST .echo "Done archiving"
```

<p>
Now we upload this archive to our s3 bucket. “a/path/you/desire/” is a relative path of your choice within your s3 bucket. Easy as cake!
</p>

```bash
echo "Uploading to s3 bucket" $BUCKETaws s3 cp $TAR s3://$BUCKET/a/path/you/desire/
```

<p>
If all went well the backup should now appear on your s3 bucket.<br/>
<b>NOTE: Make sure to download and restore the backup on your local database to make sure all your data are there once restored
</b></p>

<p>
<b>BUT</b> we still have a few more steps to get through..
We first need to cleanup our backup directory and the following lines handles exactly that!
</p>

```bash
echo "Cleaning up temp files"
rm -f $TAR    # Remove tar file
rm -rf $DEST  # Remove backup directory
echo "Done cleaning up!"
```

<p>
Now we’re done writing the script. Let’s log it
</p>

```bash
echo "Backup available at https://s3.amazonaws.com/$BUCKET/a/path/you/desire/$DBNAME-$TIME.tar.gz"
```
<p></p><br/>
<h2>Automating the script</h2>

<p>
Now let’s automate this script using cron and we’re done!<br/>
Firstly, open a crontab using the below command in the terminal (Select a preferred text editor of your choice if prompted, I usually use nano).
</p>

```bash
crontab -e
```

<p>
Now at the end of the file add the following line. It will set our cron job to run everyday at 01:00 a.m. based on System date/time. You can set it to your preference. A good reference is <a href="https://crontab.guru/#0_1_*_*_*">crontab.guru</a> if you’re not familiar with the notation! And in case you’re wondering, the “>> backup_log” at the end will append all your <b>echo</b> output to a file named backup_log in the same directory as the script.
</p>

```cron
0 1 * * * path/to/backup/script.sh >> backup_log
```

<p>
It is advised to initially set it to run around every 10 minutes in order to test run whether your script works flawlessly with cron. If your cron job fails in any case, you’d have wasted your time following this tutorial and writing that beautiful work of art(the backup script)!
</p>

<p>
So that’s it. If you’ve clearly followed the steps, you should have a fine working automated backup cycle up and running now for your mongo db. So go ahead and sip a well-deserved cup of coffee while your script does all the work for you.
</p>

<p><b>
Cheers!</b></p>