Sybase備份指令碼 btmbfe_sybase_backup.sh

fjzcau發表於2015-02-08
#!/bin/sh

BACKUPDIR=/sybase/dumpdb
log=$BACKUPDIR/dbbackup.log
ddays=3  #3天前的檔案刪除

Date=`date +%Y%m%d`
dayDir=${BACKUPDIR}/MBFEDB_BT_${Date}
tarfile=MBFEDB_BT_${Date}.tar
zfile=${tarfile}.Z

echo "--------------------------------------------------------" >> $log
nowtime=`date +"%Y%m%d %H:%M:%S"`
echo "--&gt 1-1 $nowtime backup begin" >> $log

if [ -d $dayDir ]
then
    echo "--&gt 1-2 Dir $dayDir exist!" >> $log
else
    mkdir $dayDir
    echo "--&gt 1-3 Create dir $dayDir  ok!" >> $log
fi

#-- dump --#
DBName='MBFEWKDB MBFEHISDB DIRWAYSDB DISWAYSDB'
chown -R sybase $dayDir

for db in $DBName
do
    nowtime=`date +"%Y%m%d %H:%M:%S"`
su - sybase -c "isql -Usa -PBtsa1@ <
dump database $db to '$dayDir/${db}_${Date}.dump'
go
dump transaction $db with truncate_only
go
!"
    echo "--&gt 2 $nowtime $db dump  " >> $log
done

#-- 檢查 dump 備份檔案 --#
flag=0
for db in $DBName
do
if [ -f $dayDir/${db}_${Date}.dump ]
then
    echo "--&gt 2-1 $nowtime $db dump ok   (flag=$flag)" >> $log
else
    flag=`expr $flag + 1`
    echo "--&gt 2-2 $nowtime $db dump fail (flag=$flag)" >> $log
fi
done

if [ "$flag" != "0" ]
then
    echo "--&gt 2-3  backup fail (flag=$flag) " >> $log
    echo "--------------------------------------------------------\n\n" >> $log
    exit 2
fi


#-- 打包 --#
nowtime=`date +"%Y%m%d %H:%M:%S"`
echo "--&gt 3-1 $nowtime tar begin" >> $log
tar -cf $BACKUPDIR/$tarfile $dayDir
res=$?
if [ "$res" = "0" ]
then
    echo "--&gt 3-2 tar ok" >> $log
else
    echo "--&gt 3-3 tar fail(res=$res)" >> $log
    echo "--------------------------------------------------------\n\n" >> $log
    exit 3
fi

#-- 壓縮 --#
nowtime=`date +"%Y%m%d %H:%M:%S"`
echo "--&gt 4-1 $nowtime compress begin" >> $log
compress $BACKUPDIR/$tarfile
res=$?
if [ "$res" = "0" ]
then
    echo "--&gt 4-2 compress ok   (res=$res)" >> $log
    cd $BACKUPDIR
    rm -rf $dayDir
else
    echo "--&gt 3-3 compress fail (res=$res)" >> $log
    echo "--------------------------------------------------------\n\n" >> $log
    exit 4
fi

echo "--&gt 5 Delete files of $ddays days before " >> $log
find $BACKUPDIR -mtime +$ddays -name "MBFEDB_BT_*.Z" >> $log
find $BACKUPDIR -mtime +$ddays -name "MBFEDB_BT_*.Z" | xargs  -i /bin/rm {}

ftp -i -n 10.8.7.100 <
user coredb coredb
bin
put $zfile
bye
!

res=$?
echo "--&gt 6 backup success (res=$res)" >> $log
echo "--------------------------------------------------------\n\n" >> $log

exit 0 

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

相關文章