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.
#!/bin/sh
TIMESTAMP=`date +%m-%d-%y--%H%M`
MONGO_HOST="127.0.0.1"
MONGO_PORT=27017
MONGO_USER="root"
MONGO_PASS="root"
MONGO_DB="mydatabase"
USERNAME="wirddin"
DEST=/home/$USERNAME/mongo_backups_temp/$TIMESTAMP
mkdir $DEST
mongo mongodb://$MONGO_USER:$MONGO_PASS@$MONGO_HOST:$MONGO_PORT/admin --authenticationDatabase admin --eval "printjson(db.fsyncLock())"
mongodump -h $MONGO_HOST -d $MONGO_DB -u $MONGO_USER -p $MONGO_PASS -o $DEST
mongo mongodb://$MONGO_USER:$MONGO_PASS@$MONGO_HOST:$MONGO_PORT/admin --eval "printjson(db.fsyncUnlock())"
tar -zcvf /home/$USERNAME/mongo_backups/$TIMESTAMP.tgz $DEST
rm -rf $DESTand 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).