본문 바로가기
Database/Oracle

Archive 삭제 #4, archive deletion script based on disk usage using ASM of Linux (Linux ASM 기반의 디스크 사용량에 따른 archive 삭제 스크립트)

by DBTechBiz 2023. 11. 24.

 

 


archive deletion script based on disk usage using ASM of Linux (Linux ASM 기반의 디스크 사용량에 따른 archive 삭제 스크립트)

Linux (OEL) OS 기반의 ASM 을 사용하는 Oracle 에 대한 디스크 사용량에 따른 archive 삭제 스크립트를 소개한다.


crontab registration and archive_del.sh files must be created as grid user.

# Crontab
# delete archive logs when it's over 20%
*/30 * * * * sh /dbwork/script/archive/archive_del.sh > /dbwork/script/archive/log/archive_del_ORAP_$(date +'\%Y\%m\%d_\%H\%M').log
# /dbwork/script/archive/archive_del.sh

# Delete archive logs when it's over 20%
export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_BASE=/u01/app/grid
export ORACLE_HOME=/u01/app/19.0/grid
export ORACLE_TERM=xterm
export BASE_PATH=/usr/sbin:$PATH
export PATH=$ORACLE_HOME/bin:$BASE_PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib

export BASE_DIR='/dbwork/script/archive'
export LOG_DIR=$BASE_DIR/log

export ORACLE_UNIQUE_SID=ORAP
export ORACLE_SID=+ASM1

#Archive Path
ARCHIVE_DIR="+ARCH/${ORACLE_UNIQUE_SID}/ARCHIVELOG"

#Number of log files remaining
MAX_ARCHIVE_COUNT=10

#Disk Usage
TOTAL_SIZE=$(asmcmd lsdg | grep 'ARCH' | awk 'NR==1{print $8}')
USED_SIZE=$(asmcmd du +ARCH  | awk 'NR==2{print $1}')
DISK_USAGE=$(echo "scale=4; $USED_SIZE / $TOTAL_SIZE * 100" | bc | awk '{printf "%.0f", $1}')
echo "Disk Usage : "$DISK_USAGE"%"
asmcmd lsdg | egrep 'State|ARCH'
echo ""

#Delete when it's over 20%
if [ $DISK_USAGE -gt 20 ]; then
  echo "Disk usage is above 20%. Deleting archive logs."
  CURRENT_ARCHIVE_COUNT=$(asmcmd ls $ARCHIVE_DIR/*/thread* | wc -l)
  FILES_TO_DELETE=$((CURRENT_ARCHIVE_COUNT - MAX_ARCHIVE_COUNT))

  #Delete archive logs.
  for file in $(asmcmd ls -t --reverse $ARCHIVE_DIR/*/ | grep thread | head -n $FILES_TO_DELETE); do
        echo "Deleting: $file"
        asmcmd rm $ARCHIVE_DIR/*/$file
  done
  echo "Archive logs deleted."
  echo ""

  TOTAL_SIZE=$(asmcmd lsdg | grep 'ARCH' | awk 'NR==1{print $8}')
  USED_SIZE=$(asmcmd du +ARCH  | awk 'NR==2{print $1}')
  DISK_USAGE=$(echo "scale=4; $USED_SIZE / $TOTAL_SIZE * 100" | bc | awk '{printf "%.0f", $1}')
  echo "Disk Usage : "$DISK_USAGE"%"
  asmcmd lsdg | egrep 'State|ARCH'

  echo ""

#Delete expired archive logs.
echo "Delete expired archive logs."
su - oracle <<EOF
{oracle password}
rman target /
CROSSCHECK ARCHIVELOG ALL;
DELETE EXPIRED ARCHIVELOG ALL;
YES
exit
EOF
else
  echo "Disk usage is below 20%. No action required."
fi
반응형

댓글