Friday, February 14, 2014

Oracle Logs CLEANUP Script

In this post, I'll share a script to backup & cleanup Oracle logs associated with a specific database on the Oracle server.

This script was tested on Linux & SUN environments.

You can download the script from here:
https://www.dropbox.com/s/eytsv5duxe95lrh/oracle_cleanup.sh?dl=0

Once you run it, it will ask you to select a database (in case you have multiple running databases on the server), then it will ask you the location you want to back up the logs, then it will start to clean up all logs under udump, bdump, cdump folder plus the audit logs and also will clean up the logs of the listener associated with the selected database.

Note: This script will backup and delete all logs and will keep the logs of the last 5 days only.

Note: it's recommended to test this script on a test environment before you run it on production.

Also, you can download the whole DBA bundle which having many other smart and easy to use scripts for database administration tasks:
http://dba-tips.blogspot.ae/2014/02/oracle-database-administration-scripts.html

DISCLAIMER: THIS SCRIPT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY. IT IS PROVIDED "AS IS".

Below is the code of this script in case the download link didn't work:

# ###################################################################################
# This script Backup & Cleanup the database logs.
# To be run by ORACLE user
# [Ver 2.0]
# # # #
# Author: Mahmmoud ADEL # # # # ###
# Created: 03-06-2013 # # # # #
# Modified: 02-07-2013
# 14-01-2014 Customized the script to run on various environments.
# 14-06-2017 Increased the script accuracy and elimiated tar bug.
# 15-05-2018 Added the option of archiving Audit log files.
# 27-12-2018 Verify the trace/log locations are valid before cleaning.
# 23-01-2019 Added the option of Skipping backing up the trace/logs.
# 03-07-2020 Enhance locating the listener's log.
# 03-07-2020 Enhance locating the alertlog/bdump/cdump location.
#
#
# ###################################################################################
SCRIPT_NAME="oracle_cleanup"
# ###########
# Description:
# ###########
echo
echo "=================================================================="
echo "This script will Back up & Delete the database logs and Audit logs ..."
echo "=================================================================="
echo
sleep 1
# #######################################
# Excluded INSTANCES:
# #######################################
# Here you can mention the instances the script will IGNORE and will NOT run against:
# Use pipe "|" as a separator between each instance name.
# e.g. Excluding: -MGMTDB, ASM instances:
EXL_DB="\-MGMTDB|ASM|APX" #Excluded INSTANCES [Will not get reported offline].
# ###########################
# Listing Available Instances:
# ###########################
# Count Instance Numbers:
INS_COUNT=$( ps -ef|grep pmon|grep -v grep|egrep -v ${EXL_DB}|wc -l )
# Exit if No DBs are running:
if [ $INS_COUNT -eq 0 ]
then
echo No Database Running !
exit
fi
# If there is ONLY one DB set it as default without prompt for selection:
if [ $INS_COUNT -eq 1 ]
then
export ORACLE_SID=$( ps -ef|grep pmon|grep -v grep|egrep -v ${EXL_DB}|awk '{print $NF}'|sed -e 's/ora_pmon_//g'|grep -v sed|grep -v "s///g" )
# If there is more than one DB ASK the user to select:
elif [ $INS_COUNT -gt 1 ]
then
echo
echo "Select the Instance You Want To Backup & Delete It's Logs: [Enter the Number]"
echo "----------------------------------------------------------"
select DB_ID in $( ps -ef|grep pmon|grep -v grep|egrep -v ${EXL_DB}|awk '{print $NF}'|sed -e 's/ora_pmon_//g'|grep -v sed|grep -v "s///g" )
do
if [ -z "${REPLY##[0-9]*}" ]
then
export ORACLE_SID=$DB_ID
echo
echo Selected Instance:
echo "********"
echo $DB_ID
echo "********"
break
else
export ORACLE_SID=${REPLY}
break
fi
done
fi
# Exit if the user selected a Non Listed Number:
if [ -z "${ORACLE_SID}" ]
then
echo "You've Entered An INVALID ORACLE_SID"
exit
fi
# #########################
# Getting ORACLE_HOME
# #########################
ORA_USER=`ps -ef|grep ${ORACLE_SID}|grep pmon|grep -v grep|egrep -v ${EXL_DB}|grep -v "\-MGMTDB"|awk '{print $1}'|tail -1`
USR_ORA_HOME=`grep -i "^${ORA_USER}:" /etc/passwd| cut -f6 -d ':'|tail -1`
# SETTING ORATAB:
if [ -f /etc/oratab ]
then
ORATAB=/etc/oratab
export ORATAB
## If OS is Solaris:
elif [ -f /var/opt/oracle/oratab ]
then
ORATAB=/var/opt/oracle/oratab
export ORATAB
fi
# ATTEMPT1: Get ORACLE_HOME using pwdx command:
PMON_PID=`pgrep -lf _pmon_${ORACLE_SID}|awk '{print $1}'`
export PMON_PID
ORACLE_HOME=`pwdx ${PMON_PID}|awk '{print $NF}'|sed -e 's/\/dbs//g'`
export ORACLE_HOME
# ATTEMPT2: If ORACLE_HOME not found get it from oratab file:
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
## If OS is Linux:
if [ -f /etc/oratab ]
then
ORATAB=/etc/oratab
ORACLE_HOME=`grep -v '^\#' ${ORATAB} | grep -v '^$'| grep -i "^${ORACLE_SID}:" | perl -lpe'$_ = reverse' | cut -f3 | perl -lpe'$_ = reverse' |cut -f2 -d':'`
export ORACLE_HOME
## If OS is Solaris:
elif [ -f /var/opt/oracle/oratab ]
then
ORATAB=/var/opt/oracle/oratab
ORACLE_HOME=`grep -v '^\#' ${ORATAB} | grep -v '^$'| grep -i "^${ORACLE_SID}:" | perl -lpe'$_ = reverse' | cut -f3 | perl -lpe'$_ = reverse' |cut -f2 -d':'`
export ORACLE_HOME
fi
fi
# ATTEMPT3: If ORACLE_HOME is in /etc/oratab, use dbhome command:
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
ORACLE_HOME=`dbhome "${ORACLE_SID}"`
export ORACLE_HOME
fi
# ATTEMPT4: If ORACLE_HOME is still not found, search for the environment variable: [Less accurate]
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
ORACLE_HOME=`env|grep -i ORACLE_HOME|sed -e 's/ORACLE_HOME=//g'`
export ORACLE_HOME
fi
# ATTEMPT5: If ORACLE_HOME is not found in the environment search user's profile: [Less accurate]
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
ORACLE_HOME=`grep -h 'ORACLE_HOME=\/' ${USR_ORA_HOME}/.bash_profile ${USR_ORA_HOME}/.*profile | perl -lpe'$_ = reverse' |cut -f1 -d'=' | perl -lpe'$_ = reverse'|tail -1`
export ORACLE_HOME
fi
# ATTEMPT6: If ORACLE_HOME is still not found, search for orapipe: [Least accurate]
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
if [ -x /usr/bin/locate ]
then
ORACLE_HOME=`locate -i orapipe|head -1|sed -e 's/\/bin\/orapipe//g'`
export ORACLE_HOME
fi
fi
# TERMINATE: If all above attempts failed to get ORACLE_HOME location, EXIT the script:
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
echo "Please export ORACLE_HOME variable in your .bash_profile file under oracle user home directory in order to get this script to run properly"
echo "e.g."
echo "export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1"
exit
fi
# ########################################
# Exit if the user is not the Oracle Owner:
# ########################################
CURR_USER=`whoami`
if [ ${ORA_USER} != ${CURR_USER} ]; then
echo ""
echo "You're Running This Sctipt with User: \"${CURR_USER}\" !!!"
echo "Please Run This Script With The Right OS User: \"${ORA_USER}\""
echo "Script Terminated!"
exit
fi
# ########################
# Getting ORACLE_BASE:
# ########################
echo "Setting ORACLE_BASE ..."
# Get ORACLE_BASE from user's profile if it EMPTY:
if [ ! -d "${ORACLE_BASE}" ]
then
ORACLE_BASE=`cat ${ORACLE_HOME}/install/envVars.properties|grep ^ORACLE_BASE|tail -1|awk '{print $NF}'|sed -e 's/ORACLE_BASE=//g'`
export ORACLE_BASE
fi
if [ ! -d "${ORACLE_BASE}" ]
then
ORACLE_BASE=`grep -h 'ORACLE_BASE=\/' ${USR_ORA_HOME}/.bash* ${USR_ORA_HOME}/.*profile | perl -lpe'$_ = reverse' |cut -f1 -d'=' | perl -lpe'$_ = reverse'|tail -1`
export ORACLE_BASE
fi
# Neutralize login.sql file:
# #########################
# Existance of login.sql file under current working directory eliminates many functions during the execution of this script:
if [ -f ./login.sql ]
then
mv ./login.sql ./login.sql_NeutralizedBy${SCRIPT_NAME}
fi
if [ -f ${USR_ORA_HOME}/login.sql ]
then
mv ${USR_ORA_HOME}/login.sql ${USR_ORA_HOME}/login.sql_NeutralizedBy${SCRIPT_NAME}
fi
# #########################
# Getting DB_NAME:
# #########################
echo "Getting DB NAME ..."
DB_NAME_RAW=$(${ORACLE_HOME}/bin/sqlplus -S "/ as sysdba" <<EOF
set pages 0 feedback off lines 1000;
prompt
SELECT name from v\$database;
exit;
EOF
)
# Getting DB_NAME in Uppercase & Lowercase:
DB_NAME_UPPER=`echo ${DB_NAME_RAW}| perl -lpe'$_ = reverse' |awk '{print $1}'|perl -lpe'$_ = reverse'`
DB_NAME_LOWER=$( echo "${DB_NAME_UPPER}" | tr -s '[:upper:]' '[:lower:]' )
export DB_NAME_UPPER
export DB_NAME_LOWER
# #########################
# Getting DB_UNQ_NAME:
# #########################
VAL121=$(${ORACLE_HOME}/bin/sqlplus -S "/ as sysdba" <<EOF
set pages 0 feedback off;
prompt
select value from v\$parameter where name='db_unique_name';
exit;
EOF
)
# Getting DB_NAME in Uppercase & Lowercase:
DB_UNQ_NAME=`echo ${VAL121}| perl -lpe'$_ = reverse' |awk '{print $1}'|perl -lpe'$_ = reverse'`
export DB_UNQ_NAME
# In case DB_UNQ_NAME variable is empty then use DB_NAME instead:
case ${DB_UNQ_NAME} in
'') DB_UNQ_NAME=${DB_NAME}; export DB_UNQ_NAME;;
esac
if [ -d ${ORACLE_BASE}/diag/rdbms/${DB_NAME_UPPER} ]
then
DB_NAME=${DB_NAME_UPPER}
fi
if [ -d ${ORACLE_BASE}/diag/rdbms/${DB_NAME_LOWER} ]
then
DB_NAME=${DB_NAME_LOWER}
fi
if [ -d ${ORACLE_BASE}/diag/rdbms/${DB_UNQ_NAME} ]
then
DB_NAME=${DB_UNQ_NAME}
fi
export DB_NAME
# #########################
# Getting ALERTLOG path:
# #########################
# First Attempt:
VAL_DUMP=$(${ORACLE_HOME}/bin/sqlplus -S "/ as sysdba" <<EOF
set pages 0 feedback off lines 30000;
prompt
SELECT VALUE from V\$DIAG_INFO where name='Diag Trace';
exit;
EOF
)
ALERTZ=`echo ${VAL_DUMP} | perl -lpe'$_ = reverse' |awk '{print $1}'|perl -lpe'$_ = reverse'`
ALERTDB=${ALERTZ}/alert_${ORACLE_SID}.log
export ALERTDB
# Second Attempt:
if [ ! -f ${ALERTDB} ]
then
VAL_DUMP=$(${ORACLE_HOME}/bin/sqlplus -S "/ as sysdba" <<EOF
set pages 0 feedback off lines 30000;
prompt
SELECT value from v\$parameter where NAME='background_dump_dest';
exit;
EOF
)
ALERTZ=`echo ${VAL_DUMP} | perl -lpe'$_ = reverse' |awk '{print $1}'|perl -lpe'$_ = reverse'`
ALERTDB=${ALERTZ}/alert_${ORACLE_SID}.log
export ALERTDB
fi
# Third Attempt:
if [ ! -f ${ALERTDB} ]
then
VAL_DUMP=$(${ORACLE_HOME}/bin/sqlplus -S "/ as sysdba" <<EOF
set pages 0 feedback off lines 30000;
prompt
SELECT value from v\$parameter where NAME='core_dump_dest';
exit;
EOF
)
ALERTZ=`echo ${VAL_DUMP} | perl -lpe'$_ = reverse' |awk '{print $1}'|perl -lpe'$_ = reverse'|sed -e 's/\/cdump/\/trace/g'`
ALERTDB=${ALERTZ}/alert_${ORACLE_SID}.log
export ALERTDB
fi
# Forth Attempt:
if [ ! -f ${ALERTDB} ]
then
ALERTDB=${ORACLE_BASE}/diag/rdbms/${DB_NAME}/${ORACLE_SID}/trace/alert_${ORACLE_SID}.log
export ALERTDB
fi
# Fifth Attempt: [Expensive search with locate command]
if [ ! -f ${ALERTDB} ]
then
if [ -x /usr/bin/locate ]
then
ALERTDB=`ls -rtl \`locate alert_${ORACLE_SID}\`|tail -1|awk '{print $NF}'`
export ALERTDB
fi
fi
if [ -f ${ALERTDB} ]
then
BDUMP=`echo ${VAL_DUMP} | perl -lpe'$_ = reverse' |awk '{print $1}'|perl -lpe'$_ = reverse'`
DUMP=`echo ${BDUMP} | sed 's/\/trace//g'`
CDUMP=${DUMP}/cdump
export BDUMP
export DUMP
export CDUMP
fi
echo DUMP location is: ${DUMP}
echo BDUMP location is: ${BDUMP}
# #########
# Variables:
# #########
echo ""
echo "Do you want to BACK UP the LOGFILES before Cleaning them up? [Y|N] Default is NO"
echo "============================================================"
while read ANS
do
case $ANS in
y|Y|yes|YES|Yes) export BACKUP_FLAG="ON"; break ;;
""|n|N|NO|no|No) echo; export BACKUP_FLAG=""; break ;;
*) echo; echo "Please enter a VALID answer [Y|N]" ;;
esac
done
export HASH_BKP="echo #"
case $BACKUP_FLAG in
ON)
export HASH_BKP=""
echo ""
echo "Please Enter The Full Path of Backup Location: [i.e. /tmp]"
echo "============================================="
while read LOC1
do
if [ ! -w "${LOC1}" ]; then
echo "Provided Backup Location is NOT Exist/Writable !"
echo
echo "Please Provide a VALID Backup Location:"
echo "--------------------------------------"
else
BKP_LOC_DB=${LOC1}/${ORACLE_SID}_DB_LOGS/`date +%d-%b-%y`
/bin/mkdir -p ${BKP_LOC_DB}
echo "Backup Location is: ${BKP_LOC_DB}"
break
fi
done
# Exit if the user has pressed Ctrl+D:
if [ ! -w "${LOC1}" ]; then
exit
fi
esac
# Setting a Verifier:
echo ""
echo "Shall we go ahead with CLEANING UP LOGS and TRACES of Database \"${ORACLE_SID}\" and its Listener: [Y|N] Y"
echo "==========================================================================================="
while read ANS
do
case $ANS in
""|y|Y|yes|YES|Yes) break ;;
n|N|NO|no|No) echo; echo "Script Terminated !";echo; exit; break ;;
*) echo; echo "Please enter a VALID answer [Y|N]" ;;
esac
done
echo ""
echo "Do you want to Cleanup Database Audit Logs: [Y|N] Y"
echo "=========================================="
while read ANS
do
case $ANS in
""|y|Y|yes|YES|Yes) echo;export AUDIT_FILES_CLEANUP=Y ;break ;;
n|N|NO|no|No) echo;export AUDIT_FILES_CLEANUP=N ;break ;;
*) echo;echo "Please enter a VALID answer [Y|N]" ;;
esac
done
INS=${ORACLE_SID}
export INS
# ######################
# Getting Listener name:
# ######################
LSNR_COUNT=$( ps -ef|grep tnslsnr|grep -v grep|wc -l )
if [ ${LSNR_COUNT} -eq 1 ]
then
LSNR_NAME=$( ps -ef|grep tnslsnr|grep -v grep|awk '{print $(9)}' )
else
LSNR_NAME=$( ps -ef|grep tnslsnr|grep -i "${ORACLE_SID} "|grep -v grep|awk '{print $(9)}' )
fi
if [ -z "${LSNR_NAME}" ]
then
LSNR_NAME=LISTENER
fi
LISTENER_NAME=${LSNR_NAME}
# #######################
# Backup & Delete DB logs:
# #######################
# Exit if DUMP/BDUMP/CDUMP variables are NULL:
if [ -z "${DUMP}" ]
then
echo "DUMP variable is NULL."
echo "Script Terminated."
exit
fi
if [ -z "${BDUMP}" ]
then
echo "BDUMP variable is NULL."
echo "Script Terminated."
exit
fi
if [ -z "${CDUMP}" ]
then
echo "CDUMP variable is NULL."
echo "Script Terminated."
exit
fi
# Exit if DUMP/BDUMP/CDUMP locations are not ACCESSIBLE:
if [ ! -d ${DUMP} ]
then
echo "The Parent Log DUMP location cannot be Located!"
echo
exit
fi
if [ ! -d ${BDUMP} ]
then
echo "The Log BDUMP location cannot be Located!"
echo
exit
fi
if [ ! -d ${CDUMP} ]
then
echo "The Log CDUMP location cannot be Located!"
echo
exit
fi
echo "Backing up & removing DB & Listener Logs ..."
sleep 1
tail -1000 ${ALERTDB} > ${BDUMP}/alert_${INS}.log.keep
echo
echo -e "\033[32;5mAlertlog Cleanup\033[0m"
echo "Zipping the Alertlog ..."
gzip -f9 ${BDUMP}/alert_${INS}.log
${HASH_BKP} echo "Backing up the Alertlog ..."
${HASH_BKP} mv -f ${BDUMP}/alert_${INS}.log.gz ${BKP_LOC_DB}
echo "Rotating the Alertlog ..."
mv -f ${BDUMP}/alert_${INS}.log.keep ${BDUMP}/alert_${INS}.log
#tar zcvfP ${BKP_LOC_DB}/${INS}-dump-logs.tar.gz ${DUMP}
${HASH_BKP} echo "Backing up ${DUMP} ..."
#find ${DUMP} -name '*' -print > ${BKP_LOC_DB}/dump_files_list.txt
#tar zcfP ${BKP_LOC_DB}/${INS}-dump-logs.tar.gz --files-from ${BKP_LOC_DB}/dump_files_list.txt
cd ${DUMP}
${HASH_BKP} tar zcfP ${BKP_LOC_DB}/${INS}-dump-logs.tar.gz *
# Delete DB logs older than 5 days:
echo
echo -e "\033[32;5mDatabase Logs Cleanup\033[0m"
echo "Deleting DB logs older than 5 days under ${BDUMP} ..."
find ${BDUMP} -type f -name '*.trc' -mtime +5 -exec rm -f {} \;
find ${BDUMP} -type f -name '*.trm' -mtime +5 -exec rm -f {} \;
find ${BDUMP} -type f -name '*.log' -mtime +5 -exec rm -f {} \;
echo "Deleting DB logs older than 5 days under ${DUMP}/alert ..."
find ${DUMP}/alert -type f -name '*.xml' -mtime +5 -exec rm -f {} \;
echo "Deleting DB logs older than 5 days under ${DUMP}/incident ..."
find ${DUMP}/incident -type f -name '*.trc' -mtime +5 -exec rm -f {} \;
find ${DUMP}/incident -type f -name '*.trm' -mtime +5 -exec rm -f {} \;
find ${DUMP}/incident -type f -name '*.log' -mtime +5 -exec rm -f {} \;
echo "Deleting DB logs older than 5 days under ${CDUMP} ..."
find ${CDUMP} -type f -name '*.trc' -mtime +5 -exec rm -f {} \;
find ${CDUMP} -type f -name '*.trm' -mtime +5 -exec rm -f {} \;
find ${CDUMP} -type f -name '*.log' -mtime +5 -exec rm -f {} \;
# Backup & Delete listener's logs:
# ################################
#LISTENER_HOME=`ps -ef|grep -v grep|grep tnslsnr|grep -i ${LSNR_NAME}|awk '{print $(NF-2)}' |sed -e 's/\/bin\/tnslsnr//g'|grep -v sed|grep -v "s///g"|head -1`
echo
echo -e "\033[32;5mListener Logs Cleanup\033[0m"
LISTENER_HOME=`ps -ef|grep -v grep|grep tnslsnr|grep "${LSNR_NAME} "|awk '{print $(8)}' |sed -e 's/\/bin\/tnslsnr//g'|grep -v sed|grep -v "s///g"|head -1`
TNS_ADMIN=${LISTENER_HOME}/network/admin
export TNS_ADMIN
LISTENER_LOGDIR=`${LISTENER_HOME}/bin/lsnrctl status ${LISTENER_NAME}|grep "Listener Log File"| awk '{print $NF}'| sed -e 's/\/alert\/log.xml//g'`
LISTENER_LOG=${LISTENER_LOGDIR}/trace/${LISTENER_NAME}.log
echo LISTENER_NAME: $LISTENER_NAME
echo LISTENER_HOME: $LISTENER_HOME
echo TNS_ADMIN: $TNS_ADMIN
# Determine if the listener name is in Upper/Lower case:
if [ ! -r ${LISTENER_LOG} ]
then
# Listner_name is lowercase:
LISTENER_NAME=$( echo ${LISTENER_NAME} | awk '{print tolower($0)}' )
export LISTENER_NAME
LISTENER_LOG=${LISTENER_LOGDIR}/trace/${LISTENER_NAME}.log
export LISTENER_LOG
if [ ! -r ${LISTENER_LOG} ]
then
# Listener_name is Uppercase:
LISTENER_NAME=$( echo "${LISTENER_NAME}" | awk '{print toupper($0)}' )
export LISTENER_NAME
LISTENER_LOG=${LISTENER_LOGDIR}/trace/${LISTENER_NAME}.log
export LISTENER_LOG
echo LISTENER_LOG: $LISTENER_LOG
else
echo LISTENER_LOG: $LISTENER_LOG
fi
fi
# Exit if LISTENER LOG directory is NULL:
if [ -z "${LISTENER_LOGDIR}" ]
then
echo "LISTENER_LOGDIR variable is NULL."
echo "Script Terminated."
exit
fi
# Exit if LISTENER LOG directory is IN-ACCESSIBLE:
if [ ! -d ${LISTENER_LOGDIR} ]
then
echo 'Listener Logs Location Cannot be Found!'
echo "Script Terminated."
exit
fi
${HASH_BKP} echo "Backing up listener logs under: ${LISTENER_LOGDIR}/trace ..."
${HASH_BKP} cd ${LISTENER_LOGDIR}/trace
${HASH_BKP} tar zcfP ${BKP_LOC_DB}/${LISTENER_NAME}_trace.tar.gz *
${HASH_BKP} echo "Backing up listener logs under: ${LISTENER_LOGDIR}/alert ..."
${HASH_BKP} cd ${LISTENER_LOGDIR}/alert
${HASH_BKP} tar zcfP ${BKP_LOC_DB}/${LISTENER_NAME}_alert.tar.gz *
tail -10000 ${LISTENER_LOGDIR}/trace/${LISTENER_NAME}.log > ${LISTENER_LOGDIR}/${LISTENER_NAME}.log.keep
echo "Deleting listener logs older than 5 days under: ${LISTENER_LOGDIR}/trace ..."
find ${LISTENER_LOGDIR}/trace -type f -name '*.trc' -mtime +5 -exec rm -f {} \;
find ${LISTENER_LOGDIR}/trace -type f -name '*.trm' -mtime +5 -exec rm -f {} \;
find ${LISTENER_LOGDIR}/trace -type f -name '*.log' -mtime +5 -exec rm -f {} \;
echo "Deleting listener logs older than 5 days under: ${LISTENER_LOGDIR}/alert ..."
find ${LISTENER_LOGDIR}/alert -type f -name '*.xml' -mtime +5 -exec rm -f {} \;
echo "Rotating listener log ${LISTENER_LOGDIR}/trace/${LISTENER_NAME}.log ..."
mv -f ${LISTENER_LOGDIR}/${LISTENER_NAME}.log.keep ${LISTENER_LOGDIR}/trace/${LISTENER_NAME}.log
# ############################
# Backup & Delete AUDIT logs:
# ############################
# Getting Audit Files Location:
# ############################
case ${AUDIT_FILES_CLEANUP} in
Y)
echo
echo -e "\033[32;5mAudit Logs Cleanup\033[0m"
VAL_AUD=$(${ORACLE_HOME}/bin/sqlplus -S "/ as sysdba" <<EOF
set pages 0 feedback off;
prompt
SELECT value from v\$parameter where NAME='audit_file_dest';
exit;
EOF
)
AUD_LOC=`echo ${VAL_AUD} | perl -lpe'$_ = reverse' |awk '{print $1}'|perl -lpe'$_ = reverse'`
# Exit if AUDIT LOG variable is NULL:
if [ -z "${AUD_LOC}" ]
then
echo "AUD_LOC variable is NULL."
echo "Script Terminated."
exit
fi
if [ -d ${AUD_LOC} ]
then
#tar zcvfP ${BKP_LOC_DB}/audit_files.tar.gz ${AUD_LOC}/${ORACLE_SID}_*
#find ${AUD_LOC} -type f -name '${ORACLE_SID}_*.aud' -print > ${BKP_LOC_DB}/audit_files_list.txt
#tar zcvfP ${BKP_LOC_DB}/${INS}-audit-logs.tar.gz --files-from ${BKP_LOC_DB}/audit_files_list.txt
${HASH_BKP} echo "Backing up Audit files under: ${AUD_LOC} ..."
${HASH_BKP} cd ${AUD_LOC}
${HASH_BKP} tar zcfP ${BKP_LOC_DB}/${INS}-audit-logs.tar.gz ${ORACLE_SID}_*.aud
# Delete Audit logs older than 5 days
echo "Deleting Audit files older than 5 days under: ${AUD_LOC} ..."
find ${AUD_LOC} -type f -name "${ORACLE_SID}_*.aud" -mtime +5 -exec rm -f {} \;
else
# Exit if AUDIT LOG directory is IN-ACCESSIBLE:
echo "Audit Files Location Cannot be Found!"
exit
fi
;;
esac
echo ""
echo "------------------------------------"
${HASH_BKP} echo "Old logs are backed up under: ${BKP_LOC_DB}"
echo "The Last 5 Days Logs are KEPT."
echo "CLEANUP COMPLETED."
echo "------------------------------------"
echo
# De-Neutralize login.sql file:
# ############################
# If login.sql was renamed during the execution of the script revert it back to its original name:
if [ -f ./login.sql_NeutralizedBy${SCRIPT_NAME} ]
then
mv ./login.sql_NeutralizedBy${SCRIPT_NAME} ./login.sql
fi
if [ -f ${USR_ORA_HOME}/login.sql_NeutralizedBy${SCRIPT_NAME} ]
then
mv ${USR_ORA_HOME}/login.sql_NeutralizedBy${SCRIPT_NAME} ${USR_ORA_HOME}/login.sql
fi
# #############
# END OF SCRIPT
# #############
# REPORT BUGS to: <mahmmoudadel@hotmail.com>.
# DISCLAIMER: THIS SCRIPT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY. IT IS PROVIDED "AS IS".
# DOWNLOAD THE LATEST VERSION OF DATABASE ADMINISTRATION BUNDLE FROM:
# http://dba-tips.blogspot.com/2014/02/oracle-database-administration-scripts.html
view raw oracle_cleanup hosted with ❤ by GitHub


4 comments:

  1. Dear Script not available on provided links
    Thanks

    ReplyDelete
  2. Dear Md. Tanweer,

    Thanks a lot for your comment.

    I've updated the links to a good working ones.

    Thanks again and sorry for any inconvenience caused.

    Mahmmoud ADEL

    ReplyDelete
  3. I need to find a list of ""very essential"" user/schemas off oracle database. want to delete unnecessary users in order to gain more space and just keep the application data (which I know about users / schemas). However, I cannot find a list of what is needed and / or what can be deleted.
    can you give me a list or documentation that has this information?

    ReplyDelete
    Replies
    1. Hi Carlos,

      What are you trying to do is very critical, only Oracle Support can help you with that.

      Delete