Abhishek Biswal
Apr 23, 2018 • 2 min read

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 $DEST

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

What else can be done / Changes

Make this into a Cron Job

  1. Make sure it’s executable : chmod +x backupMongo.sh
  2. Schedule the job: sudo crontab -e
  3. 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).

Post by: Abhishek Biswal