#!/usr/bin/ksh
#File-name: purge_files.sh
#---------------------------------------------------------------------
# Author: Larry Roberts 2/12/2004
# E-mail: lroberts@tandbergtv.com
# W-page: http://www.tandbergtv.com
# Modified by: Larry Roberts 4/12/2004
#---------------------------------------------------------------------
# Notes: REMOVES these files
#        .aud, audit files created by connect internal
#        .trc, trace files
#        core, core dump files
#        old archive logs
#        alert*.log every 60 days
#        listener.log every 60 days
# Should be run in the following way in crontab
# 0 11 * * * /usr/local/n2bb/backup/scripts/purge_files.sh
#---------------------------------------------------------------------
#
#---------------------------------------------------------------------
#--- Info highlighted like this is the start of a function -----------
#---------------------------------------------------------------------
#
#----------------------------------------------
# Info highlighted like this is for doco only
#----------------------------------------------

#-----------------------------------------------------------
#  These values can be changed depending on your environment
#-----------------------------------------------------------
other_log_days=30
#retention days or for archivelogs and should be set to same
#value as in the rman_backup.ksh script
retention_days=14
backup_home=/usr/local/n2bb/backup
#---------------------------------------------------------
#---------------------------------------------------------

if test -f /var/opt/oracle/oratab
then
    ORATAB=/var/opt/oracle/oratab
else
    ORATAB=/etc/oratab
fi

TMP=/tmp/tmp$$.lst

BACKUP_DIR=$backup_home
l_date=`date '+%Y%m%d'`
l_time=`date`

l_log=${BACKUP_DIR}/purge_${l_date}.log

#---------------------------------------------------------------------
#--- Determine location of Archive logs ------------------------------
#---------------------------------------------------------------------
function get_archive_location
{
echo "Determing archive location"
echo "Determing archive location" >> $l_log
sqlplus /nolog  <<EOF
        connect / as sysdba
        set termout off

        spool ${TMP}
        select 'ARCH_DEST='||value DEST FROM v\$parameter where name = 'log_archive_dest';
        spool off;

EOF

SED="sed -e '/^SQL/d' -e '/^DEST/d' -e '/^VALUE/d' -e '/^---.*--$/d' \${TMP}"
eval $SED | while read FILE
do
    export $FILE
done
echo "Archive log location is ${ARCH_DEST}"
echo "Archive log location is ${ARCH_DEST}" >> $l_log
rm $TMP

} # END get_archive_location


#---------------------------------------------------------------------
#--- Determine location of trace and other files ---------------------
#---------------------------------------------------------------------
function get_other_location
{
echo "Determining other locations `date`" >> $l_log
sqlplus /nolog <<EOF
        connect / as sysdba
        set termout off
        set pagesize 0
        set heading off
        set feedback off

        spool ${TMP}
        select value FROM v\$parameter WHERE name = 'user_dump_dest';
        spool off;
EOF

SED="sed -e '/^SQL/d' -e '/^DEST/d' -e '/^ /d' -e '/^---.*--$/d' \${TMP}"
eval $SED | while read FILE
do
     DIRUSER=$FILE
done
echo "User dump log location is ${DIRUSER}"
echo "User dump log location is ${DIRUSER}" >> $l_log
rm $TMP

sqlplus /nolog <<EOF1
        connect / as sysdba
        set termout off
        set pagesize 0
        set heading off
        set feedback off

        spool ${TMP}
        select value FROM v\$parameter WHERE name = 'core_dump_dest';
        spool off;
EOF1

SED="sed -e '/^SQL/d' -e '/^VALUE/d' -e '/^ /d' -e '/^---.*--$/d' \${TMP}"
eval $SED | while read FILE
do
     DIRCORE=$FILE
done
echo "User core dumps location is ${DIRCORE}"
echo "User core dumps location is ${DIRCORE}" >> $l_log
rm $TMP

sqlplus /nolog <<EOF
        connect / as sysdba
        set termout off
        set pagesize 0
        set heading off
        set feedback off

        spool ${TMP}
        select value FROM v\$parameter WHERE name = 'background_dump_dest';
        spool off;
EOF

SED="sed -e '/^SQL/d' -e '/^VALUE/d' -e '/^ /d' -e '/^---.*--$/d' \${TMP}"
eval $SED | while read FILE
do
     DIRBACK=$FILE
done
echo "User background dumps location is ${DIRBACK}"
echo "User background dumps location is ${DIRBACK}" >> $l_log
rm $TMP

sqlplus /nolog  <<EOF
        connect / as sysdba
        set termout off
        set pagesize 0
        set heading off
        set feedback off

        spool ${TMP}
        select value FROM v\$parameter WHERE name = 'audit_file_dest';
        spool off;
EOF

SED="sed -e '/^SQL/d' -e '/^VALUE/d' -e '/^ /d' -e '/^---.*--$/d' \${TMP}"
eval $SED | while read FILE
do
     DIRAUDIT=$FILE
done
echo "User audit file location is ${DIRAUDIT}" >> $l_log
rm $TMP

} # END get_other_location

