Cron Job for Backing up MongoDB
Why this?
I had a very simple backup script for MongoDB till I realized that it got so huge that it started breaking in between due to new documents being written while it was still backing up.
So I implemented mongo’s db.fsyncLock
and db.fsyncUnlock
to Avoid writes while the backup process is going on.
Here’s the script, which works for me on mongo v3.6.3
. You would want to change the value of the variables according to your setup.
and make it executable : chmod +x backupMongo.sh
You can run the script like this : ./backupMongo.sh
or you can set up a cron job (below).
What this does
- Created a temporary directory
- Locks writes on the database
- Creates a mongo dump in that temporary directory
- Unlocks writes on the database
- Makes an archive of the temporary directory into the specified directory
- Deletes the temporary dump
What else can be done / Changes
- To keep it safe, I have attached a volume to
~/mongo_backups
. This can also be S3 / GCP Bucket. Just make sure the temporary directory is not in that volume. - You can change the directories and timestamps according to your setup.
- Make sure the disk you’re dumping to (temporary)is fast enough because if it’s not, other applications cannot write to the database for this whole time.
- Make sure for every lock, there’s an unlock command. If you lock twice, you’ve to unlock twice.
Make this into a Cron Job
- Make sure it’s executable :
chmod +x backupMongo.sh
- Schedule the job:
sudo crontab -e
- Add this to the file:
00 01 * * * /bin/bash /home/<YOUR_USERNAME>/scripts/backupMongo.sh
You can check the Cron Expression here : https://cronexpressiondescriptor.azurewebsites.net/
(Or tweak and make your own).