Pages

Tuesday, October 14, 2014

bash script by example. Create crontab task, to run a script that does cleanup of old oracle files.

============================
Elaborate Version
============================
============================
General

============================
Common task, of running a crontab task once a day, at 5:00 AM, to delete oracle generated files, that exceed the keep time parameters.
Below are two versions, doing same stuff.


============================
Simple Version
============================
>% crontab -l
0 6 * * * /software/oracle/oracle/scripts/delete_old_trace_files.sh

delete_old_trace_files.sh
export ORA_INST=igt
export DAYS_TO_KEEP=14
export DAYS_TO_KEEP_LIST=7
export LIST_SERVER=tha-ais-2-dwh-1

export LISTENER_ROOT=/software/oracle/diag/tnslsnr/${LIST_SERVER}/lsnr_igt/trace
export LISTENER_ROOT_ALERT=/software/oracle/diag/tnslsnr/${LIST_SERVER}/lsnr_igt/alert

find /software/oracle/diag/rdbms/${ORA_INST}/${ORA_INST}/trace -type f -name "*.trc" -mtime +${DAYS_TO_KEEP} -exec rm {} \;
find /software/oracle/diag/rdbms/${ORA_INST}/${ORA_INST}/trace -type f -name "*.trm" -mtime +${DAYS_TO_KEEP} -exec rm {} \;
find /software/oracle/diag/rdbms/${ORA_INST}/${ORA_INST}/trace -type f -size +1000M -exec rm {} \;

find $LISTENER_ROOT_ALERT  -type f -mtime +${DAYS_TO_KEEP_LIST} -exec rm {} \;

mv -f  ${LISTENER_ROOT}/lsnr_igt.log    ${LISTENER_ROOT}/lsnr_igt.log_1
mv -f  ${LISTENER_ROOT}/lsnr_igt.log_1  ${LISTENER_ROOT}/lsnr_igt.log_2

mv -f  ${LISTENER_ROOT}/lsnr_igt.log_2  ${LISTENER_ROOT}/lsnr_igt.log_3

============================
Elaborate Version
============================

crontab task
>% crontab -l
0 5 * * * /usr/local/etc/homedir/oracle_purge/oracle_purge.sh inst

script
====================================
oracle_purge.sh
====================================

#!/bin/bash
#
# NAME:
#       oracle_purge.sh
#
# ABSTRACT:
#
#       The script cleanup unwanted oracle files
#
# ARGUMENTS:
#
#       Syntax: oracle_purge.sh instance
#
#       instance - the name of the oracle instance we want to cleanup
#       days_to_keep - the number of days to keep trace/log/audit files
#
#
# ENVIRONMENT VAR:
#
#       ORASH
#       BAS_ORACLE_LIST
#
# INPUT FORMAT:
#
# OUTPUT FORMAT:
#
# RETURN/EXIT STATUS:
#       0 - Normal exit
#       1 - Syntax error
#       2 - Unknown instance
#
# LIMITATIONS:
#
# MODIFICATION HISTORY:
# +-------------+------+------------------------------------------------------+
# | Date        | Name |  Description                                         |
# +-------------+------+------------------------------------------------------+
# |             |      |                                                      |
# +-------------+------+------------------------------------------------------+

. /etc/profile > /dev/null 2> /dev/null


FACILITY=`basename $0 .sh`\:

TIMESTAMP=`date +%d_%m_%y_%H_%M`
UNAME=`uname | cut -d_ -f1`
INI_FILE=`dirname $0`/conf/`basename $0 .sh`.ini

