Shell script to delete old archive logs in RAC databases

Today, I will be sharing one handy shell script that can be used to delete old archive logs. Considering the archive logs are already backed up and not required for any recovery purpose. In the following example i will be deleting archive that are 5 days old.

vi archive_del.sh

#!/bin/bash
 
# Set the number of days
DAYS_OLD=5
 
# Function to delete archive logs older than specified days for a given ORACLE_SID
delete_old_archivelogs() {
  ORACLE_SID=$1
  export ORACLE_SID
 
  echo “Deleting archive logs older than $DAYS_OLD days for database $ORACLE_SID…”
 
  rman target / <<EOF
    CROSSCHECK ARCHIVELOG ALL;
    DELETE NOPROMPT ARCHIVELOG ALL COMPLETED BEFORE ‘SYSDATE-$DAYS_OLD’;
    EXIT;
EOF
 
  if [ $? -eq 0 ]; then
    echo “Archive logs older than $DAYS_OLD days deleted successfully for database $ORACLE_SID.”
  else
    echo “Failed to delete archive logs for database $ORACLE_SID.”
  fi
}
 
# Source the Oracle environment variables
source /usr/local/bin/oraenv
 
# Get the list of running Oracle database instances (ORACLE_SID) excluding ASM
ORACLE_INSTANCES=$(ps -ef | grep pmon | grep -v grep | grep -v asm | awk ‘{print $9}’ | cut -d’_’ -f3)
 
# Loop through each instance and delete old archive logs
for SID in $ORACLE_INSTANCES; do
  delete_old_archivelogs $SID
done

Schedule the above script in crontab as per your requirement.


Categories

Leave a Reply

Your email address will not be published. Required fields are marked *