Article Listing 1 Listing 2 Listing 3 aug2004.tar

Listing 3 rman_purge script: This script removes backup and archive log files that are beyond the specified retention tolerance

#!/bin/sh

. $HOME/oracle_env
USAGE="Usage: backpg_rman <SID> <DAYS>"
#
# Check for proper parameter usage.
#
if [ "$1" = "" ]
then
        echo "Backup purge error - No \$1 parameter specified."
        echo "$USAGE" 1>&2
        exit 2
fi

if [ -r /etc/oratab ]
then
        ORATAB=/etc/oratab
else
        if [ -r /var/opt/oracle/oratab ]
        then
                ORATAB=/var/opt/oracle/oratab
        else
                echo "Can't find any oratab file to read..."
                exit 2
        fi
fi

for ORACLE_SID in `grep -v "^#" $ORATAB | grep -v "^$" | cut -d: -f1`
do
        if [ "$ORACLE_SID" = "$1" ] ; then
        mysid=$1
        fi
done
if [ "$mysid" = "" ]
then
    echo "Backup error - Invalid database specified: $1"
    exit 2
fi

if [ "$2" = "" ]
then 
        echo "Backup purge error - No \$2 parameter specified."
        echo "$USAGE" 1>&2
        exit 2
fi 

# Retrieve the list of active backupset pieces and archived log 
# files that are at least as old as the number of days defined in 
# $2, but which are NOT older than the youngest full (level 0) 
# backup. This guarantees that we don't accidentally delete a 
# full backup or needed archive log through our vigor to clean 
# out the system. The last thing we want to do is assume that
# a full backup exists and blow it away, only to have the system 
# crash...
$ORACLE_HOME/bin/sqlplus -s / <<EOF
whenever sqlerror exit failure;
set feedback off
set heading  off
set lines    9999
set echo     off

spool /tmp/backpg_$ORACLE_SID.lst

   select handle
     from v\$backup_piece
    where completion_time        < sysdate - $2
      and status                 = 'A'
      and deleted                = 'NO'
      and completion_time        < (
   select max(s.completion_time)
     from v\$backup_set          s
,        v\$backup_piece         p
    where s.incremental_level    = 0
      and p.set_stamp            = s.set_stamp
      and p.set_count            = s.set_count
      and p.status               = 'A');

   select name
     from v\$archived_log
    where completion_time        < sysdate - $2
      and status                 = 'A'
      and deleted                = 'NO'
      and completion_time        < (
   select max(s.completion_time)
     from v\$backup_set          s
,        v\$backup_piece         p
    where s.incremental_level    = 0
      and p.set_stamp            = s.set_stamp
      and p.set_count            = s.set_count
      and p.status               = 'A');

spool off
exit
EOF

# The list of files to be removed is spooled. Now get rid of them!
if [ -r /tmp/backpg_$ORACLE_SID.lst ]
then
        #
        # Loop through and remove the files.
        #
        cat /tmp/backpg_$ORACLE_SID.lst | while read LINE
        do
                #
                # Make sure the file exists.
                #
                if [ -r "$LINE" ]
                then
                        rm $LINE     # Remove it.
                fi
        done
        #
        # Clean up after ourselves.
        #
        rm /tmp/backpg_$ORACLE_SID.lst

#
# Now go into RMAN and clean up the catalog.
#
$ORACLE_HOME/bin/rman target / nocatalog <<EOF
allocate channel for maintenance type disk;
crosscheck backup;
change archivelog all crosscheck;
delete noprompt expired backup of database;
delete noprompt expired backup of controlfile;
delete noprompt expired backup of archivelog all;
exit
EOF

        if [ $? -ne 0 ]
        then
                echo "Backup purge error during RMAN delete \
expired backup." fi else echo "/tmp/backpg_$ORACLE_SID.lst not found." fi