#!/bin/bash
#获取linux服务器上/dev/sda7的磁盘空间大小
DISK_SIZE=`df -k|grep /dev/sda7 |awk '{ print substr($5,1,2) }'`
export DISK_SIZE
LANG=zh_CN.UTF-8
export LANG
#判断磁盘空间是否大于85% 如果大于85%就直接退出执行s
if [ $DISK_SIZE -gt 85 ]; then
echo DISK_SIZE is $DISK_SIZE over 85
exit
else
echo DISK_SIZE=$DISK_SIZE
fi
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
#oralce安装的路径
ORACLE_BASE=/opt/app/oracle
export ORACLE_BASE
ORACLE_HOME=/opt/app/oracle/product/10.2.0/db_1
export ORACLE_HOME
#oracle安装的服务名称
ORACLE_SID=metardb
export ORACLE_SID
#BAK_PATH is a path that store your exp file
BAK_PATH="/orabak"
#LOG_FILE is a file that recorde the results of export
LOG_FILE="$BAK_PATH"/`date +%Y%m%d`_brief.log
#Define the exp user
USER=system
#Define the exp user's password
PASSWD=metarnet
#Determine the current user name
CUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`
#Define the retention days of your backup file
DAYS=365
#******************************************************************
#It is unnecessary to edit the the below text in normal condition.*
#******************************************************************
echo "The start time is: "`date` >> $LOG_FILE
echo >> $LOG_FILE
if [ $DISK_SIZE -gt 85 ]; then
echo DISK_SIZE is $DISK_SIZE over 85|tee -a $LOG_FILE
exit
else
echo DISK_SIZE=$DISK_SIZE|tee -a $LOG_FILE
fi
#Define the backup file name
BAK_FILE=metardb_`date +%Y%m%d_%w`_full.dmp
#Define the backup log file name
BAK_LOG=metardb_`date +%Y%m%d_%w`_full.log
#Define your backup command
CMD_STR="
setenv ORACLE_HOME $ORACLE_HOME
exp $USER/$PASSWD@$ORACLE_SID file=$BAK_PATH/$BAK_FILE log=$BAK_PATH/$BAK_LOG full=y
"
#Begin backup
echo "Backup is started" >>$LOG_FILE
echo "......">>$LOG_FILE
if [ "$CUSER" = "root" ]
then
su - oracle -c "$CMD_STR" >> $LOG_FILE
RSTAT=$?
else
/usr/bin/csh -c "$CMD_STR" >> $LOG_FILE
RSTAT=$?
fi
if [ "$RSTAT" = "0" ]
then
echo "Backup to Hard disk is ended successful!" >> $LOG_FILE
else
echo "Notice! Backup hard disk is ended unsuccessful!">> $LOG_FILE
fi
#compress
#yestoday=`date +%D | awk -F/ '{s=$2-2}{printf"20%02d%02d%02d/n", $3,$1,s}'`
cd $BAK_PATH
#CMP_FILE="yujing_"$yestoday"_full.dmp"
echo "Compress "$BAK_FILE >>$LOG_FILE
gzip $BAK_FILE
#Delete 3 days ago file
OLDFILE=metardb_`date -d '3 days ago' +%Y%m%d_%w`_full.dmp.gz
OLDLOG=metardb_`date -d '3 days ago' +%Y%m%d_%w`_full.log
rm -rf /orabak/$OLDFILE
rm -rf /orabak/$OLDLOG
echo >>$LOG_FILE
echo "Backup to Hard disk End time is: "`date`>>$LOG_FILE