Skip to content

Instantly share code, notes, and snippets.

@sheharyarn
Last active August 28, 2024 08:18
Show Gist options
  • Save sheharyarn/0f04c1ba18462cddaaf5 to your computer and use it in GitHub Desktop.
Save sheharyarn/0f04c1ba18462cddaaf5 to your computer and use it in GitHub Desktop.
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
@vladwa
Copy link

vladwa commented Jan 6, 2018

Thanks, Mate!!

@bradburycode
Copy link

Thanks for this... really handy

@rauhmaru
Copy link

Great! Thanks

@ganeshrrhce
Copy link

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
Copy link

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
Copy link

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
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
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 👍

@abdullahsiddique
Copy link

abdullahsiddique commented Jan 9, 2020

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 👍

There is one little error make this line
rm -rf $BACKUP_NAME`
to this:
rm -rf $BACKUP_NAME

Otherwise it script broken on line 24.

However, thanks for it. 👍

@16g
Copy link

16g commented May 29, 2020

#!/bin/bash
mongo_dump_user="mongo_dump"
mongo_dump_password=""
mongo_dump_host="127.0.0.1"
mongo_dump_port="27017"
mongo_dump_db="db"
mongo_dump_storage="/storage/mongo/daily"


if [[ ! -e $mongo_dump_storage ]]
  then
    mkdir -p ${mongo_dump_storage}
  fi

echo "create backup" $(date +%Y-%m-%d)

mongodump --host ${mongo_dump_host} --db ${mongo_dump_db} --username ${mongo_dump_user} --password ${mongo_dump_password}  --gzip --archive=${mongo_dump_storage}/parser_$(date +%Y-%m-%d).gz

echo "remove old backup"
find ${mongo_dump_storage} -name "*.gz" -type f -mtime +30 -exec rm -f {} \;

@moschap
Copy link

moschap commented Jun 1, 2020

Hi @serverguru666, just wondering does mongoDB authenticationType have anything to do with this error that i get when i try mongodump using your connection format -
2020-05-31T23:53:02.921+0000 Failed: can't create session: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-256": (AuthenticationFailed) Authentication failed.

NB: The issue is that the command doesn't want me to add --password as plaintext, if i remove the --password flag it prompts me for password and works ok.

@Mathur777
Copy link

How to delete after 1 week please let me know..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment