svn每日备份和删除(上传到云)- 迁

svn 定时备份的脚本,要求备份完成后发送邮件,把备份的大小、压缩的大小,过滤的目录都罗列

通过远程服务器的nfs服务 把备份盘挂载到本地。

#!/bin/sh
# 两者只能选填一个,都为空就全备份,格式 ('deploy_doc' 'weibo')
#过滤目录 ,指定不备份
FILTER_DIR_ARR=()
#包含目录, 只备份列出的文件
ALLOW_DIR_ARR=('deploy_doc' 'weibo')

#本地保留天
LOCAL_STAY_DAYS=6
#远端保留天
REMOTE_STAY_DAYS=15

# 发送邮件
EMAIL='/usr/local/bin/sendEmail'
SMTP_SERVER='smtp.huored.com'    # SMTP服务器,变量值需要自行修改
USERNAME='sentinel@huored.com'     # 用户名,变量值需要自行修改
PASSWORD='8issHFw4s'             # 密码,变量值需要自行修改
TO='gaotao@huored.com'
SUBJECT="[$(date +%Y%m%d%H%M%S)]svn backup daily"


#挂载目录
MOUNT_DIR="/mnt/aliyun_ecs_nfs_data"
#云目录空间最小空间,单位G
NFS_REMAIN_SPACE=100

#挂载阿里云nfs
mount -t nfs 106.14.162.14:/data $MOUNT_DIR
subPath="$(date +%Y%m%d%H%M%S)"

dumpPath="/opt/svn/svn_manual/hotcopy/$subPath"
echo "dumpPath $dumpPath"
mkdir $dumpPath -p

destPath="${MOUNT_DIR}/svn_backup/manual/hotcopy/$subPath"
mkdir $destPath -p
echo "dataPath $dumpPath" 


function send_mail()
{
    local BODY=$(echo "svn backup list is: $1")
    which $EMAIL || echo "not found sendEmail"
    $EMAIL -f $USERNAME -s $SMTP_SERVER -xu $USERNAME -xp $PASSWORD \
    -t $TO -u "${SUBJECT}" -m "${BODY}" -o message-charset=utf-8
}

function check_remain_space()
{
    value=$(df -h|grep $MOUNT_DIR|awk 'NF>2{print $(NF-2)}' |tr -d 'G')
    echo "backup dir: $MOUNT_DIR remain space is:" $value "G"

    if [ $value"x" == "x" ]; then
        echo "not of dir to backup"
        exit 0

    elif [ $value -lt ${NFS_REMAIN_SPACE} ]; then
        echo "not enough space to backup"
        exit 0
    fi
    echo "let's backup"
}

function do_clean()
{
    echo "to delete"
    local parent_dumpPath=${dumpPath%/*}
    local parent_destPath=${destPath%/*}
    find $parent_dumpPath -maxdepth 1 -name "20[0-9][0-9]*" -ctime +${LOCAL_STAY_DAYS} -type d|xargs rm -rf
    find $parent_destPath -maxdepth 1 -name "20[0-9][0-9]*" -ctime +${REMOTE_STAY_DAYS} -type d|xargs rm -rf
}


function get_size()
{
    local FILE=$1
    du -sh $FILE 2>/dev/null|cut -f 1
}


function get_striptime()
{
    local ABT=$1
    echo $(($(date +%s)-${ABT}))|awk 'BEGIN{a[3]="d";a[2]="h";a[1]="m";a[0]="s";b[0]=60;b[1]=60;b[2]=24;b[3]=9999}{t="";for(n=0;n<=3;n++){if($1>=b[n]){t=$1%b[n]a[n]t;$1=int($1/b[n])}else{t=$1a[n]t;n=4}}print t}'
}



function backup_rule()
{
    local file=$1
    local repoName=$2
    if [ -d "$file" ]; then
        if [ "${#ALLOW_DIR_ARR}" -eq 0 ] && [ "${#FILTER_DIR_ARR}" -eq 0 ]; then
            return 0
        fi

        if [ "${#ALLOW_DIR_ARR}" -ne 0 ] && echo "${ALLOW_DIR_ARR[*]}"|grep -w "$repoName" 1>>/dev/null; then
            return 0
        fi

        if [ "${#FILTER_DIR_ARR}" -ne 0 ] && echo "${FILTER_DIR_ARR[*]}"|grep -vw "$repoName" 1>>/dev/null; then
            return 0
        fi
    fi
    return 2

}


function do_dump()
{
    local BACKUP_LIST=""
    local ORGIN_SIZE=0
    local TAR_SIZE=0
    local ALL_BEGIN_TIME=$(date +%s)
    local ALL_SIZE=0
    local BEGIN_TIME=0
    local ELAPSED_TIME=0
    for file in /home/admin/svn_repo/*
    do
        repoName=${file##*/}
        repoDumpFile=${dumpPath}/${repoName}
        ( backup_rule $file $repoName ) && {
           tagFile=$file/format
           if [ ! -a "$tagFile" ]
           then
            echo "----------------------------------"
            echo "start hotcopy $file to $repoDumpFile"
            rm ${repoDumpFile} -rf
            svnadmin hotcopy $file $repoDumpFile
            svnlook youngest $repoDumpFile
            echo `svnlook youngest $repoDumpFile`
            [ $? -ne 0 ] && continue
            cd ${dumpPath}
            BEGIN_TIME=$(date +%s)
            tar -zcPf ${repoDumpFile}.tar.gz $repoDumpFile
            ORGIN_SIZE=$(get_size $repoDumpFile)
            TAR_SIZE=$(get_size ${repoDumpFile}.tar.gz)
            rm ${repoDumpFile} -rf
            echo "end hotcopy $repoName"
            echo "start remote copy ${repoDumpFile}.tar.gz  to $destPath"
            cp $repoDumpFile.tar.gz  $destPath
            ELAPSED_TIME=$(($(date +%s)-$BEGIN_TIME))
            echo "end remote copy"
            echo "----------------------------------"
            BACKUP_LIST=$(echo "$BACKUP_LIST\n${repoName},${ORGIN_SIZE},${TAR_SIZE},${ELAPSED_TIME}s")
           fi
        } || { 
        echo "${repoName},not match backup rule" 
        }
    done
    [ $? -eq 0 ] && do_clean
    ALL_SIZE=$(du -sh ${destPath})
    ALL_ELAPSED_TIME=$(get_striptime ${ALL_BEGIN_TIME})
    BACKUP_LIST=$(echo "$BACKUP_LIST\nALL_ELAPSED_TIME:${ALL_ELAPSED_TIME}\nALL_SIZE:${ALL_SIZE}")
    #send_mail $BACKUP_LIST
}

check_remain_space
do_dump

 

执行:

       将过滤目录和包含目录直接写到脚本,后期可以用配置文件。

 

邮件返回格式:

svn backup list is: 
xxx0,172K,12K,0s
xxx1,6.7M,5.8M,4s
xxx2,8.7M,6.9M,5s
xxx3,29M,11M,9s
ALL_ELAPSED_TIME:20s
ALL_SIZE:24M
posted @ 2017-11-08 12:06  richardzgt  阅读(424)  评论(0编辑  收藏  举报