cross clmn

Monday 2 May 2016

Script for NZHostbackup on IBM Netezza

##Script for NZHostbackup on IBM Netezza
## Manish Jaiswal
#!/bin/sh
cd /export/home/nz
. ~/.bashrc
export PATH=/nz/kit/bin:/nz/kit/sbin:/nz/kit/bin/adm:/bin:/usr/bin:/usr/local/bin:.:/nz/support/bin
DATE=`date +%Y-%m-%d:%H:%M:%S`
HOST=`hostname`
email_list=manish.jaiswal@gmail.com
EVENT_LOG_FILE_BEFORE=/export/home/nz/scripts/log/EVENT_LOG_FILE_BEFORE_$DATE.log
NZ_HOST_BACKUP_DMP_FILE=/dbbackup/nz_hostbackup/edw-ntz-001-lqc/nz_hostbackup_$DATE.dmp
NZ_HOST_BACKUP_LOG_FILE=/dbbackup/nz_hostbackup/edw-ntz-001-lqc/nz_hostbackup_$DATE.log
NPS_STATE=`nzstate | awk '{print $4}' | tr -d "[.']" | tr "[a-z]" "[A-Z]"`

# -----------------------------------------------------------------------------
# Make sure the system is online and no backups are running.
# -----------------------------------------------------------------------------
# ------------------------------------------------------------------------
# Make sure the system is online.
# ------------------------------------------------------------------------
if [ "${NPS_STATE}" = "ONLINE" ]
then
   echo "${NPS_STATE}"
SYSTEMSTATE=0

#elif [ "${NPS_STATE}" = "STOPPED" ]
#then
        echo "${NPS_STATE}"

else
   echo "NPS ${NPS_STATE} ONLINE REQUIRED EXITING!"
   echo  "NZHostbackup Required NPS state of online not the current state! Current state: [${NPS_STATE}]"|mailx -s "Notification:NZHOSTBACKUP  on  $HOST " $email_list
   exit;
fi

# ------------------------------------------------------------------------
# Make sure that there are no backups & Restore under execution.
# ------------------------------------------------------------------------
COUNT_OF_BACKUP_PROCESSES=`ps -ef | grep nzbackup | grep -v grep | wc -l`
COUNT_OF_RESTORE_PROCESSES=`ps -ef | grep nzrestore | grep -v grep | wc -l`

if [ ${COUNT_OF_BACKUP_PROCESSES} -eq 0 ] && [ ${COUNT_OF_RESTORE_PROCESSES} -eq 0 ]
then
        echo " NO Backup & Restore is running "
BACKUPSTATE=0
else
BR_COUNT=${COUNT_OF_BACKUP_PROCESSES} + ${COUNT_OF_RESTORE_PROCESSES}
     echo  "NPS having ${COUNT_OF_BACKUP_PROCESSES} RUNNING BACKUPS FOUND EXITING!"
     echo  "NZHostbackup  Catalog backups cannot run with other backups! Found ${BR_COUNT} running backup(s)."|mailx -s "Notification:NZHOSTBACKUP  on  $HOST" $email_list
     exit;
fi
# -----------------------------------------------------------------------------
# Disable the state change events on the system.
# -----------------------------------------------------------------------------
# ------------------------------------------------------------------------
# Log the state of the events on the system.
# ------------------------------------------------------------------------
nzevent show -maxColW 4096 -orient vertical   > ${EVENT_LOG_FILE_BEFORE}
RetCode=$?

if [ ${RetCode} -eq 0 ]
then
        echo "Successful"
else
        echo "FAILED!"
        echo "NZHostbackup Error logging system events! Error: [${RetCode}]"|mailx -s "Notification:NZHOSTBACKUP  on  $HOST " $email_list
        exit;
fi

# ------------------------------------------------------------------------
# Disable the events.
# ------------------------------------------------------------------------
# Cut the event listing down to the name of the event, the state, and the type.  Then loop on the output.
nzevent show -maxColW 4096 -orient vertical | grep -E  ^'Name|On |Event Type' | while read line
do

# If we're reading a Name then grab the 2nd field as the value.
if [ `echo ${line} | grep -c "^Name"` -eq 1 ]
then
        EvtName=`echo ${line} | awk '{print $2}'`
        continue
fi

# If we're reading a status line then grab the 2nd field as the value.
if [ `echo ${line} | grep -c "^On"` -eq 1 ]
then
        EvtOn=`echo ${line} | awk '{print $2}'`
        continue
