wasbakup.sh:备份数据库和应用并上传到FTP

wasbakup.sh:备份数据库和应用并上传到FTP
 
#!/bin/bash
#备份目录变量
BACKUPDIR="/home/WasBackup"
#WAS起停脚本目录
WASBIN="/opt/WebSphere/AppServer/bin"
#IBMHTTP 起停脚本目录
IBMHTTP="/opt/IBMHttpServer/bin"
#WAS用户名和密码
USER="wpsbind"
PASS="welcome"
#生成日志
LOG="WasBackup`date +"%m-%d-%y"`.log"
#生成备份文件
FILE="WasBak`date +"%m-%d-%y"`.tar.z"
ORAFILE="Oradatabak`date +"%m-%d-%y"`.tar.z"
#设置ORACLE参数
ORACLE_HOME=/home/oracle/product/9.2.4
ORADATAFILE=/home/oracle/oradata
ORAALLFILE=/home/oracle
FLAGUSER="sys"
FLAGTNSNAME="wpsdb"
FLAGPASS="welcomeoracle"
WARING="The Db Not Run"
#FTP 设置
IP="122.120.1.90"
FTPUSER="ftpbak"
FTPPASS="welcome"
FTPROOT="coldbak"
FTPDIR=PRODWas`date +"%Y-%m-%d"`
IP1="122.120.1.21"
FTPUSER1="ftpbak"
FTPPASS1="welcome"
FTPDIR1=PRODWas`date +"%Y-%m-%d"`

echo "       ===============================   "
echo "       |     Begin Backup WAS        |   "
echo "       ===============================   "

#JUDGERUN=`ps -ef |grep  WasBackup.sh |grep /bin/bash`
#WHORUN=`ps -ef |grep  WasBackup.sh |grep /bin/bash |awk '{ print $6 }'`
#if [ "${JUDGERUN}" != "" ]; then
#   echo "This shell is already run by IP:`who |grep $WHORUN |awk '{print $6}'`"
#   exit 0
#fi
if [ ! -d  $BACKUPDIR ];then
        mkdir $BACKUPDIR
fi

cd $WASBIN
#./serverStatus.sh -all -username $USER -password $PASS >> $BACKUPDIR/$LOG 2>>&1
echo ".............Stop  All Services.................."
echo "=============STOP server1===================" > $BACKUPDIR/$LOG
./stopServer.sh server1 -username $USER -password $PASS >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
echo "=================END STOP===================" >> $BACKUPDIR/$LOG
echo "" >>$BACKUPDIR/$LOG
echo "=============STOP WebSphere_Portal==========" >> $BACKUPDIR/$LOG
./stopServer.sh WebSphere_Portal -username $USER -password $PASS >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
echo "=============END STOP=======================" >> $BACKUPDIR/$LOG
echo "" >>$BACKUPDIR/$LOG
cd $IBMHTTP
echo "=============STOP IBMHTTPServer=============" >> $BACKUPDIR/$LOG
./apachectl stop >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
echo "=============END STOP=======================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
echo "" >>$BACKUPDIR/$LOG
DBRUNWPSDB=`ps -ef |grep ora_ |awk '{print $8 }' |sed 's/^ora_//g'|sed 's/^.....//g'|grep wpsdb`
if [ "${DBRUNWPSDB}" = "" ]; then
        echo "" >>$BACKUPDIR/$LOG
        echo "*******************************" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        echo $WARING >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        echo "*******************************" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
else
        echo "" >>$BACKUPDIR/$LOG
        echo "======================================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        echo " "
        su - oracle -c "sqlplus /nolog @$ORACLE_HOME/mysql/stopdb.sql $FLAGUSER $FLAGTNSNAME $FLAGPASS" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        echo "======================================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
fi
echo "" >>$BACKUPDIR/$LOG
echo "==============Stop Listener====================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
su - oracle -c "lsnrctl stop" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
echo "==============END==============================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
echo "..............ALL Services Stopped......................"
cd
echo "" >>$BACKUPDIR/$LOG
if [ -f $BACKUPDIR/$FILE ]; then
        echo "The $FILE Exist!!"
else
        echo "...............Starting Backup Was.................."
        echo "============Starting Backup WAS============" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        date +"%Y年-%m月-%d日:%T:Begin backup WAS" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        tar cvfz $BACKUPDIR/$FILE  /opt/IBMHttpServer/ /opt/mqm/  /opt/WebSphere/ /opt/wemps/ /var/mqm/ /var/wemps/ >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        date +"%Y年-%m月-%d日:%T:End  backup " >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        echo "============End Backup Was=================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        echo "...............End WAS Backup........................"
fi
echo "" >>$BACKUPDIR/$LOG
if [ -f $BACKUPDIR/$ORAFILE ]; then
        echo "The $ORAFILE Exist!!"
else
        echo "...............Starting Backup Oracle Datafile............"
        echo "===============Starting Backup Oracle Data================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        date +"%Y年-%m月-%d日:%T:Begin backup Oracle Datafile" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        tar cvfz $BACKUPDIR/$ORAFILE $ORADATAFILE >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        date +"%Y年-%m月-%d日:%T:End  backup " >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        echo "===============End DB Backup================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
        echo "...............End Db Backup.............................."
fi

echo ".................Starting All Services.................."
echo "=================Starting ORACLE  =================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
DBRUNWPSDB=`ps -ef |grep ora_ |awk '{print $8 }' |sed 's/^ora_//g'|sed 's/^.....//g'|grep wpsdb`
if [ "${DBRUNWPSDB}" = "" ]; then
        su - oracle -c "sqlplus /nolog @$ORACLE_HOME/mysql/startdb.sql $FLAGUSER $FLAGTNSNAME $FLAGPASS" >>  $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
else
        echo "The db have started!!" >>  $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
fi
echo "=================END DB Start======================" >>  $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
cd $WASBIN
echo "=================Starting server1==================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
./startServer.sh server1 >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
echo "=================END server1 Start=================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
echo "">>$BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
echo "=================Starting WebSPhere_Portal=========" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
./startServer.sh WebSphere_Portal >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
echo "=================END WebSphere_Portal==============" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
cd $IBMHTTP
echo "=================Starting IBMHTTPServer==================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
./apachectl start>> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
echo "=================END IBMHTTPserver Start=================" >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
cd
#产生一个日期记录文件
DATELOG=`date +"%m-%d-%y"`
echo $DATELOG >dateflag.txt
#
echo "..................FTP Trans $IP...................."
date +"%Y年-%m月-%d日:%T:FTP $IP Trans Begin..." >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
ftp -d -i -n <<FTPIT >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
open $IP
user $FTPUSER $FTPPASS
cd $FTPROOT
mkdir $FTPDIR
cd $FTPDIR
bi
lcd $BACKUPDIR
put $FILE
put $ORAFILE
put $LOG
bye
FTPIT
date +"%Y年-%m月-%d日:%T:FTP $IP Trans END..." >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
#
#echo "..................FTP Trans $IP1...................."
#date +"%Y年-%m月-%d日:%T:FTP $IP1 Trans Begin..." >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
#ftp -d -i -n <<FTPIT >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
#open $IP1
#user $FTPUSER1 $FTPPASS1
#mkdir $FTPDIR1
#cd $FTPDIR1
#bi
#lcd $BACKUPDIR
#put $FILE
#put $ORAFILE
#put $LOG
#bye
#FTPIT

#date +"%Y年-%m月-%d日:%T:FTP $IP1 Trans END..." >> $BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
#echo "..................Trans Finish................">>$BACKUPDIR/$LOG 2>>$BACKUPDIR/$LOG
 
del_files.sh:删除前一天备份的数据,加到crontab中,可以在备份前执行
 
#!/bin/sh
###########################################
#此程序是删除历史文件两天前的,避免人为删除错误
#做此程序得目的是当删除历史文件得时候
#只保留wasbackup.sh最近2天备份的文件和之后的arc日志备份
#人为的删除可能会将之后的arc删除,使备份无意义
#
#            author songrh
###########################################
 
