Purge Oracle Maintenance Logs

Purging Oracle Logs

The basis of this script is to purge logs from the Oracle Base directory daily

The script will be executed from cron on a daily basis, however this could run at different times on each system due to business and or system load requirements.
Technical Steps

#!/bin/ksh
#--------------------------------------------------------------------------------
#--
#-- File name:   purge_logs.sh
#-- Purpose:     Script is used to purge all relevant logs from the Oracle Base location.
#--              Remove Archive Logs older than one day
#--
#-- Author:      Mark Young
#-- Copyright:   TOLL GIS
#--
#--
#--------------------------------------------------------------------------------
#--  Usage:
#--  purge_logs.sh
#--
#--  Examples:
#--  purge_logs.sh
#--
#--
#--  Modified   (MM/DD/YY)
#--  marky       6/12/11 - Creation
#--
#--  If any changes are made to this script, it must be updated in confluence
#--
#--  Location of this script
#--  http://confluence.toll.com.au/display/OPSDBA/Purge+Oracle+Log+Maintenance #--   RGM 9/11/12 - added UID to flag file for multiple users to use..
#--------------------------------------------------------------------------------
 
OracleBase=/usr/local/oracle/admin
 
#Make sure the script isn't already runningIsRunning=/tmp/.isrunning
LOGFILE=/var/local/oracle/log/purge_log.log
HOSTNAME=`hostname`
DBAMAIL=dba_team@toll.com.au
UID=`id -u`
IsRunning="/tmp/.isrunning${UID}"
 
if [ -d "$OracleBase" ]; then
        RC=0
else
        echo "Failed to find directory /usr/local/oracle/admin the script has stopped due to this error" > $LOGFILE
        RC=1
fi
 
 
if [[ -f ${IsRunning} || ${RC} -gt 0 ]]; then
        echo "The script is already running.. Exiting" > $LOGFILE
        RC=1
else
        touch $IsRunning
        echo "The script is about to start"
 
        #Process trc files, but keep files for the past 30 days
        find $OracleBase -mtime +30 -type f -name "*.trc" -exec rm -rf {} \;
 
        #Process AUD files, but keep files for the past 5 days
        find $OracleBase -mtime +5 -type f -name "*.aud" -exec rm -rf {} \;
 
        #Process txt files, but keep files for the past 7 days
        find $OracleBase -mtime +7 -type f -name "*.txt" -exec rm -rf {} \;
 
        #Process txt files, but keep files for the past 5 days
        #turned this off, Oracle 12c agent creates a directory called core which is removed by this script
        #find $OracleBase -mtime +5 -type d -name core -exec rm -rf {} \;
 
        #Remove the file for next time.
        rm $IsRunning
        RC=$?
fi
 
 
        if [ $RC -ne "0" ]; then
                echo "The script failed and needs your attention" >> $LOGFILE
                rm $IsRunning
                mailx -s "purge_log.sh has failed on $HOSTNAME, this needs your attention" $DBAMAIL < $LOGFILE
        fi

Change permissions on this file

chmod +x /usr/local/oracle/admin/scripts/ksh/purge_logs.sh

Crontab Entry

# oracle crontab placeholder
#  .---------------- minute (0 - 59)
#  |   .------------- hour (0 - 23)
#  |   |   .---------- day of month (1 - 31)
#  |   |   |   .------- month (1 - 12) OR jan,feb,mar,apr ...
#  |   |   |   |  .----- day of week (0 - 6) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
#  |   |   |   |  |
#  *   *   *   *  *  command to be executed

30 6 * * * /usr/local/oracle/admin/scripts/ksh/purge_logs.sh > /dev/null 2>&1

>Known Issues
Must be run on a system 10g or earlier. We are going to be using ADRCI for 11g

Applies to:
Oracle Version: 10g and earlier. Information in this document applies to any platform.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.