fi

# If we're reading a type line then grab the 3rd field as the value.
if [ `echo ${line} | grep -c "^Event Type"` -eq 1 ]
then
        EvtType=`echo ${line} | awk '{print $3,$4,$5}'`
fi

# Only attempt to disable an event if we have all three values present.
if [ ${#EvtName} -gt 0 -a ${#EvtOn} -gt 0 -a ${#EvtType} -gt 0 ]
then
    # Only deal with the target event type.
    if [ "${EvtType}" = "Sys State Changed" ]
    then
        # If the event is enabled then we have something to do.
        # Otherwise we don't care.
        if [ "${EvtOn}" = "yes" ]
        then
           echo "   Disabling event: ${EvtName}"

           nzevent modify -name ${EvtName} -on false
           RetCode=$?

           if [ ${RetCode} -eq 0 ]
           then
               echo "Successful"
           else
               echo "FAILED!"
               echo "NZHOSTBACKUP DISABLE event error: ${EvtName} on ${BACKUP_HOST}! [${RetCode}]"|mailx -s "Notification:NZHOSTBACKUP  on  $HOST " $email_list
               exit;
           fi
        fi
    fi
fi
done

# ------------------------------------------------------------------------
# Execute the catalog backup.
# ------------------------------------------------------------------------
echo "Performing host catalog backup of ${HOST}"

BACKUP_START=`date "+%Y-%m-%d %H:%M:%S"`

nzhostbackup ${NZ_HOST_BACKUP_DMP_FILE}       > ${NZ_HOST_BACKUP_LOG_FILE} 2>&1
RetCode=$?

# Check the outcome.
if [ ${RetCode} -eq 0 ]
then
     BACKUP_END=`date "+%Y-%m-%d %H:%M:%S"`
     BACKUP_STATUS="COMPLETED"
echo "NZHOSTBACKUP host catalog backup of ${HOST} Success [${RetCode}]"|mailx -s "Success :NZHOSTBACKUP  on  $HOST " $email_list
else
     BACKUP_STATUS="FAILED"
     rm -f ${NZ_HOST_BACKUP_DMP_FILE}
     echo "NZHOSTBACKUP host catalog backup of ${HOST} failure! [${RetCode}]"|mailx -s "Failed:NZHOSTBACKUP  on  $HOST " $email_list

fi
# ------------------------------------------------------------------------
# Enable the events.
# ------------------------------------------------------------------------
# Cut the event listing down to the name of the event, the state, and the type.  Then loop on the output.
nzevent show -maxColW 4096 -orient vertical | grep -E  ^'Name|On |Event Type' | while read line
do

  # If we're reading a Name then grab the 2nd field as the value.
  if [ `echo ${line} | grep -c "^Name"` -eq 1 ]
  then
          EvtName=`echo ${line} | awk '{print $2}'`
          continue
  fi

  # If we're reading a status line then grab the 2nd field as the value.
  if [ `echo ${line} | grep -c "^On"` -eq 1 ]
  then
          EvtOn=`echo ${line} | awk '{print $2}'`
          continue
  fi

  # If we're reading a type line then grab the 3rd field as the value.
  if [ `echo ${line} | grep -c "^Event Type"` -eq 1 ]
  then
          EvtType=`echo ${line} | awk '{print $3,$4,$5}'`
  fi

  # Only attempt to Enable an event if we have all three values present.
  if [ ${#EvtName} -gt 0 -a ${#EvtOn} -gt 0 -a ${#EvtType} -gt 0 ]
  then
      # Only deal with the target event type.
      if [ "${EvtType}" = "Sys State Changed" ]
      then
          # If the event is disabled then we have something to do.
          # Otherwise we don't care.
          if [ "${EvtOn}" = "no" ]
          then
              echo" Enabling event: ${EvtName}"
              nzevent modify -name ${EvtName} -on true
              RetCode=$?

              if [ ${RetCode} -eq 0 ]
              then
                      echo "Successful"
              else
              echo "FAILED!"
              echo "NZHOSTBACKUP ENABLE event error: ${EvtName} on ${BACKUP_HOST}! [${RetCode}]"|mailx -s "Notification:NZHOSTBACKUP  on  $HOST " $email_list      
 exit;
              fi
          fi
      fi
  fi
done
###Done

No comments:

Post a Comment