最近由于有需求,遂把原来的服务器打包备份脚本重写了一下,根据现有的情况做了很大调整。tar.set文件进行备份配置。脚本主要的功能是将从远程服务器同步过来的文件打包放到预定的打包文件目录,同时把主从同步的数据库导出来也放到预置的目录。在打包文件保存目录会进行删除太旧的打包,保留每月1号和最近N天的打包文件,避免因为没用的备份文件过去占用了太大空间。针对数据库,在tar.set中特意设置了端口和socket的配置,为了方便使用非3306端口的情况。先看目录结构:

/home/www/IP/www.xxx.com    #同步过来的文件放在这个目录
/home/logs/ #打包日志存放此处
/home/backup/www/IP/    #打包的网站文件存放此处
/home/backup/mysql/IP/  #导出的mysql文件放在此处

下面是备份配置文件tar.set的内容,这个文件要放在需要备份的网站根目录下,即/home/www/IP/www.xxx.com/tar.set

WWWNAME=www.xxx.com
TARSET=daily || weekly || monthly
OLDSET=5 days ago || 2 weeks ago
DBNAME=xxxxx
DBPORT=3306
DBSOCKET=/tmp/mysql.sock
SRCIP=123.123.123.123

主要的代码如下,看官如果有能提高一点效率的建议欢迎留言。现在很多读文件的,效率会低。

#!/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="xxxxxx"
DB_HOST="localhost"

TAG=$(date +%Y%m%d)
#======================================================
readset(){
#readset WWWNAME tarsetfile
    grep $1 $2 | sed "s/$1=//"
}

exclude(){
#exclude directory
    for ex in $EXCLUDE
    do
        exc=`ls $1 | grep $ex`
        if [ -n $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 "Error: Wrong Frepquency was sent to clean()!" >> $LOGDIR/tarback_$TAG.log
        ;;
    esac
}

dump(){
#dump tarsetfile ip dbname
    if [ -n $1 ];then
        dbport=`readset DBPORT $1`
        dbsocket=`readset DBSOCKET $1`
        echo "Dumping ${3} ${dbport} ${dbsocket} ..." >> $LOGDIR/tarback_$TAG.log
        $BINDIR/mysqldump --opt $3 --user=$DB_USER --host=$DB_HOST --password=$DB_PASS -P $dbport -S $dbsocket > $SQLDIR/$2/${3}_${TAG}.sql
        if [ "$?" = "0" ];then
            echo "Dumping ${3} 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
    if [ -e $ROOTDIR/exclude.tmp ];then
        tar jcPf ${destfile}_$TAG.tar.bz2 $srcdir --exclude-from="$ROOTDIR/exclude.tmp"
        rm -f $ROOTDIR/exclude.tmp
    else
        tar jcPf ${destfile}_$TAG.tar.bz2 $srcdir
    fi
    echo "tar $srcdir done!" >> $LOGDIR/tarback_$TAG.log

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

    sqldir=$SQLDIR/$2
    echo "cleanning sql $4 ..." >> $LOGDIR/tarback_$TAG.log
    clean $4 $sqldir $3 $1
}

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

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

if [ -e $ROOTDIR/tarback_$(date +%Y%m%d --date="5 days ago").log ];then
    rm -f $ROOTDIR/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`
if [ -n $LIST ];then
    echo "Nothing to do"
    exit 1
fi

for setfile in $LIST
do
    echo -e "-----------\nUsing $setfile..." >> $LOGDIR/tarback_$TAG.log
    ip=`readset SRCIP $setfile`
    dbname=`readset DBNAME $setfile`
    dump $setfile $ip $dbname
    frequency=`readset TARSET $setfile`
    case $frequency in
    daily)
        tarfile $setfile $ip $frequency $dbname
        ;;
    weekly)
        if [ $(date +%w) == $DAY_WEEK ];then
            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
            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 "Error: Wrong frequency set in $setfile\n" >> $LOGDIR/tarback_$TAG.log
        ;;
    esac
done

echo -e "Tarback work has been finished!" >> $LOGDIR/tarback_$TAG.log

exit 0
#End

欢迎留言提意见!