shell生成rsync同步脚本

test

#!/bin/bash
#
# Rsync Install Script
# Last Updated
#
##### modify by Jinayf  #####

######手动修改以下内容######

#测试脚本名称
test_name='obs_test111.sh'
#测试分支名称,以空格隔开
branch_name=(test1 test2 test3 test4)
#本地目录,以空格隔开
local_dir=(/wos/test1/portal111/ /wos/test1/portal222/ /wos/test1/portal333/ /wos/test1/portal444/)
#代码发布目录
web_dir='/data/www/vhosts/wos-obs.wondershare.cn/httpdocs/'
#发布服务器IP,以空格隔开
server_ip=(10.10.18.149 10.10.18.149 10.10.18.130 10.10.18.130)
#脚本存放地址
script_dir='/data/wos/rsync/obs'
#过滤不同步内容,以空格隔开
exclude_txt=(/.git/ /runtime/ .env web/index.php config/main.php)
#口令文本
rsync_scrt='/usr/local/rsync/rsync.scrt'

######手动修改内容结束######

echo "========================================"
echo "Generating Rsync script"

#检测脚本是否已经存在,存在则退出
if [[ -f "$script_dir/$test_name" ]];then
        echo "$script_dir/$test_name is exists!"
        exit
fi

#检测本地目录是否存在,不存在则创建
for d in ${local_dir[@]}
do
    if [[ ! -d $d ]];then
        mkdir -p $d
    fi
done

#检测脚本存放目录是否存在,不存在则创建
if [[ ! -d $script_dir ]];then
    mkdir -p $script_dir
fi

#检测过滤文本是否存在,不存在则创建
if [[ ! -d $script_dir/exclude.txt ]];then
    rm $script_dir/exclude.txt -rf
fi

#生成过滤文本
for each in ${exclude_txt[@]}
do
cat >> $script_dir/exclude.txt<<eof
$each
eof
done

cat > $script_dir/$test_name<<eof
#!/bin/sh

echo "\$1"
model=\$
echo "\${model}"
version=\$

case \$model in
eof

num=${#branch_name[@]}
;i<$num;i++))
do
cat >> $script_dir/$test_name<<eof
    ${branch_name[$i]})
        echo "deploy_model: \${model}"
        echo "version: \${version}"
    sudo chown -R  apache:users ${local_dir[$i]}
    sudo rsync -zavP --exclude-from=$script_dir/exclude.txt --password-file=$rsync_scrt ${local_dir[$i]} apache@${server_ip[$i]}::apache$web_dir
    ;;
eof
done

cat >> $script_dir/$test_name<<eof
    *)
        exit
        ;;
esac
eof
echo "Rsync script is OK"
echo "========================================"

real

#!/bin/bash
#
# Rsync Install Script
# Last Updated
#
##### modify by Jinayf  #####

######手动修改以下内容######

#真实脚本名称
real_name='obs111.sh'
#预发布本地目录
pre_local_dir='/wos/obs96111/'
#真实发布本地目录
real_locla_dir='/wos/obs230111/'
#预发布备份目录
pre_backups_dir='/data/wosbak/obs/pre111/'
#真实发布备份目录
real_backups_dir='/data/wosbak/obs/real111/'
#代码发布目录
web_dir='/data/www/vhosts/wos-obs.wondershare.cn/httpdocs/'
#预发布IP
pre_ip=(192.168.10.96)
#真实发布IP,以空格隔开
real_ip=(192.168.10.195 192.168.10.196 192.168.10.201 192.168.10.203)
#脚本存放地址
script_dir='/data/wos/rsync/obs'
#过滤不同步内容,以空格隔开
exclude_txt=(/.git/ /runtime/ .env web/index.php config/main.php)
#口令文本
rsync_scrt='/usr/local/rsync/rsync.scrt'

######手动修改内容结束######

echo "========================================"
echo "Generating Rsync script"

#检测脚本是否已经存在,存在则退出
if [[ -f "$script_dir/$real_name" ]];then
        echo "$script_dir/$real_name is exists!"
        exit
fi

#检测本地目录是否存在,不存在则创建
if [[ ! -d $pre_local_dir ]];then
    mkdir -p $pre_local_dir
fi

if [[ ! -d $real_locla_dir ]];then
    mkdir -p $real_locla_dir
fi

#检测脚本存放目录是否存在,不存在则创建
if [[ ! -d $script_dir ]];then
    mkdir -p $script_dir
fi

#检测备份目录是否存在,不存在则创建
if [[ ! -d $pre_backups_dir ]];then
    mkdir -p $pre_backups_dir
fi

if [[ ! -d $real_backups_dir ]];then
    mkdir -p $real_backups_dir
fi

#检测过滤文件是否存在,存在则删除
if [[ -f $script_dir/real_exclude.txt ]];then
        rm $script_dir/real_exclude.txt -rf
fi

#生成过滤文本
for each in ${exclude_txt[@]}
do
cat >> $script_dir/real_exclude.txt<<eof
$each
eof
done

cat > $script_dir/$real_name<<eof
#!/bin/sh

echo "\$1"
model=\$
echo "\${model}"
version=\$

case \$model in
    Pre)
        echo "deploy_model: \${model}"
        echo "version: \${version}"
        #同步代码到发布目录
    sudo chown -R  apache:users $pre_local_dir
    for pi in ${pre_ip[@]}
    do
        sudo rsync -zavP --exclude-from=$script_dir/exclude.txt --password-file=$rsync_scrt $pre_local_dir apache@\$pi::apache$web_dir
    done
        #创建备份目录
        mkdir -p $pre_backups_dir\${version}
        #把代码复制到备份目录
        cp -r $pre_local_dir* $pre_backups_dir\${version}
        sudo chown -R  apache:users $pre_backups_dir\${version}
        ;;
    Real)
        echo "deploy_model: \${model}"
        echo "version: \${version}"
        #同步代码到发布目录
    sudo chown -R  apache:users $real_locla_dir
    for ri in ${real_ip[@]}
    do
        sudo rsync -zavP --exclude-from=$script_dir/exclude.txt --password-file=$rsync_scrt $real_locla_dir apache@\$ri::apache$web_dir
    done
        #创建备份目录
        mkdir -p $real_backups_dir\${version}
        #把代码复制到备份目录
        cp -r $real_locla_dir* $real_backups_dir\${version}
        sudo chown -R  apache:users $real_backups_dir\${version}
        ;;
    Rollback_Pre)
        echo "deploy_model: \${model}"
        echo "version: \${version}"
    for pib in ${pre_ip[@]}
    do
            sudo rsync -zavP --exclude-from=$script_dir/exclude.txt --password-file=$rsync_scrt $pre_backups_dir\${version}* apache@\$pib::apache$web_dir
    done
        ;;
    Rollback_Real)
        echo "rollback_model: \${model}"
        echo "version: \${version}"
    for rib in ${pre_ip[@]}
    do
            sudo rsync -zavP --exclude-from=$script_dir/exclude.txt --password-file=$rsync_scrt $real_backups_dir\${version}* apache@\$rib::apache$web_dir
    done
        ;;
    *)
        exit
        ;;
esac

eof
echo "Rsync script is OK"
echo "========================================"
上一篇:codeforces补题记录


下一篇:The long goodbye 漫长的告别 | 经济学人中英双语对照精读笔记