본문 바로가기
Database/Oracle

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

by DBTechBiz 2023. 11. 24.

 

 


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

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


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

# Crontab
# delete archive logs when it's over 20%
0,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 BASE_DIR='/dbwork/script/archive'
export LOG_DIR=$BASE_DIR/log
export ORACLE_UNIQUE_SID=ORAP

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

#Number of log files remaining
MAX_ARCHIVE_COUNT=10

su - grid <<EOF
TOTAL_SIZE=\$(asmcmd lsdg | grep "ARCH" | awk 'NR==1{print \$8}')
USED_SIZE=\$(asmcmd du +ARCH | /usr/bin/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 | /usr/bin/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 ""
else
  echo "Disk usage is below 20%. No action required."
fi
EOF

#Delete expired archive logs.
echo "Delete expired archive logs."
su - oracle <<EOF
rman target /
CROSSCHECK ARCHIVELOG ALL;
DELETE EXPIRED ARCHIVELOG ALL;
YES
exit
EOF
반응형

댓글