rman備份指令碼 傳送郵件通知

raysuen發表於2016-08-24
指令碼大部分內容載錄網上,本人僅新增郵件傳送內容。郵件使用sendEmail包實現,需要自己單獨安裝。然後再次封裝。

#!/bin/bash

#use Oracle Env.
source ~/.bash_profile


#Def backupdir
dirname=`date "+%Y-%U"`
lastweek=`date -d "-3 week" +%Y-%U`
backupdir=/mnt/usbp/u01/database/rman_bak/$dirname
rmdir=/mnt/usbp/u01/database/rman_bak/$lastweek
#check backdir
if [ ! -d $backupdir ];then
    mkdir -p $backupdir
fi


#
#Get Time And Set Backup Level.
#
Dayname=`date +%a`
if [ $Dayname = 'Sun' ]; then
        Level=0
   elif [ $Dayname = 'Thu' ];then
        Level=1
   else 
        Level=2
fi

#Wed means wedsday

#
#Define Backup Type.
#
if [ $Level = 0 ]; then  
        BACKUP_TYPE="incremental level 0" 
   elif [ $Level = 1 ]; then      
        BACKUP_TYPE="incremental level 1"
   else
        BACKUP_TYPE="incremental level 2"
fi

#
#Log.
#
logdate=`date +"%Y%m%d"`
logdate2=`date +%F`
logfile=$backupdir/racdb_rman_"$logdate".log
echo `date +'%Y%m%d %T'`' start backup-----' >> $logfile
cat >$logfile <<EOF
#
#This is RMAN Backup log.
#
#Today is $logdate2,$Dayname.The RMAN Backup Level is $Level and Backup type is $BACKUP_TYPE.
#
#RMAN Bakcup Process starting.
EOF

#backup body
rman target / log $logfile append <<EOF
     run{
        allocate channel a1 type disk;
        crosscheck backup;
        delete noprompt expired backup;
        crosscheck archivelog all;
        delete noprompt expired archivelog all;
        backup as compressed backupset $BACKUP_TYPE database  format '${backupdir}/incr${Level}_%T_%d_%s_%p.bak' filesperset=8;
        sql 'alter system archive log current';
        backup as compressed backupset archivelog all format  '${backupdir}/arc_%T_%d_%s_%p.bak' delete all input;
        backup current controlfile format '${backupdir}/ctl_%T_%d_db_%s_%p.bak';
        release channel a1;
        }
EOF
echo `date +'%Y%m%d %T'`' end backup-----' >> $logfile
#delete last week backup.
if [ -d $rmdir ];then

        rm -rf $rmdir
echo `date +'%Y%m%d %T'`' end rm_his-----' >> $logfile
echo 'hello'
fi

#send mail to smc,if it have some error in backing
if [ -f ${logfile} ];then
sum=`cat ${logfile} | grep -E "RMAN-|ORA-"|wc -l`
if [ ${sum} -lg 0 ];then
sh /home/oracle/shell/mail_pl.sh "***.sohu.com" '備份報警' "`cat ${logfile} | grep -E 'RMAN-|ORA-'`" "${logfile}"
fi
fi

exit 0
-----------------------------------------------------------------------------------------------
mail_pl.sh指令碼內容:

#!/bin/bash

SMTP_server='***.sohu.com'
username='*****'
password='****'
from_email_address='***.sohu.com'
to_email_address="$1"
message_subject_utf8="$2"
message_body_utf8="$3"
attachment="$4"
message_subject_gb2312=`iconv -t UTF-8 -f UTF-8 << EOF
$message_subject_utf8
EOF`
[ $? -eq 0 ] && message_subject="$message_subject_gb2312" || message_subject="$message_subject_utf8"

message_body_gb2312=`iconv -t UTF-8 -f UTF-8 << EOF
$message_body_utf8
EOF`
[ $? -eq 0 ] && message_body="$message_body_gb2312" || message_body="$message_body_utf8"

sendEmail='/home/oracle/shell/sendEmail.pl'

if [ -e ${message_body} ];then
$sendEmail -s "$SMTP_server" -xu "$username" -xp "$password" -f "$from_email_address" -t "$to_email_address" -u "$message_subject" -o message-file="$message_body" -a ${attachment} -o message-content-type=text -o message-charset=gb2312
else
$sendEmail -s "$SMTP_server" -xu "$username" -xp "$password" -f "$from_email_address" -t "$to_email_address" -u "$message_subject" -m "$message_body" -a ${attachment} -o message-content-type=text -o message-charset=gb2312
fi

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28572479/viewspace-2123959/,如需轉載,請註明出處,否則將追究法律責任。

相關文章