之前的通用Linux服务器备份打包脚本因为有新的需求,添加了一些功能.新版本能根据配置文件对指定目录单独打包,更新规则还是一样,保留1号的包,删除指定天数前的包。打包与同步的程序不一样,但是配置文件相同,这就只需要将配置文件放在需要同步的目录下,就能确保打包与同步的策略一致,具体同步程序的代码请猛击服务器运维同步备份脚本 同步篇。数据库使用主从同步到备份的服务器上,打包程序会将它导出,放到指定位置。配置文件及打包程序代码如下:

配置文件如下:

#set tar www and dump db and rsync
DOWWW=true
DODB=true
DORSYNC=true

#base info
WWWNAME=www.test.com
SRCIP=123.123.123.123

#frequence
TARSET=daily
#clean frequence
OLDSET=3 days ago

#db setting
DBNAME=testdb
DBPORT=3306
DBSOCKET=/tmp/mysql.sock

#tar setting
TAREXTRA=--exclude=img/p/* --exclude=upload/p_img/*
DEDICATEDIR=/htdocs/upload/image
DEDICATETAR=upload_image

#rsync setting
RSYNCIP=123.123.123.124
RSYNCNAME=123.123.123.123_conf
RSYNCEXTRA=--exclude='- compile/*' --exclude='- upload/image/*' --exclude='- log/*'

tarback.sh程序代码如下:

#!/bin/bash
# By Chen Zhidong
# http://sillydong.com
# Email:njutczd+gmail.com

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

#Begin
#======================================================
ROOTDIR="/home/www"
LOGDIR="/home/logs"
TARDIR="/home/backup/www"
SQLDIR="/home/backup/mysql"
BINDIR="/usr/local/mysql/bin"

EXCLUDE=".tar.gz .tar.bz2 .zip"

DAY_WEEK="5"
DAY_MONTH="26"

DB_USER="root"
DB_PASS="password"
DB_HOST="localhost"

TAG=$(date +%Y%m%d)
#======================================================

readset(){
    #readset WWWNAME tarsetfile
    grep $1 $2 | grep -v "^#" | sed "s/$1=//"
}

exclude(){
    #exclude directory
    for ex in $EXCLUDE
    do
        exc=`ls $1 | grep $ex`
        if [ $exc ];then
            echo ${1}/${exc} >> exclude.tmp >> $LOGDIR/tarback_$TAG.log
        fi
    done
}

clean(){
    #clean name filedir frequency tarset
    oldset=`readset OLDSET $4`
    case $3 in
    daily)
        FILELIST=`ls -l $2 | awk '{print $9}' | egrep "^$1_.*"`
        for file in $FILELIST
        do
            old=$(date +%Y%m%d --date="$oldset")
            mold=$(date +%m)
            tim=`echo "$file" | sed "s/.*_\([0-9]*\).*/\1/"`
            day=`expr substr $tim 7 2`
            month=`expr substr $tim 5 2`
            if [ $tim -le $old ];then
                if [ "$day" = "01" -a "$month" = "$mold" ];then
                    echo "$file was left as No.1!" >> $LOGDIR/tarback_$TAG.log
                else
                    echo "$file to be removed!" >> $LOGDIR/tarback_$TAG.log
                    rm -f $2/$file
                fi
            else
                echo "$file was left!" >> $LOGDIR/tarback_$TAG.log
            fi
        done
        ;;
    weekly)
        FILELIST=`ls -l $2 | awk '{print $9}' | grep $1`
        for file in $FILELIST
        do
            old=$(date +%Y%m%d --date=$oldset)
            tim=`echo "$file" | sed "s/.*${1}_\([0-9]*\)..*/\1/"`
            if [ $tim -le $old ];then
                echo -e "$2/$file to be removed!" >> $LOGDIR/tarback_$TAG.log
                rm -f $2/$file
            else
                echo "$file will be kept!" >> $LOGDIR/tarback_$TAG.log
            fi
        done
        ;;
    monthly)
        FILELIST=`ls -l $2 | awk '{print $9}' | grep $1`
        for file in $FILELIST
        do
            old=$(date +%Y%m%d --date=$oldset)
            tim=`echo "$file" | sed "s/.*${1}_\([0-9]*\)..*/\1/"`
            if [ $tim -le $old ];then
                echo -e "$2/$file to be removed!" >> $LOGDIR/tarback_$TAG.log
                rm -f $2/$file
            else
                echo "$file will be kept!" >> $LOGDIR/tarback_$TAG.log
            fi
        done
        ;;
    *)
        echo "***Wrong Frepquency was sent to clean()!***" >> $LOGDIR/tarback_$TAG.log
        ;;
    esac
}

