#!/bin/sh


#############################################ALL Required Functions defined Here START#####################################

function print_header_start()
{
echo -e "\n\n####################SERVER REMOTE BACKUP SYNCRONIZATION STARTED ON DATE - $(date +%d-%b-%Y-%H-%M-%S)###########################################\n\n"
}

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


function inform_error()
{
  echo -e "\n${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_CONF}" -o ! -f "${ALERT_CONF}/alert.conf"  ]; then
        echo -e "\n\nalert configuration not exist so not able to triggeer mail\n"
   else
        . ${ALERT_CONF}/alert.conf
        ${ALERT_CONF}/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
}


#This Function deletes all data under BACKUP_BASIC_ROOT_PATH specified which is created before BACKUP_RETENTION_PERIOD
function clean_old_backup()
{
  if [ ! -z $BACKUP_RETENTION_PERIOD ]; then
        echo -e "\n\n---------------DELETING OLD BACKUP BEFORE $BACKUP_RETENTION_PERIOD DAYS START-----------------\n\n"
        if [ "${BACKUP_RETENTION_PERIOD}" -le "7" ]; then
          echo -e "BACKUP_RETENTION_PERIOD is too small so setting default to 7 days."
          BACKUP_RETENTION_PERIOD="7"
        fi


        if [ "${BACKUP_RETENTION_PERIOD}" -ge "7" ]; then
                echo "find  /backup_repository_idc/${SOURCE_SERVER_IP}/*  -type  d  -ctime +$BACKUP_RETENTION_PERIOD -exec  rm  -frv {} \;"
                find  /backup_repository_idc/${SOURCE_SERVER_IP}/*  -type  d  -ctime +$BACKUP_RETENTION_PERIOD -exec  rm  -frv {} \;
        fi
        echo -e "\n\n---------------DELETING OLD BACKUP BEFORE $BACKUP_RETENTION_PERIOD DAYS END-----------------\n\n"
  fi
}

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


SOURCE_SERVER_IP="$1"
SOURCE_SERVER_USER="backup"
SOURCE_SERVER_LOCATION="/opt/backup.repository/"
DESTINATION_ROOT_LOCATION="/backup_repository_idc/"
BACKUP_RETENTION_PERIOD="7"


ALERT_EMAIL_TO="naaptoltech@gmail.com;monitoring.alert@naaptol.com;"
ALERT_EMAIL_SUBJECT="IDC-RABALE REMOTE BACKUP OF ${SOURCE_SERVER_IP} SERVER"
ALERT_EMAIL_FROM="tech@naaptol.com"
ALERT_SUPPORT_EMAIL_TO="naaptoltech@gmail.com;monitoring.alert@naaptol.com;tech@naaptol.com;"

ALERT_CONF="/opt/anm/scripts/alert/"




print_header_start



if [ $# -eq 0 ]; then
        echo "Souce Server IP not Defined"
        print_header_end
        exit 1
fi

if [ -z ${SOURCE_SERVER_IP} ]; then
        echo "Souce Server IP not Defined"
        print_header_end
        exit 1
fi



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



BACKUP_FOLDER="$(date +%Y-%m-%d)"
#BACKUP_FOLDER="2012-11-10"
SOURCE_LOCATION="${SOURCE_SERVER_USER}@${SOURCE_SERVER_IP}:${SOURCE_SERVER_LOCATION}/${BACKUP_FOLDER}*"
DESTINATION_LOCATION="${DESTINATION_ROOT_LOCATION}/${SOURCE_SERVER_IP}/"
KEY_PATH="local-dsa-key"

echo -e "\n-----------------------------------------------"
echo -e "Taking Server Remote Backup with following configuration"
echo "Server IP : ${SOURCE_SERVER_IP}"
echo "Source Location : ${SOURCE_LOCATION}"
echo "Destination Location : ${DESTINATION_LOCATION} "
echo -e "-----------------------------------------------\n"

if [ ! -f ${KEY_PATH} ]; then
        ERROR_MSG="$KEY_PATH Key File not found"
        inform_error
fi

if [ ! -d ${DESTINATION_LOCATION} ]; then
        ERROR_MSG="$DESTINATION_LOCATION Destination Folder not exist"
        inform_error
fi



 echo "/usr/bin/rsync -arvz --delete --force -e \"ssh -i $KEY_PATH\"  --rsync-path=\"sudo rsync\"   $SOURCE_LOCATION $DESTINATION_LOCATION"
 /usr/bin/rsync -arvz --delete --force -e "ssh -i $KEY_PATH"  --rsync-path="sudo rsync"  $SOURCE_LOCATION $DESTINATION_LOCATION

 status="$?"

 if [ "${status}" != "0" ]; then
   ERROR_MSG="ERROR IN BACKUP SYNCHRONIZATION"
   inform_error
 fi


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

 ALERT_EMAIL_MESSAGE="${ALERT_EMAIL_SUBJECT} COMPLETED SUCESSFULLY ON $(date +%Y-%m-%d-%H-%M-%S) <BR>"
 ALERT_EMAIL_MESSAGE="${ALERT_EMAIL_MESSAGE} Server IP : ${SOURCE_SERVER_IP} <BR>"
 ALERT_EMAIL_MESSAGE="${ALERT_EMAIL_MESSAGE} Source Location : ${SOURCE_LOCATION} <BR>"
 ALERT_EMAIL_MESSAGE="${ALERT_EMAIL_MESSAGE} Destination Location : ${DESTINATION_LOCATION} <BR>"
 
 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> "

   if [ ! -d "${ALERT_CONF}" -o ! -f "${ALERT_CONF}/alert.conf"  ]; then
        echo -e "\n\nalert configuration not exist so not able to triggeer mail\n"
   else
        . ${ALERT_CONF}/alert.conf
        ${ALERT_CONF}/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######################


clean_old_backup

print_header_end