if [ $# -ne 1 ] ; then

   echo Syntax: oracle_purge.sh instance>&2
   exit 1
fi

DAYS_TO_KEEP=`grep ^DAYS_TO_KEEP ${INI_FILE} | cut -d= -f2`

MIN_TO_KEEP=$((${DAYS_TO_KEEP}*24*60))

INSTANCE=$1

shift $#

echo $BAS_ORACLE_LIST | grep -q $INSTANCE


if [ $? -ne 0 ] ; then

   echo $FACILITY `date` ERR- $INSTANCE - unknown oracle instance . >&2
   exit 2
fi

# Inits the Oracle environment of the specific instance.

. $ORASH/oracle_login.sh $INSTANCE

# Delete old aud files

if [ $UNAME = 'CYGWIN' ] ; then
  CYG_ORACLE_BASE=`echo $CYG_ORACLE_HOME | cut -d/ -f1-5`
  AUD_FILE_LOC=${CYG_ORACLE_BASE}/admin/${INSTANCE}/adump
else
  AUD_FILE_LOC=/software/oracle/admin/${INSTANCE}/adump
fi

find ${AUD_FILE_LOC} -name '*.aud' -mtime +${DAYS_TO_KEEP} -exec \rm -f {} \;


# Purge Oracle diagnostic files

for home_name in `adrci exec="show homes" | tail -n +2`
do
  if [ $UNAME = 'CYGWIN' ] ; then
    home_name=$(echo $home_name | sed 's/\\/\\\\\\/g')
  fi
  # For each ADR home, purge the files older than 60 days (86400 minutes)
  adrci exec="set home ${home_name} ; purge -age ${MIN_TO_KEEP}"
done


Helper shell script to set environment variables.
====================================
$ORASH/oracle_login.sh
====================================
#!/bin/bash
#
# NAME:
#      oracle_login.sh
#
# ABSTRACT: Defining oracle's environment variables. - Unix version
#
# ARGUMENTS: [p1] - oracle sid
#             Note - 1) the parameter can be omitted
#
#
# SPECIAL FILE:
#
# ENVIRONMENT VAR:
#     PROJECT_SID -
#        should be defined in the following way:
#        globally       - the node run only one oracle instance
#        group login    - there is more than one instance on the current node
#        as a parameter - there is more than one instance per project,
#                         or for system users.
#
# SPECIAL CCC MACROS:
#
# INPUT FORMAT:
#
# OUTPUT FORMAT:
#
# RETURN/EXIT STATUS:
#            1) PROJECT_SID is not defined
#            2) the database PROJECT_SID does not exist
#            3) PROJECT_SID missing from $ORASH/sid.hosts
#            4) ORA_VER missing from $ORASH/sid.hosts
#            5) charcter missing for current sid from $ORASH/charset.dat

if [ "$ORASH" = "" ]; then

   ORASH=$SH_ETC/etc/orash
fi

FACILITY="oracle_login:"

UNAME=`uname | cut -d_ -f1`
UNAME_M=`uname -m`

  if [ "$#" -ge 1 ]; then

      PROJECT_SID=$1
   else
      project_sid_def=`set | grep "^PROJECT_SID=" |wc -l`
      if [ "$project_sid_def" -eq 0 ]; then
         echo "<oracle_login> ERROR>> PROJECT_SID is not defined"
         return 1
      fi
   fi

   ORACLE_BASE=/software/oracle

   project_sid_def=`set | grep "^PROJECT_SID=" |wc -l`
   if [ "$project_sid_def" -eq 0 ]; then
         echo "<oracle_login> ERROR>> PROJECT_SID is not defined"
         return 1
   fi

   ORACLE_HOME=`grep ^"$PROJECT_SID": /var/opt/oracle/oratab | awk '{if (NR==1) print $0}' | cut -d: -f2`

   if [ "$ORACLE_HOME" = "" ]; then
      echo "<oracle_login> ERROR>> the database $PROJECT_SID does not exist in /var/opt/oracle/oratab or /etc/oratab"
      return 2
   fi

   ORA_VER=`grep ^"$PROJECT_SID": $ORASH/sid.hosts | awk '{if (NR==1) print $0}' |cut -d: -f3`

   if [ "$ORA_VER" = "" ]; then
      echo "$FACILITY unable to verify ORA_VER for $PROJECT_SID using $ORASH/sid.hosts"
      return 4
   fi

   DEFAULT_SID=$PROJECT_SID


   ORACLE_SID=$DEFAULT_SID

   TNS_ADMIN=${ORACLE_HOME}/network/admin

#----------------------------------------------------------------#

# the hostname of the DEFAULT_SID appears first in file sid.hosts
#----------------------------------------------------------------#
   HOST=`grep ^"$PROJECT_SID": $ORASH/sid.hosts | awk '{if (NR==1) print $0}' | cut -d: -f2`
   if [ "$HOST" = "" ]; then
      echo "<oracle_login> ERROR>> the database $PROJECT_SID does not exist in $ORASH/sid.hosts"
      return 3
   fi

#-------------------------------------#

#  TWO_TASK is used for a unix clients
#-------------------------------------#
# Disable TWO_TASK feature eliminating the need to change hostname, after disk replication
#   NODE=`hostname`
#   if [ "$NODE" != "$HOST" ]; then
#      TWO_TASK=${DEFAULT_SID}_remote
#   fi

#-------------------------------------#

# setting up NLS_LANG
#-------------------------------------#
   CHARSET=`grep ^"$PROJECT_SID": $ORASH/charset.dat | awk '{if (NR==1) print $0}' | cut -d: -f2`
   if [ "$CHARSET" = "" ]; then
      echo "<oracle_login> ERROR>> the database $PROJECT_SID does not exist in $
ORASH/charset.dat"
      return 5
   fi

   NLS_DATE_FORMAT="DD-MON-RR"

   ORACLE_LPPROG=lpr
   ORACLE_LPARGS="-P la424"
   NLS_LANG="AMERICAN_AMERICA."${CHARSET}
   ORA_NLS32=$ORACLE_HOME/ocommon/nls/admin/data # for oracle
   ORA_NLS33=$ORACLE_HOME/ocommon/nls/admin/data # for oracle8

   cleaned_path=$PATH

   for ora_path in `grep -v "^#" /var/opt/oracle/oratab | cut -d: -f2 | uniq` ; do
      cleaned_path=`echo -e "${cleaned_path}\c" | tr ':' '\n' | grep -v "$ora_path" | tr '\n' ':'`
   done
   if [ $UNAME = 'SunOS' ] ; then
     PATH=$cleaned_path:$ORACLE_HOME/bin:/usr/ccs/bin:/usr/openwin/bin
   else
     PATH=$cleaned_path$ORACLE_HOME/bin
   fi

   shlib_def=`set | grep "^LD_LIBRARY_PATH" |wc -l`

   if [ "$shlib_def" -eq 0 ]; then
      LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib
   else
      cleaned_shlib_path=$LD_LIBRARY_PATH
      for ora_path in `grep -v "^#" /var/opt/oracle/oratab | cut -d: -f2 | uniq` ; do
         cleaned_shlib_path=`echo -e "${cleaned_shlib_path}\c" | tr ':' '\n' | grep -v "$ora_path" | tr '\n' ':'`
      done

      if [ $UNAME = 'SunOS' ] ; then

         LD_LIBRARY_PATH=$cleaned_shlib_path:$ORACLE_HOME/lib:/usr/lib
      else
         LD_LIBRARY_PATH=$cleaned_shlib_path$ORACLE_HOME/lib:/usr/lib
      fi
   fi

   if [ $UNAME_M = 'x86_64' ]; then

      LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ORACLE_HOME}/lib32
   fi

   export PROJECT_SID

   export ORACLE_TERM
   export ORACLE_HOME
   export ORACLE_BASE
   export ORACLE_SID
   export TNS_ADMIN
   export ORACLE_SERVER
   export PATH
   export LD_LIBRARY_PATH
   export NLS_LANG
   export ORA_NLS33
   unset ORA_NLS32
   ORACLE_ENV_DEFINED="yes"
   export ORACLE_ENV_DEFINED
   export ORA_VER



No comments:

Post a Comment