#!/bin/sh

##########################################################################################################################################
#                                                                                                                                        #
#       AUTHER  : MAHENDRA B. PATIL                                                                                                      #
#       DATE    : 03-JULY-2010                                                                                                           #
#       PURPOSE : To synchronize the content from Source Location to Destination Location						 #
#       NOTE    : Dont forget to add / at the end of Source Location defined								 #
#		  if / is not defined at the end of Source Location then it copies Last Source Location in Destination path		 #
#                                                                                                                                        #
##########################################################################################################################################

#############################################ALL Required Functions defined Here START#####################################
ALERT_ROOT_PATH="/opt/anm/scripts/alert/"
function print_header_start()
{
echo -e "\n\n####################STATIC CONTENT SYNCRONIZATION STARTED ON DATE - $(date +%d-%b-%Y-%H-%M-%S)###########################################\n\n"
}

function print_header_end()
{
echo -e "\n\n####################STATIC CONTENT SYNCRONIZATION COMPLETED ON DATE - $(date +%d-%b-%Y-%H-%M-%S)###########################################\n\n"
}

function inform_error()
{
  echo -e "${ERROR_MSG}"

   ALERT_EMAIL_MESSAGE="Error while ${ALERT_EMAIl_SUBJECT} ON  $(date +%Y-%m-%d-%H-%M-%S) <BR/> Error : ${ERROR_MSG}<BR>Please check the logs for further analysis."
   ALERT_EMAIL_MESSAGE="$ALERT_EMAIL_MESSAGE <BR> <i>Note : If any issues or suggetions,"
   ALERT_EMAIL_MESSAGE="$ALERT_EMAIL_MESSAGE then inform us at <a href='mailto:mahendra.patil@naaptol.com'>mahendra.patil@naaptol.com</a></i> "


   ##################################################SEND EMAIL OF STATUS START######################

   if [ ! -d ${ALERT_ROOT_PATH} -o ! -f ${ALERT_ROOT_PATH}/alert.conf  ]; then
        echo -e "\n\nalert configuration not exist so not able to triggeer mail\n"
   else
        . ${ALERT_ROOT_PATH}/alert.conf
        ${ALERT_ROOT_PATH}/sendEmail -s "${MAIL_SERVER}"  -f "${ALERT_EMAIL_FROM}" -t "${ALERT_SUPPORT_EMAIL_TO}" -u "ERROR:${ALERT_EMAIl_SUBJECT} ON $(date +%Y-%m-%d-%H-%M-%S)"  -o  message-content-type=html  -m "${ALERT_EMAIL_MESSAGE}"
   fi

   ##################################################SEND EMAIL OF STATUS END######################

   print_header_end

  exit 0
}

#############################################ALL Required Functions defined Here END#####################################




 ALERT_EMAIL_TO="naaptoltech@gmail.com;monitoring.alert@naaptol.com;"
 ALERT_EMAIl_SUBJECT="DAILY STATIC CONTENT SYNC OF 10.1.51.20 on DR "
 ALERT_EMAIL_FROM="tech@naaptol.com"
 ALERT_SUPPORT_EMAIL_TO="naaptoltech@gmail.com;monitoring.alert@naaptol.com;tech@naaptol.com;"


 SOURCE_LOCATION="root@10.1.51.20:/usr/local/csp/"
 #DESTINATION_LOCATION="/usr/local/csp/"
 DESTINATION_LOCATION="/opt/backup_repository_content_idc/10.1.51.20-csp/csp/"
 KEY_PATH="/opt/anm/scripts/static_content_sync_idc/naaptol_app_server_id_dsa"




print_header_start

#Changing Directory to Current Shell Directory
CURRENT_SHELL_HOME="$(dirname $0)"
cd "${CURRENT_SHELL_HOME}"

 echo -e "SOURCE_LOCATION=$SOURCE_LOCATION\nDESTINATION_LOCATION=$DESTINATION_LOCATION\n"

 /usr/bin/rsync -arvz --delete --force -e "ssh -i $KEY_PATH" $SOURCE_LOCATION $DESTINATION_LOCATION

 status="$?"
 
 if [ "${status}" != "0" ]; then
   ERROR_MSG="ERROR WHILE STATIC CONTENT SYNC"
   IS_ERROR="Y"
   inform_error
 fi

 ALERT_EMAIL_MESSAGE="${ALERT_EMAIl_SUBJECT} COMPLETED SUCESSFULLY ON $(date +%Y-%m-%d-%H-%M-%S)"
 ALERT_EMAIL_MESSAGE="$ALERT_EMAIL_MESSAGE <BR> <i>Note : If any issues or suggetions,"
 ALERT_EMAIL_MESSAGE="$ALERT_EMAIL_MESSAGE then inform us at <a href='mailto:mahendra.patil@naaptol.com'>mahendra.patil@naaptol.com</a></i> "


 ##################################################SEND EMAIL OF STATUS START######################

   if [ ! -d ${ALERT_ROOT_PATH} -o ! -f ${ALERT_ROOT_PATH}/alert.conf  ]; then
        echo -e "\n\nalert configuration not exist so not able to triggeer mail\n"
   else
        . ${ALERT_ROOT_PATH}/alert.conf
        ${ALERT_ROOT_PATH}/sendEmail -s "${MAIL_SERVER}"  -f "${ALERT_EMAIL_FROM}" -t "${ALERT_EMAIL_TO}" -u "${ALERT_EMAIl_SUBJECT} ON $(date +%Y-%m-%d-%H-%M-%S)"  -o  message-content-type=html  -m "${ALERT_EMAIL_MESSAGE}"  

   fi

 ##################################################SEND EMAIL OF STATUS END######################



print_header_end