#---------------------------------------------------------------------
#--- Purge unwanted files older than one week or two months ----------
#---------------------------------------------------------------------
function funct_purge_files
{
echo "Purging audit files that are more than $other_log_days days OLD - started `date`" >> $l_log
 find $DIRAUDIT -follow -name "*.aud" -mtime +$other_log_days -exec ls -l {} \; >> $l_log
 find $DIRAUDIT -follow -name "*.aud" -mtime +$other_log_days -exec rm -r {} \;
echo "Purging trace files that are more than $other_log_days days OLD - started `date`" >> $l_log
 find $DIRBACK -follow -name "*.trc" -mtime +$other_log_days -exec ls -l {} \; >> $l_log
 find $DIRBACK -follow -name "*.trc" -mtime +$other_log_days -exec rm -r {} \;
 find $DIRUSER -follow -name "*.trc" -mtime +$other_log_days -exec ls -l {} \; >> $l_log
 find $DIRUSER -follow -name "*.trc" -mtime +$other_log_days -exec rm -r {} \;
echo "Purging coredump files that are more than $other_log_days days OLD - started `date`" >> $l_log
 find $DIRCORE -follow -name "core*" -mtime +$other_log_days -exec ls -l {} \; >> $l_log
 find $DIRCORE -follow -name "core*" -mtime +$other_log_days -exec rm -r {} \;
echo "Purging archive logs that are more than $retention_days days OLD - started `date`" >> $l_log
 find $ARCH_DEST/ -follow -name "*.ARC" -mtime +$retention_days -exec ls -l {} \; >> $l_log
 find $ARCH_DEST/ -follow -name "*.ARC" -mtime +$retention_days -exec rm -r {} \;
 find $ARCH_DEST/ -follow -name "*.arc" -mtime +$retention_days -exec ls -l {} \; >> $l_log
 find $ARCH_DEST/ -follow -name "*.arc" -mtime +$retention_days -exec rm -r {} \;
echo "Purging alert*.log and listener.log that are more than 60 days OLD - started `date`" >> $l_log
 find /home/oracle/admin/$ORACLE_SID/bdump -follow -name "alert*.log" -mtime +60 -exec ls -l {} \; >> $l_log
 find /home/oracle/admin/$ORACLE_SID/bdump -follow -name "alert*.log" -mtime +60 -exec rm -r {} \;
 find $ORACLE_HOME/network/log -follow -name "listener.log" -mtime +60 -exec ls -l {} \; >> $l_log
 find $ORACLE_HOME/network/log -follow -name "listener.log" -mtime +60 -exec rm -r {} \;

 find $backup_home -follow -name "*.log" -mtime +7 -exec rm -r {} \;
 find $backup_home/ARCH -follow -name "arch_backup*" -mtime +$retention_days -exec rm -r {} \;

} # END funct_purge_files

############################################################
# things to do before exiting.  any exit from the program  #
# should be done by calling do_exit with a numeric argument#
# a non-numeric argument (or none at all) will generate an #
# error of its own, so be careful                          #
############################################################
function do_exit
{
    exit $1

} #END do_exit

#::::::::::::::::::::::::::
#  Main
#::::::::::::::::::::::::::
#---------------------------------------------------------------------
#--- Purge unwanted files --------------------------------------------
#---------------------------------------------------------------------


cat $ORATAB | while read LINE
do
    case $LINE in
        \#*)                ;;        #comment-line in oratab
        *)
#       Proceed only if third field is 'Y'.
        if [ "`echo $LINE | awk -F: '{print $3}' -`" = "Y" ] ; then
            ORACLE_SID=`echo $LINE | awk -F: '{print $1}' -`
            if [ "$ORACLE_SID" = '*' ] ; then
                ORACLE_SID=""
            fi
#           Called programs use same database ID
            export ORACLE_SID
            ORACLE_HOME=`echo $LINE | awk -F: '{print $2}' -`
#           Called scripts use same home directory
            export ORACLE_HOME
#           Put $ORACLE_HOME/bin into PATH and export.
            PATH=$ORACLE_HOME/bin:/bin:/usr/bin:/etc ; export PATH
            LD_LIBRARY_PATH=${ORACLE_HOME}/lib ; export LD_LIBRARY_PATH
            get_archive_location
            get_other_location
            funct_purge_files
echo "-----------------------------------------------------------------------------------------" >> $l_log
echo "-----------------------completed for $ORACLE_SID --------------------`date`" >> $l_log
echo "-----------------------------------------------------------------------------------------" >> $l_log
        fi
    esac
done


do_exit 0
#-----------------------------------------------------
# End of file

