#!/usr/bin/env bash
#
# INTRO : The script for delete physical standby applied archivelog.
# Please set ur environment variables before use it.
# Please execute the script on physical standby site.
#
# USAGE : ./del_archivelog.sh
#
# TEST : This script has been successfully tested on these platforms:
# Linux
# Physical standby
# Oracle Database 10gR2,11gR2,Include RAC
#
# NOTE : Please test this script in ur development environment
# before attempting to run it in production.
# =================================================================================
#----------------------------------------------------------------------------------
###setup environment variables
export ORACLE_HOME=/oracle/app/oracle/product/11.2.0/db_1
export ORACLE_SID=standby
export PATH=$ORACLE_HOME/bin:$PATH
#export ARCHIVE_DIR=+DATA/standby/archivelog
export LOG_FILE=$HOME/scripts/logs/del_archive.log
#----------------------------------------------------------------------------------
###determine user
if [ `whoami` != 'oracle' ];then
echo "Warning: Please use oracle execute.">>$LOG_FILE
exit 99
fi
###define archivelog sequence will be deleted
sqlplus -s / as sysdba << EOF > tmp.log
set lines 100 feedback off echo off heading off;
select thread#,max(sequence#) from v$archived_log where applied='YES' group by thread# order by thread#;
EOF
MAXLINE=`cat tmp.log|wc -l`
for (( i=1;i<$MAXLINE;i++ )); do
i=$(( i + 1 ))
THREAD=`sed -n "$i,$i"p tmp.log|awk -F' ' '{print $1}'`
MAXSEQ=`sed -n "$i,$i"p tmp.log|awk -F' ' '{print $2}'`
###Retains the most recent five Archive
MAXSEQ=$(( $MAXSEQ - 5 ))
###Delete physical standby applied archivelog
echo "****************************************************************************" >> $LOG_FILE
echo ">>> Begin deleting applied archivelogs : `date` <<<">>$LOG_FILE
i=$(( i - 1 ))
rman target / <<EOF >> $LOG_FILE
##catalog start with '$ARCHIVE_DIR' noprompt;
delete noprompt archivelog until sequence $MAXSEQ thread $THREAD;
EOF
echo >> $LOG_FILE
echo ">>> End delete applied archivelogs : `date` <<<">>$LOG_FILE
echo "****************************************************************************" >> $LOG_FILE
echo >> $LOG_FILE
done
rm -f tmp.log