Skip to content

Instantly share code, notes, and snippets.

@gudata
Forked from sheharyarn/mongo_backup.sh
Created September 25, 2019 08:51
Show Gist options
  • Save gudata/0a3b167b887a0f6d6074cb518ca5e195 to your computer and use it in GitHub Desktop.
Save gudata/0a3b167b887a0f6d6074cb518ca5e195 to your computer and use it in GitHub Desktop.
Mongodump Shell Script for Cronjob
#!/bin/bash
MONGO_DATABASE="your_db_name"
APP_NAME="mongo18"
MONGO_HOST="172.19.0.2"
MONGO_PORT="27017"
TIMESTAMP=`date +%F-%H%M`
MONGODUMP_PATH="/usr/bin/mongodump"
BACKUPS_DIR="/opt/mongo-backup/backups/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP"
#=====================================================================
DAYSTORETAINBACKUP="15"
#=====================================================================
# mongo admin --eval "printjson(db.fsyncLock())"
$MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -o $APP_NAME
# $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 $APP_NAME $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!"
#=====================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment