Follow me

Saturday, June 22, 2013

Backup Script For Mongo Database

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]
0 0 * * * /PATH/TO/SCRIPT/mongo-backup.sh daily
For weekly backup on Sunday (Sunday = 0, Monday = 1......) at midnight make entry of below cron expression [update the script location path]
0 0 * * 0 /PATH/TO/SCRIPT/mongo-backup.sh weekly
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




Ajax Call To Populate The Select List Data And Create the Dynamic Select List Using JQuery

Here I am going to mention the steps for how to use Ajax to populate the data for the html Select list and how we can add the select option to html select element.

So below are the steps

Step 1) Create the spring action which is going to serve the Ajax request (I have used spring 3.0 controller)

@RequestMapping(value = "/countryList", method = RequestMethod.GET)
                @ResponseBody
                public HashMap<String, String> countryList(HttpServletResponse response) {

                                HashMap<String, String> map = new HashMap<String, String>();

                                map.put("India ", "India");
                                map.put("AUS", "AUS");
                                map.put("US", "US");

                                return map;
                }

Step 2) Now next step is to set place holder for the list in html form

 <select id="countryId"   name=" countryId"></select> 

Step 3) Now the main part, JavaScript method to call spring action using Ajax

For JQuery we need the below JQuery library added in our JSP or HTML

<script  type="text/javascript" src="resources/scripts/jquery-1.9.1.js" > </script>
<script type="text/javascript"  src="resources/scripts/jquery-ui.js" > </script>

Here we are calling the JavaScript method on load of html page

<body onload=" loadCountry ()">


Below is the JQuery method to call the spring action using JQuery Ajax

Here we are calling /projectname/countryList URL to load the select list data as a response which will be in the form of JSON object.  Since we are using @Responsebody this will do the JSON conversion for us.
Now here we use Jquery new Option() method to add option to  select list.

var dropdown = $('#countryId');
var defOption = new Option("ALL","ALL");

Using $.each()  we can iterate the collection and add the all option to the select list.
   We can select the default option using jquery new Option(label,value, true, true).
    Below is the complete method
<<script type="text/javascript">
function loadCountry(){
$.ajax({
type: "get",
url: "/projectname/countryList ",
cache: false,
dataType: "json",
success: function(response){
var dropdown = $('#countryId');
var defOption = new Option("ALL","ALL"); 
$(defOption).html("ALL");
$('#countryId').append(defOption)
$.each(response, function(index) {
var selected = cument.getElementById("countryIdSelect").value;
   if (selected == response[index]) {
var option = new Option(response[index],response[index], true, true); 
$(option).html(response[index]);
$('#countryId').append(option)
} else {
var option = new Option(response[index],response[index]);
$(option).html(response[index]);
$('#countryId').append(option)
}
});

},
error : function(response) {
alert('Error while request..');

}
});
}
</script>