Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mongodump Shell Script for Cronjob
#!/bin/bash
MONGO_DATABASE="your_db_name"
APP_NAME="your_app_name"
MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="/home/username/backups/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
# mongo admin --eval "printjson(db.fsyncLock())"
# $MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
$MONGODUMP_PATH -d $MONGO_DATABASE
# mongo admin --eval "printjson(db.fsyncUnlock())"
mkdir -p $BACKUPS_DIR
mv dump $BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
rm -rf $BACKUP_NAME
@sheharyarn

This comment has been minimized.

Copy link
Owner Author

sheharyarn commented Jun 28, 2014

Make it executable:

chmod +x mongo_backup.sh

Schedule a Cronjob:

sudo su
crontab -e

Enter this in a new line:

# Everyday at 1 a.m.
00 01 * * * /bin/bash /home/username/scripts/mongo_backup.sh

Also See: mongo-sync

mongo-sync demo gif

@brobertsaz

This comment has been minimized.

Copy link

brobertsaz commented Apr 30, 2015

I used part of this to do it in a cron job:

mongodump --host 0.0.0.0 -d mydb --username mayusername --password mypassword --out /var/dbbackups/backup_$(date +%Y%m%d) && cd /var/dbbackups && tar -zcf backup_$(date +%Y%m%d).tar.gz backup_$(date +%Y%m%d)/mydb

@pascalsz

This comment has been minimized.

Copy link

pascalsz commented Nov 4, 2015

Here is also a nice documentation to ensure, that the script will run only once simultaniusly:

http://wiki.bash-hackers.org/howto/mutex

@christabor

This comment has been minimized.

Copy link

christabor commented Aug 31, 2016

This is handy! I also added an option in the output to save based on hostname, should you want to organize it in a distributed fashion.

BACKUP_NAME="$APP_NAME-$HOSTNAME-$TIMESTAMP"
@Pakirava-Datsuma

This comment has been minimized.

Copy link

Pakirava-Datsuma commented Apr 17, 2017

according to this post of mongo DB team we can use new --archive=<name> and --gzip options to simpify script since mongo v.3.2.x

ARCHIVE_PATH="$BACKUPS_DIR/$BACKUP_NAME.gz"
$MONGODUMP_PATH --db $MONGO_DATABASE --archive=$ARCHIVE_PATH --gzip
@bozBash

This comment has been minimized.

Copy link

bozBash commented Jul 27, 2017

How can you make it delete dumps after 7 days ?

@dejakob

This comment has been minimized.

Copy link

dejakob commented Aug 28, 2017

Thanks man!

@vladwa

This comment has been minimized.

Copy link

vladwa commented Jan 6, 2018

Thanks, Mate!!

@bradburycode

This comment has been minimized.

Copy link

bradburycode commented Aug 9, 2018

Thanks for this... really handy

@rauhmaru

This comment has been minimized.

Copy link

rauhmaru commented Apr 17, 2019

Great! Thanks

@ganeshrrhce

This comment has been minimized.

Copy link

ganeshrrhce commented Jul 30, 2019

When I am running the script getting error "Failed to authenticate admin@trzprod with mechanism MONGODB-CR: AuthenticationFailed key mismatch"

Please help me out this

@yueyericardo

This comment has been minimized.

Copy link

yueyericardo commented Jul 30, 2019

How can you make it delete dumps after 7 days ?

The command below will only save for newest 7 files.

ls --color=no -t | sed -e '1,7d' | xargs -d '\n' rm
@ganeshrrhce

This comment has been minimized.

Copy link

ganeshrrhce commented Jul 31, 2019

That great @yueyericardo, How we can save 15-day dumps in S3, older should delete automatically.

please let me know and I want to add the lines to exiting the script

@yueyericardo

This comment has been minimized.

Copy link

yueyericardo commented Jul 31, 2019

That great @yueyericardo, How we can save 15-day dumps in S3, older should delete automatically.

please let me know and I want to add the lines to exiting the script

Hi, simply add this to the end of script.

# only keep the newest 15 backups
cd $BACKUPS_DIR
ls --color=no -t | sed -e '1,15d' | xargs -d '\n' rm
@arbermarleku

This comment has been minimized.

Copy link

arbermarleku commented Aug 1, 2019

please let me know and I want to add the lines to exiting the script

#!/bin/bash
 
MONGO_DATABASE="your_db_name"
APP_NAME="your_app_name"

MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="/home/username/backups/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
#=====================================================================
DAYSTORETAINBACKUP="15"
#=====================================================================
 
# mongo admin --eval "printjson(db.fsyncLock())"
# $MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
$MONGODUMP_PATH -d $MONGO_DATABASE
# mongo admin --eval "printjson(db.fsyncUnlock())"
 
mkdir -p $BACKUPS_DIR
mv dump $BACKUP_NAME
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
rm -rf $BACKUP_NAME`
#=====================================================================
find $BACKUPS_DIR -type f -mtime +$DAYSTORETAINBACKUP -exec rm {} +
echo "--------------------------------------------"
echo "Database backup complete!"
#=====================================================================

i added the lines for you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.