rsync同步脚本

#!/bin/bash
export LANG=C
  
date=`date +%Y-%m-%d-%H%M`
  
red=`echo -e "\033[0;31m"`
blue=`echo -e "\033[0;36m"`
white=`echo -e "\033[37m"`
  
rsync=/usr/bin/rsync
rsync_bak_dir=/opt/data_bak/rsync_update_bak
exclude_web_file=/root/sh/rsync_online_web/exclude_web_file.txt
exclude_res_file=/root/sh/rsync_online_web/exclude_res_file.txt
www_bak_dir=/opt/data_bak/rsync_update_bak/www/$date
res_bak_dir=/opt/data_bak/rsync_update_bak/res/$date
logs=/var/log/rsync_web
  
#删除历史备份函数
del_rsync_history(){
    del_rsync_history=${rsync_bak_dir:?error}                                  #删除历史目录是否定义,如果未定义程序退出,不在往下执行。
        if [ $del_rsync_history != "/opt/data_bak/rsync_update_bak"  ];then    #判断删除历史目录是否正确,避免误删除
                printf $red
                echo "$del_rsync_history File directory error !!!"
                echo "Exit..."
                sleep 3
                printf $white
                exit
        fi
  
    echo "find $del_rsync_history -mtime +30 | xargs rm -rf"
    find $del_rsync_history -mtime +30 | xargs rm -rf
}
  
#同步是否成功函数
is_ok(){
        if [ $? -eq 0 ];then
                printf $blue
                echo "file update Success"
                printf $white
        else
                printf $blue
                echo "file update Fail !!!"
                printf $white
        fi
}
  
#同步排除文件是否存在函数
file_web_exist(){
        if [ ! -f $exclude_web_file ];then
                printf $red
                echo "$exclude_web_file file does not exist !!!"
                printf $white
                exit
        fi
}
 
file_res_exist(){
        if [ ! -f $exclude_res_file ];then
                printf $red
                echo "$exclude_res_file file does not exist !!!"
                printf $white
                exit
        fi
}
  
#日志目录是否存在
if [ ! -d $logs ];then
        mkdir -p $logs
fi
  
#目标文件夹
target_www=/opt/web/test/
target_res=/opt/web/test/res/
  
 
 
ip=192.168.1.100
user=rsyncuser
pass=/root/sh/rsync_online_web/rsyncpass
  
#源文件夹
source_www=$user@$ip::web_www
source_res=$user@$ip::web_res
  
#同步流程
if [ "$1" = "www" ];then
     file_web_exist         #判断同步排除文件是否存在
     $rsync -vzrtopg \
            -n \
            --progress \
            --delete-after \
            --exclude-from=$exclude_web_file \
            --password-file=$pass \
            $source_www $target_www
  
    printf $blue
    echo "-----------------------------------------------------"
    echo "  Are you sure update www.test.com ?[Y/y]"
    echo "-----------------------------------------------------"
    printf $white
    read -p ">  " s
    if [ $s == "Y" -o $s == "y" ];then
  
        if [ ! -d $www_bak_dir ];then
                mkdir -p $www_bak_dir
        fi
  
            $rsync  -vzrtopg \                          #同步参数,-v显示详细信息,-z传输文件压缩,-r递归模式,-t保持文件时间信息,-o保持文件属主信息,-p保持文件权限,-g保持文件组信息
                    --progress \                        #显示rsync备份过程
                    --delete-after \                    #同步后删除文件
                    --exclude-from=$exclude_web_file \  #排除文件列表
                    --backup \                          #备份
                    --backup-dir=$www_bak_dir \         #备份目录
                    --log-file=$logs/www.test.com.log \ #日志文件
                    --log-file-format="%f" \            #日志格式
                    --password-file=$pass \             #rsync密码
                    $source_www $target_www             #将source_www文件及目录同步到target_www目录
            is_ok                                       #判断是否同步成功函数
            del_rsync_history                           #删除历史备份文件函数
    else
        printf "No choice,Exit...\n"
        exit  
    fi
  
elif [ "$1" = "res" ]; then
file_res_exist
       $rsync   -vzrtopg \
                -n \
                --progress \
                --delete-after \
                --exclude-from=$exclude_res_file \
                --password-file=$pass \
                $source_res $target_res
  
        printf $blue
        echo "-----------------------------------------------------"
        echo "  Are you sure update res.test.com ?[Y/y]"
        echo "-----------------------------------------------------"
        printf $white
        read -p ">  " s
    if [ $s == "Y" -o $s == "y" ];then
  
    if [ ! -d $res_bak_dir ];then
            mkdir -p $res_bak_dir
    fi
  
            $rsync  -vzrtopg \
                    --progress \
                    --delete-after \
                    --exclude-from=$exclude_res_file \
                    --backup \
                    --backup-dir=$res_bak_dir \
                    --log-file=$logs/res.test.com.log \
                    --log-file-format="%f" \
                    --password-file=$pass \
                    $source_res $target_res
                    is_ok
                    del_rsync_history
    else
                printf "No choice,Exit...\n"
                 exit
        fi
   
else
    echo "----------------------------------------------------------------------------------------"
    printf $blue
    printf "\twww.test.com\n"
    printf "\tUsage: /root/sh/rsync_online_web/rsync_test_web_update.sh   {www|res}\n"
    printf $white
    echo "----------------------------------------------------------------------------------------"
fi

 

posted on 2017-08-25 15:49  dongruiha  阅读(2362)  评论(0编辑  收藏  举报

导航