dump(){
    #dump tarsetfile ip frequency dbname
    if [ -n $1 ];then
        dbport=`readset DBPORT $1`
        dbsocket=`readset DBSOCKET $1`
        echo "Dumping ${4} ${dbport} ${dbsocket} ..." >> $LOGDIR/tarback_$TAG.log
        $BINDIR/mysqldump --opt $4 --user=$DB_USER --host=$DB_HOST --password=$DB_PASS -P $dbport -S $dbsocket > $SQLDIR/$2/${4}_${TAG}.sql
        if [ "$?" = "0" ];then
            echo "Dumping ${4} done!" >> $LOGDIR/tarback_$TAG.log

            sqldir=$SQLDIR/$2
            echo "cleanning sql $4 ..." >> $LOGDIR/tarback_$TAG.log
            clean $4 $sqldir $3 $1
            echo "done!" >> $LOGDIR/tarback_$TAG.log
        else
            echo "***Dumping ${3} failed!***" >> $LOGDIR/tarback_$TAG.log
        fi
    else
        echo "***$1 has no database!***" >> $LOGDIR/tarback_$TAG.log
    fi
}

tarfile(){
    #tarfile tarset ip frequency $dbname
    srcdir=`echo "$1" | sed "s/\/tar.set$//"`
    destfile=$TARDIR/$srcdir
    echo "tar $srcdir to ${destfile}_$TAG.tar.bz2" >> $LOGDIR/tarback_$TAG.log

    exclude $srcdir

    tarextra=`readset TAREXTRA $1`
    echo "using extra command ${tarextra}" >> $LOGDIR/tarback_$TAG.log

    if [ -e $srcdir -a -d $srcdir ];then
        if [ -e $ROOTDIR/exclude.tmp ];then
            tar $tarextra --exclude-from="$ROOTDIR/exclude.tmp" --ignore-failed-read -jcPf ${destfile}_$TAG.tar.bz2 $srcdir
            rm -f $ROOTDIR/exclude.tmp
        else
            tar $tarextra --ignore-failed-read -jcPf ${destfile}_$TAG.tar.bz2 $srcdir
        fi
        echo "done!" >> $LOGDIR/tarback_$TAG.log
    else
        echo "***$srcdir is not a existing folder!***" >> $LOGDIR/tarback_$TAG.log
    fi

    dedicatedir=(`readset DEDICATEDIR $1`)
    dedicatetar=(`readset DEDICATETAR $1`)

    if [ "$dedicatedir" -a "$dedicatetar" ];then
        len=${#dedicatedir[@]}
        i=0
        while [ $i -lt $len ];do
            dir=${srcdir}${dedicatedir[$i]}
            file=${destfile}_${dedicatetar[$i]}
            echo "tar $dir to ${file}_$TAG.tar.bz2" >> $LOGDIR/tarback_$TAG.log
            if [ -e $dir -a -d $dir ];then
                tar --ignore-failed-read -jcPf ${file}_$TAG.tar.bz2 $dir
                echo "done!" >> $LOGDIR/tarback_$TAG.log
            else
                echo "***$dir is not a existing folder!***" >> $LOGDIR/tarback_$TAG.log
            fi
            let i++
        done
    fi

    tardir=$TARDIR/$2
    wwwname=`readset WWWNAME $1`
    echo "cleanning www $wwwname ..." >> $LOGDIR/tarback_$TAG.log
    clean $wwwname $tardir $3 $1
    echo "done!" >> $LOGDIR/tarback_$TAG.log
}

#=================================================================

if [ `pwd` != "$ROOTDIR" ];then
    cd $ROOTDIR
fi

if [ -e $LOGDIR/tarback_$(date +%Y%m%d --date="5 days ago").log ];then
    rm -f $LOGDIR/tarback_$(date +%Y%m%d --date="5 days ago").log
fi

echo -e "Tarback work begin at $(date)!" >> $LOGDIR/tarback_$TAG.log

#list all *.set files in rsync directory
LIST=`find ./ -maxdepth 3 -name tar.set`

for setfile in $LIST
do
    echo -e "-----------\nUsing $setfile..." >> $LOGDIR/tarback_$TAG.log
    ip=`readset SRCIP $setfile`
    frequency=`readset TARSET $setfile`
    if [ "`readset DODB $setfile`" == "true" ];then
        dbname=`readset DBNAME $setfile`
        dump $setfile $ip $frequency $dbname
    else
        echo -e "skip dumpdb" #>> $LOGDIR/tarback_$TAG.log
    fi
    if [ "`readset DOWWW $setfile`" == "true" ];then
        case $frequency in
        daily)
            #echo "doing daily tarback"
            tarfile $setfile $ip $frequency $dbname
            ;;
        weekly)
            if [ $(date +%w) == $DAY_WEEK ];then
                #echo "do weekly tarback"
                tarfile $setfile $ip $frequency $dbname
            else
                echo -e "TARSET is $frequency, $(date +%w) is not $DAY_WEEK.\n" >> $LOGDIR/tarback_$TAG.log
            fi
            ;;
        monthly)
            if [ $(date +%d) == $DAY_MONTH ];then
                #echo "do monthly tarback"
                tarfile $setfile $ip $frequency $dbname
            else
                echo -e "TARSET is $frequency, $(date +%d) is not $DAY_MONTH.\n" >> $LOGDIR/tarback_$TAG.log
            fi
            ;;
        *)
            echo -e "***Wrong frequency set $setfile***\n" >> $LOGDIR/tarback_$TAG.log
            ;;
        esac
    else
        echo -e "skip www" >> $LOGDIR/tarback_$TAG.log
    fi
done

echo -e "-----------\nTarback work finished at $(date)!" >> $LOGDIR/tarback_$TAG.log

exit 0
#End