How to set up backups for MongoDB for free
Hello everybody! So I've recently did a bit of research trying to figure out the ways to back up my MongoDB's and found out that it costs outrageous $39/month at the official MongoDB service! I know, right? It's the cost of one of the middle-tier servers on Digital Ocean! My @voicybot runs on a $20/month server and it is used by over 7 000 000 people daily — and for almost twice as much they sell you backups.
Obviously, I would never pick this option. Instead, I took my old ThinkPad from a shelf with ElementaryOS on board (but you can set up backups with literally any Linux distro, heck, even Windows if you're adventurous) and decided to automate backups myself, for free.
I also had a 256 gb thumbstick drive laying around doing nothing so I thought why not — I'll use it for the backups! This much space should be sufficient enough.
General setup
- We will use MongoDB built-in mongodump tool to back up databases and mongorestore to restore the databases in case we need to roll back to one of the previous database states.
- In order to automate running the backup script hourly, we will use Linux built-in cronjob manager.
- After we finish the backup, we will make our machine notify us about backups being completed and about how much space is left on the thumbdrive holding the backups.
Yes, everything in this tutorial is built-in, no external proprietary software. Nice.
The backup script
I'll assume you are familiar with what bash is and how to run scripts, otherwise you wouldn't need MongoDB at all. You're a developer, right? If you aren't familiar with bash, cron, linux and Terminal, please, first duckduckgo them.
Let's create a simple script at ~/backup.sh
:
#!/bin/bash
# 1
timestamp=$(date +%s)
# 2
mongodump --uri="mongodb://username:password@1.1.1.1:27017/database" -o "/media/user/thumbdrive/backups/service/$timestamp" &&
# 3
curl "https://api.telegram.org/bot123456789:1234567890/sendMessage?chat_id=76104711&text=🎉 Service backed up (disk usage $(df -hP /media/user/thumbdrive | awk '{print $5}' |tail -1|sed 's/%$//g')%)"
First line just tells whatever IDE we are using what language this file is written in. Helpful when you open a file in VSCode, for instance.
- Getting the timestamp variable
We will be saving our backups onto the thumbdrive in the folders like /media/user/thumbdrive/backups/service/1602463694
to keep trak of the backup timestamps. So we are fetching the current timestamp (1602463694
) here.
2. Dumping the data from the database
Make sure to modify your URI and the output folder. URI should be for a user with at least the read
role. Output folder can be anywhere, in my case it was on a thumbdrive called thumbdrive
under the user called user
. I know, I'm an imaginative one.
3. Sending the confirmation to Telegram
Just change your bot token here and the path to your thumbdrive. I'm also sending the amount of space left on my thumbdrive to keep track of when to purge the old backups. You can also automate purging old backups but I personally don't like autamated data deletion.
Now go on, make the file executable with sudo chmod +x ~/backup.sh
and execute it with ~/backup.sh
! You should get everything backed up and a new message in the Telegram.
Automate running the script
There are plenty of tutorials on the Internet on how to use cronjobs. Probably there is one for your distro as well! All in all, cron allows you to schedule running cronjobs repeatedly after a set time interval. In my case, I just back up hourly.
The syntax of time intervals in cron is a bit fancy, but you can use crontab generators like this one to simplify the process.
You can access the list of your cronjobs by running the command crontab -e
. I appended this line to the file that showed up:
0 * * * * /home/user/backup.sh
And this is it. Now my backup.sh
script will run every hour. And even better, if anything goes wrong, I'll stop receiving the hourly backup messages and hopefuly notice the issue!
That's all, folks. Stop overpaying for simple things that can be done with a few simple commands and built-in tools. Work smarter, not harder!