Description
This script use for taking the mongo database backup using --oplog option, this script create The backup folder with current timestamp, also delete the backup folder older than 2 days for daily backup and delete the folder older than 30 days for weekly backup, This script will Run as part of cron job midnight everyday and once in week. This script find out the secondary server for the backup configured in mongo.properties. If no secondary found then it will use The primary for default backup. The default behavior is daily backup if no input provide.
Configure mongo.properties
In mongo.properties we need to set the below properties
· USERNAME [Backup user's username]
· PASSWORD [Password]
· DEFAULT_HOST_PORT [Default server set for backup]
· DEFAULT_BACKUP_DESTINATION [ Path to backup root folder ]
· MONGO_INSTALL_BIN_DIR [mongo installation bin directory path ]
· HOSTNAME [List of all servers(get it by execute rs.status() command on mongo client) separated with ; ]
USERNAME=username
PASSWORD=password
DEFAULT_HOST_PORT=hotsname:27017
DEFAULT_BACKUP_DESTINATION=backup/dir/name
MONGO_INSTALL_BIN_DIR=mongo/install/dir/bin
HOSTNAME="host1:27017;host2:27017;host3:27017"
Configure mongo-backup.sh
Open the mongo-backup.sh and set the path for the mongo.properties
for e.g. . /path/to/properties/mongo.properties
Setting the CRON job
To execute the script as cron job, run below command on shell prompt
#crontab -e
This will open the cron job file. For daily midnight backup make entry of below cron expression [update the script location path]
For weekly backup on Sunday (Sunday = 0, Monday = 1......) at midnight make entry of below cron expression [update the script location path]
The user who is going to run this script should have the permission [ read /write] to mongo.properties file and backup folder and script.
Below is the script
#!/bin/bash
. /usr/local/mongo.properties
# version:- 2.0
# Author:- Chandrashekhar Dehankar
# DATABASE HOST-PORT DEFAULT SETTING
HOST_PORT=$DEFAULT_HOST_PORT
#NO OF DAY FOR DELETE OLD BACKUP [DEFAULT VALUE]
NO_OF_DAY=2
CHECK_BACKUP_TYPE=$1
#METHOD TO TAKE MONGO BACKUP
function _do_backup {
# CREATING FOLDER WITH CURRENT TIME STAMP
BACKUP_CURRENT_DATE=`date +%Y%m%d_%H-%M-%S`
BACKUP_CURRENT_DATE="$(echo ${BACKUP_CURRENT_DATE} | tr ' ' '0')"
UNIQ_DIR=""
if [ "$CHECK_BACKUP_TYPE" == "daily" ] || [ "$CHECK_BACKUP_TYPE" == "" ]; then
BACKUP_FOLDER=DAILY_BACKUP/
BACKUP_DESTINATION=$DEFAULT_BACKUP_DESTINATION$BACKUP_FOLDER
UNIQ_DIR=$BACKUP_DESTINATION$BACKUP_CURRENT_DATE
elif [ "$CHECK_BACKUP_TYPE" == "weekly" ]; then
BACKUP_FOLDER=WEEKLY_BACKUP/
BACKUP_DESTINATION=$DEFAULT_BACKUP_DESTINATION$BACKUP_FOLDER
UNIQ_DIR=$BACKUP_DESTINATION$BACKUP_CURRENT_DATE
fi
mkdir -p $UNIQ_DIR
#FIND SECONDARY SERVER
DATABASE_HOST_SERVERS=$HOSTNAMES
DATABASE_HOST_SERVERS=${DATABASE_HOST_SERVERS//;/$'\n'}
for host in $DATABASE_HOST_SERVERS
do
IS_SECONDAY=$(mongo $host/admin -u $USERNAME -p $PASSWORD --eval "printjson(db.isMaster().secondary)" )
#FILTER THE RESULT
IS_SECONDAY=`echo $IS_SECONDAY | grep -o '[^ ]*$'`
if [ "$IS_SECONDAY" == "true" ]; then
HOST_PORT=$host
break
fi
done
#execute mongo dump command
#$MONGO_INSTALLATION_BIN --host $HOST_PORT -u $USERNAME -p $PASSWORD --oplog --out $UNIQ_DIR
$MONGO_INSTALL_BIN_DIR/mongodump --host $HOST_PORT -u $USERNAME -p $PASSWORD --oplog --out $UNIQ_DIR
}
# THIS METHOD WILL DELETE THE FOLDER OLDER THAN 2 DAY
function _do_delete_old_backup {
if [ "$CHECK_BACKUP_TYPE" == "daily" ] || [ "$CHECK_BACKUP_TYPE" == "" ]; then
NO_OF_DAY=2
echo "deleting daily older backup !!!!!!!!!!!!!!!!!!!!!!"
else
NO_OF_DAY=30
echo "deleting weekly older backup !!!!!!!!!!!!!!!!!!!!!!"
fi
find $BACKUP_DESTINATION -type d -mtime +$NO_OF_DAY -exec rm -rf {} \;
}
## take the backup
_do_backup
## delete the old folder
_do_delete_old_backup