Oracle Backup Trace File & AlertLog schedule on Everyday (shell script)

記錄script內容

1. create script by Oracle or Grid

$ vim bkOraLog.sh
#!/usr/bin/ksh
#==========================================================================
#       USAGE : bkOraLog.sh
# DESCRIPTION : Backup oracle alert/listener/trace log everyday
#      AUTHOR : 
#     CREATED : 
#    REVISION : 1
#=========================================================================
source /home/oracle/.bash_profile

# set scripts ENV
HOSTNAME=`hostname`
ALERTDIR=/u01/app/oracle/diag/rdbms/${ORACLE_UNQNAME}/${ORACLE_SID}/trace
ALERTLOG="alert_"${ORACLE_SID}".log"
LISTENERDIR=/u01/app/grid/diag/tnslsnr/${HOSTNAME}/listener/trace
LISTENERLOG="listener.log"
LISTENERLOGS="listener_*.log"
LISTENERDIRX=/u01/app/grid/diag/tnslsnr/${HOSTNAME}/listener/alert
LISTENERLOGX="log_*.xml"
ADUMPDIR=/u01/app/oracle/admin/${ORACLE_UNQNAME}/adump
BKDIR=/logbk/${HOSTNAME}
TODAY=`date -d "-1 days" +%Y%m%d`
SUORAC="su - oracle -c "
SUGRID="su - grid -c "
SCRIPTNAME=`basename "$0"`

if [ ! -d ${BKDIR} ]; then
  echo "mkdir -p ${BKDIR}"
  ${SUORAC} "mkdir -p ${BKDIR}"
fi

# remove old file
find ${BKDIR} -type f -mtime +90 -exec rm -f {} \;

# remove old adump file
find ${ADUMPDIR} -type f -mtime +90 -exec rm -f {} \;

#  ?~B~Y份 alert log
${SUORAC} "cp -p ${ALERTDIR}/${ALERTLOG} ${BKDIR}/${ALERTLOG}.${TODAY}"
if [ $? -eq 0 ]
then
  echo "cat /dev/null > ${ALERTDIR}/${ALERTLOG}"
  ${SUORAC} "cat /dev/null > ${ALERTDIR}/${ALERTLOG}"
else
  echo `date` >> /tmp/${SCRIPTNAME}.log
  echo "Error!!! cp -p ${ALERTDIR}/${ALERTLOG} ${BKDIR}/${ALERTLOG}.${TODAY}" >> /tmp/${SCRIPTNAME}.log
fi

#  ?~B~Y份 listener log
${SUORAC} "cp -p ${LISTENERDIR}/${LISTENERLOG} ${BKDIR}/${LISTENERLOG}.${TODAY}"
if [ $? -eq 0 ]
then
  echo "cat /dev/null > ${LISTENERDIR}/${LISTENERLOG}"
  ${SUGRID} "cat /dev/null > ${LISTENERDIR}/${LISTENERLOG}"

  # move mutiple listener_*.log
  for f in ${LISTENERDIR}/${LISTENERLOGS}
  do
      ${SUORAC} "cp ${f} ${BKDIR}/$(basename $f).${TODAY}"
      ${SUGRID} "rm -f ${f}"
  done

  # remove listener xml
  find ${LISTENERDIRX}/${LISTENERLOGX} -type f -mtime +7 -exec rm -f {} \;
else
  echo `date` >> /tmp/${SCRIPTNAME}.log
  echo "Error!!! cp -p ${LISTENERDIR}/${LISTENERLOG} ${BKDIR}/${LISTENERLOG}.${TODAY}" >> /tmp/${SCRIPTNAME}.log
fi

# Backup Trace File
cd ${ALERTDIR}
${SUORAC} "cd ${ALERTDIR} && tar --warning=no-file-changed -zcvf ${BKDIR}/TRACE.${TODAY}.tar.gz *.trc *.trm > ${BKDIR}/TRACE.${TODAY}.tar.log"
if [ $? -eq 0 ] || [ $? -eq 1 ]
then
  echo "rm -f ${ALERTDIR}/*.trc"
  echo "rm -f ${ALERTDIR}/*.trm"
  rm -f ${ALERTDIR}/*.trc
  rm -f ${ALERTDIR}/*.trm
else
  echo `date` >> /tmp/${SCRIPTNAME}.log
  echo "Error!!! tar --warning=no-file-changed -zcvf ${BKDIR}/TRACE.${TODAY}.tar.gz *.trc *.trm" >> /tmp/${SCRIPTNAME}.log
fi

# Remove Cluster Old File
for adrhome in `${SUGRID} "adrci exec=""show home"""`;
do
  if [[ ( "${adrhome}" != "ADR" ) && ( "${adrhome}" != "Homes:" ) ]]
  then
    # Change to 30 Days
    ${SUGRID} "adrci exec='""SET HOME $adrhome; set control \(LONGP_POLICY = 2160\); set control \(SHORTP_POLICY = 2160\)""'"
    # Delete Old Thand 30 Days
    ${SUGRID} "adrci exec='""SET HOME $adrhome; purge -age 10080;""'"
  fi;
done;

2. edit crontab by root ( the script will be change user )

# crontab -e
0 0 * * * {SCIRPT_PATH}/bkOraLog.sh > /dev/null 2>&1

已轉向 blogger 記錄

https://slowlife-notes.blogspot.com