• rsync同步脚本


    #!/bin/bash
    export LANG=C
      
    date=`date +%Y-%m-%d-%H%M`
      
    red=`echo -e "33[0;31m"`
    blue=`echo -e "33[0;36m"`
    white=`echo -e "33[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...
    "
            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...
    "
                     exit
            fi
       
    else
        echo "----------------------------------------------------------------------------------------"
        printf $blue
        printf "	www.test.com
    "
        printf "	Usage: /root/sh/rsync_online_web/rsync_test_web_update.sh   {www|res}
    "
        printf $white
        echo "----------------------------------------------------------------------------------------"
    fi
  • 相关阅读:
    1 2 3 4 5 6 7 8 9 = 81 在19之间添加加减乘除号,使等式成立
    D3D 模板缓存的创建过程
    visual studio 引用lib
    ASP.NET Core MVC 打造一个简单的图书馆管理系统 (修正版)(一) 基本模型以及数据库的建立
    ViewModel、ViewData、ViewBag、TempData、Session之间的区别和各自的使用方法
    ASP.NET Core MVC 打造一个简单的图书馆管理系统 (修正版)(二)数据库初始化、基本登录页面以及授权逻辑的建立
    在visual studio code和visual studio中编写TypeScript文件自动生成JavaScript文件
    605. 种花问题
    53. 最大子数组和
    47. 全排列 II
  • 原文地址:https://www.cnblogs.com/dongruiha/p/7428393.html
Copyright © 2020-2023  润新知