Script to delete RMAN backupsets older than a specified number of days

DAYSTOKEEP=60

# -- Do not change anything below this line ---------------------------

CMDFILE=/tmp/rmanpurge$$.rcv
LOGFILE=/tmp/rmanprige$$.log

if [ ! -x $ORACLE_HOME/bin/rman ]; then
   echo "ERROR: RMAN not found or ORACLE_HOME not set."
   exit 8
fi
if [ ! $ORACLE_SID ]; then
   echo "ERROR: ORACLE_SID not set."
   exit 8
fi
if [ ! $1 ]; then
   echo "USAGE: $0 CatalogConnectString"
   exit;
else
   RCVCAT=$1
fi;

echo "Delete RMAN backupsets older than $DAYSTOKEEP days for db $ORACLE_SID."
echo "ALLOCATE CHANNEL FOR DELETE TYPE 'SBT_TAPE';" >>$CMDFILE

# List all old entries that needs to be deleted
(rman rcvcat $RCVCAT target / <<-EOF
        list backupset of database
                from time 'SYSDATE-3000' until time 'SYSDATE-$DAYSTOKEEP';
        exit;
        EOF
) | grep RMAN-06233 | while read filler key filler filler date rest
do
        echo "# Delete backupset $key dated $date..." >>$CMDFILE
        echo "CHANGE BACKUPSET $key DELETE;"          >>$CMDFILE
done

echo "RELEASE CHANNEL;" >>$CMDFILE

# Delete the old entries
rman rcvcat $RCVCAT target / cmdfile $CMDFILE

echo "Done!"
  • March 10, 2019 | 18 views
  • Comments