#要删除文件的目录
HOMEDIR="/home/WasBackup"
#磁盘使用
USEDISK=`df -k |sed -n '/home/'p | awk '{print $5}' |sed 's/\%//'`
if [ "$USEDISK" -ge "85" ]; then
#提取日期
MONTH=`date +"%m"`
DAY=`date +"%d"`
YEAR=`date +"%y"`
MONTH=`expr $MONTH + 0`
DAY=`expr $DAY + 0 - 2`
YEAR=`expr $YEAR + 0`
CURRENTWEEK=`date +"%a"`
#这里是为了最大称度增强通用性设置得变量,但是郁闷得是后面得字符串处理还是出现了指定得情况(*.tar.z),以后再研究吧
ARCFILE="Prodarclog"
WASFILE="WasBak"
ORAFILE="Oradatabak"
LOGFILE="Prodarcbak"

#COUNTNUM=0
#定义一个函数做比较,参数顺序$files $YEAR $MONTH $DAY
del_files ()
{
              FILEMONTH=`echo $1 |sed 's/.tar.z//g'|tr -cs "[0-9]" "[\012*]" |sed -n '2p'`
              FILEDAY=`echo $1 |sed 's/.tar.z//g'|tr -cs "[0-9]" "[\012*]" |sed -n '3p'`
              FILEYEAR=`echo $1 |sed 's/.tar.z//g'|tr -cs "[0-9]" "[\012*]" |sed -n '4p'`
              #比较月份和天数决定删除的文件
              FILEMONTH=`expr $FILEMONTH + 0`
              FILEDAY=`expr $FILEDAY + 0`
              FILEYEAR=`expr $FILEYEAR + 0`
              #echo $FILEMONTH $FILEDAY $FILEYEAR
              if [ "$FILEYEAR" -lt "$2" ]; then
                        DELFILE=`echo $1 |sed 's/.tar.z//g'`
                        echo "...正在删除文件$DELFILE........" >> del_files_list.txt
                        rm -rf $HOMEDIR/$DELFILE*
              else
                        if [ "$FILEMONTH" -lt "$3" ]; then
                                DELFILE=`echo $1 |sed 's/.tar.z//g'`
                                echo "...正在删除文件$DELFILE........" >> del_files_list.txt
                                rm -rf $HOMEDIR/$DELFILE*
                         else
                                if [ "$FILEMONTH" -eq "$3" ]; then
                                        if [ "$FILEDAY" -lt "$4" ]; then
                                                DELFILE=`echo $1 |sed 's/.tar.z//g'`
                                                echo "...正在删除文件$DELFILE........" >> del_files_list.txt
                                                rm -rf $HOMEDIR/$DELFILE*
                                        fi
                                fi
                        fi
              fi
}
 
#处理数据
echo "==========`date +"%y-%m-%d"`执行删除===============">>del_files_list.txt
for files in `ls -l /home/WasBackup/*.tar*|awk '{print $9}'|sed 's/\/.*\/*\///'`
do
    #COUNTNUM=`expr $COUNTNUM + 1`
    #echo $COUNTNUM
    file=`echo $files |sed 's/.tar.z//g'|tr -cs "[a-z][A-Z]" "[\012*]"`
    case $file in
    $ARCFILE) del_files $files $YEAR $MONTH $DAY
    ;;
    $WASFILE)
             #if [ "$CURRENTWEEK" = "Wed" ]; then
                del_files $files $YEAR $MONTH $DAY
             #fi
    ;;
    $ORAFILE)
             #if [ "$CURRENTWEEK" = "Wed" ]; then
                del_files $files $YEAR $MONTH $DAY
             #fi
    ;;
    *) echo ""
    ;;
    esac
done
echo "==========End===========================">>del_files_list.txt
else
       exit 1
fi

你可能感兴趣的:(ftp,职场,备份数据库,休闲,wasbakup.sh)