원문 : http://www.ischo.net -- 조인상 // 시스템 엔지니어

Writer : http://www.ischo.net -- ischo // System Engineer in Replubic Of Korea

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

원문 : http://www.ischo.net -- 조인상 //시스템 엔지니어

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

스크립트 : chk_arc.sh

목적 : 오라클 아카이브 로그가 빈번하게 쌓이고 파일시스템 여분이 충분치 않은 시스템에서

           파일시스템이 50% 이상 넘어갔을시에 아카이브로그를 아바마로 백업후 백업한 로그파일만 삭제

           작업이 완료된 후 해당 내용을 SMS DB로 넣어서 문자메시지로 관리자가 받을수 있도록 한다.

 

 

 

#!/usr/bin/ksh

# script by ischo
# http://www.ischo.net
# chosim1@naver.com

# Shell script : Check Archived-log filesystem and backup

#############################################################################
# ENVIRONMENT SETTING
#############################################################################

# For Shell
export LANG=C
SH_HOME=/home/admin/chk_arc
ARC_DIR="/oracle/arc"
LOG=${SH_HOME}/chkarc_`date +%Y%m%d`.log
AVR_LOG=${SH_HOME}/avr.log

# For Avamar
BIN_DIR=/opt/AVMRclnt/bin
ACCOUNT=/domain_name/host_name                                     /* 아바마 형식에 맞춰   /도메인명/호스트명  형식으로 기입 */
ID=Avamar_ID                                                                           /* 아바마 ID */
AP=Avamar_Password                                                              /* 아바마 패스워드 */

# TIME mark VARS
STIME=`date +%Y%m%d%H%M%S`
TTIME=`date +%H:%M`
DDATE=`date +%Y%m%d`

# For SMS shoot #########################################
# SQL file for insert into DB
SQLFILE=${SH_HOME}/send_sms.sql

# Phone Number to Send
# PHN1 - Cho In Sang
PHN1=01012341234

# Phone Number for Sender
SEND_PHN=0421231234

# ServerName : This setting will be included in SMS
SERVERNAME=`hostname`

# DB user/group : OS user/group for ORACLE
DBUSER=oracle
DBGRP=dba
#########################################################

 

#############################################################################
#############################################################################

#############################################################################
# Function
#############################################################################

# Excuting SQL into EMS DB
function Func_InsertDB
{
su - $DBUSER -c "sqlplus dbuser/dbpassword@SMSDB << !
start $SQLFILE
exit
!"
}

 

function Func_MakeSQL
{
#echo "$SQLFILE inserted!"
cat /dev/null > $SQLFILE

for TARGET_SEND in `echo $PHN1;`
do

echo "Insert into sms.sms_sktelink (CMP_MSG_ID, CMP_USR_ID, ODR_FG, SMS_GB, USED_CD, MSG_GB, WRT_DTTM, SND_DTTM, SND_PHN_ID, RCV_PHN_ID, CALLBACK, SND_MSG, CMP_SND_DTTM, EXPIRE_VAL, SMS_ST, RSLT_VAL, RSRVD_ID, RSRVD_WD) Values (sms.SKTELINK_SEQ.nextval, '00000', '2', '1', '00 ', 'A','"$STIME"','"$STIME"','"$SEND_PHN"','"$TARGET_SEND"','"$SEND_PHN"','"$SEND_MSG"','"$STIME"', 0, '0', 99, '', '');" >> $SQLFILE
echo "commit;" >> $SQLFILE

done

chown $DBUSER:$DBGRP $SQLFILE
}


function Func_Backup
{
PRO_CHK=`ps -ef | grep avtar.bin | grep -v grep |wc -l`

if [ ${PRO_CHK} -eq 0 ];
then
        cat /dev/null > ${AVR_LOG}
        ls -l ${ARC_DIR} | grep .arc | awk '{print $9}' > ${SH_HOME}/list.txt
        echo "${DDATE} ${TTIME}  Archived log Backup Start" >> ${LOG}

        ## Backup Archived-log files ##########################################################
        ${BIN_DIR}/avtar -c --expires=14D --label=hostname_arch_50pct --id=${ID} --ap=${AP} --account=${ACCOUNT} --logfile=${AVR_LOG} ${ARC_DIR}
        sleep 3
        ########################################################################################
        TTIME=`date +%H:%M`
        DDATE=`date +%Y%m%d`
        echo "${DDATE} ${TTIME}  Archived log Backup Finished." >> ${LOG}

        ## Delete Archived-log files ##########################################################
        CHK_SUCCESS=`cat ${AVR_LOG} | grep "Command completed (exit code 0: Success)" | wc -l`

        if [ ${CHK_SUCCESS} -ge 1 ];
        then
                echo "${DDATE} ${TTIME}  Delete Archive log file" >> ${LOG}
                for i in `cat ${SH_HOME}/list.txt`
                do
                echo "-- Delete Arc Log file name : ${i}" >> $LOG
                rm -f ${ARC_DIR}/${i}
                done
                echo "${DDATE} ${TTIME}  Delete Archived log Complete!" >> ${LOG}
                echo "--------------------------------------------------------------" >> ${LOG}
                ARC_PCT=`bdf | grep ${ARC_DIR} | grep -v grep | awk '{print $5}'`
                ARC_PCT=`echo ${ARC_PCT%'%'}`
                SEND_MSG=`echo "${SERVERNAME}:Arclog Backup success(${ARC_PCT}%),delete arc-logs. ${TTIME}"`
                Func_MakeSQL
                Func_InsertDB
        else
                echo "${DDATE} ${TTIME}  Backup ended, but can't delete Archived log" >> ${LOG}
                ARC_PCT=`bdf | grep ${ARC_DIR} | grep -v grep | awk '{print $5}'`
                ARC_PCT=`echo ${ARC_PCT%'%'}`
                SEND_MSG=`echo "${SERVERNAME}:Arclog Backup Fail(${ARC_PCT}%),not delete arc-logs. ${TTIME}"`
                Func_MakeSQL
                Func_InsertDB
                exit 0

        fi
        ########################################################################################

 

else
        echo "${DDATE} ${TTIME}  Backup is canceled cause already Started!" >> ${LOG}
fi

}

#############################################################################
#############################################################################


#############################################################################
# Main Procedure
#############################################################################

ARC_PCT=`bdf | grep ${ARC_DIR} | grep -v grep | awk '{print $5}'`
ARC_PCT=`echo ${ARC_PCT%'%'}`


#--- Check File System. If Free_Space is small, Execute Backup.
if [ ${ARC_PCT} -ge 50 ];
then

        Func_Backup

        if [ $? -ne 0 ];
        then
                exit 0
        fi

else
        echo "${DDATE} ${TTIME}  ${ARC_DIR} < 50% : ${ARC_PCT}%" >> ${LOG}
fi

find ${SH_HOME} -name "chkarc*.log" -type f -mtime +5 -exec rm {} \;

서버에 요청 중입니다. 잠시만 기다려 주십시오...