网站静态文件自动从源目录下载发布脚本

此脚本仅供参考学习。如果通过git.svn的方式管理代理可能更加方便。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash
#Author:Nortorm
#Date:2016-05-17
#Declare:更新发布网站静态文件,自动进行备份,并具备版本回滚功能!

#设置变量
DATE=`date '+%Y-%m-%d %H:%M:%S'`
DATE_BAK_FORMAT=`date '+%Y%m%d-%H%M'`
WEB_DIR=/data/www/html
SCRIPT_DIR=/XXX/XXX
UPDATE_APP=(app1.zip app2.zip app3.zip)
app1=xxx
app2=xxx
app3=xxx
mkdir -p ${SCRIPT_DIR}/{release,config,script}

echo_err(){
echo -e "\e[91m[Error]: $1 \e[0m"
exit 1
}

echo_info(){
echo -e "\e[92m[Info]: $1 \e[0m"
}

#初始化脚本环境
rm -rf ${SCRIPT_DIR}/release/*
:>${SCRIPT_DIR}/config/app_list.md

echo "${DATE} INFO: Environment initialization,Now begin running script..."

下载前端静态文件

1
2
3
4
5
6
7
8
9
10
11
12
wget_file(){
#UPDATE_APP=$*
#echo $UPDATE_APP
for i in ${UPDATE_APP[@]}; do
cd ${SCRIPT_DIR}/release
#echo ${i}
/usr/bin/wget http://XXX.XXX.XXX.XXX:XX/XX/${i}
if [[ $? -eq 0 ]];then
echo ${i%.*} >>${SCRIPT_DIR}/config/app_list.md
fi
done
}

备份之前文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
backup_app(){
app_list=(`cat ${SCRIPT_DIR}/config/app_list.md |xargs`)
for back in ${app_list[@]};do
#备份app1并生成最后备份路径List
if [[ ${back} = "${app1}" ]];then
echo_info "${DATE} INFO: backup ${back}"
:>${SCRIPT_DIR}/config/${app1}_last_bak_list.md
echo "/backup/${app1}_bak_${DATE_BAK_FORMAT}" >> ${SCRIPT_DIR}/config/${app1}_last_bak_list.md
mkdir -p /backup/${app1}_bak_${DATE_BAK_FORMAT}
/bin/cp -r ${WEB_DIR}/${app1}/ /backup/${app1}_bak_${DATE_BAK_FORMAT}
echo "/backup/${app1}_static_bak_${DATE_BAK_FORMAT}" >> ${SCRIPT_DIR}/config/${app1}_last_bak_list.md
mkdir -p /backup/${app1}_static_bak_${DATE_BAK_FORMAT}
/bin/cp -r ${WEB_DIR}/static/${app1}/ /backup/${app1}_static_bak_${DATE_BAK_FORMAT}
#备份app2并生成最后备份路径List
elif [[ ${back} = "${app2}" ]];then
echo_info "${DATE} INFO: backup ${back}"
:>${SCRIPT_DIR}/config/${app2}_last_bak_list.md
echo "/backup/common_static_bak_${DATE_BAK_FORMAT}" >> ${SCRIPT_DIR}/config/${app2}_last_bak_list.md
mkdir -p /backup/common_static_bak_${DATE_BAK_FORMAT}
/bin/cp -r ${WEB_DIR}/static/common/ /backup/common_static_bak_${DATE_BAK_FORMAT}
echo "/backup/${app2}_static_bak_${DATE_BAK_FORMAT}" >> ${SCRIPT_DIR}/config/${app2}_last_bak_list.md
mkdir -p /backup/${app2}_static_bak_${DATE_BAK_FORMAT}
/bin/cp -r ${WEB_DIR}/static/${app2}/ /backup/${app2}_static_bak_${DATE_BAK_FORMAT}
#备份app3并生成最后备份路径List
elif [[ ${back} = "${app3}" ]];then
echo_info "${DATE} INFO: backup ${back}"
:>${SCRIPT_DIR}/config/${app3}_last_bak_list.md
echo "/backup/${app3}_bak_${DATE_BAK_FORMAT}" >> ${SCRIPT_DIR}/config/${app3}_last_bak_list.md
mkdir -p /backup/${app3}_bak_${DATE_BAK_FORMAT}
/bin/cp -r ${WEB_DIR}/${app3}/ /backup/${app3}_bak_${DATE_BAK_FORMAT}
echo "/backup/${app3}_static_bak_${DATE_BAK_FORMAT}" >> ${SCRIPT_DIR}/config/${app3}_last_bak_list.md
mkdir -p /backup/${app3}_static_bak_${DATE_BAK_FORMAT}
/bin/cp -r ${WEB_DIR}/static/${app3}/ /backup/${app3}_static_bak_${DATE_BAK_FORMAT}
fi
done
}

发布更新包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
release_app(){
for release in ${app_list[@]};do
#发布更新app1
if [[ ${release} = "${app1}" ]];then
echo_info "${DATE} INFO: update ${release}"
cd ${SCRIPT_DIR}/release
unzip ${release}.zip -d ${release}
/bin/cp -r ${release}/_dist/${release}/* ${WEB_DIR}/${app1}/
/bin/cp -r ${release}/_dist/static/${release}/* ${WEB_DIR}/static/${app1}/
#发布更新app2
elif [[ ${release} = "${app2}" ]];then
echo_info "${DATE} INFO: update ${release}"
cd ${SCRIPT_DIR}/release
unzip ${release}.zip -d ${release}
/bin/cp -r ${release}/_dist/${release}/* ${WEB_DIR}/${app2}/
/bin/cp -r ${release}/_dist/static/${release}/* ${WEB_DIR}/static/${app2}/
#发布更新app3
elif [[ ${release} = "${app3}" ]];then
echo_info "${DATE} INFO: update ${release}"
cd ${SCRIPT_DIR}/release
unzip ${release}.zip -d ${release}
/bin/cp -r ${release}/_dist/${release}/* ${WEB_DIR}/${app3}/
/bin/cp -r ${release}/_dist/static/${release}/* ${WEB_DIR}/static/${app3}/
fi
done
}

按顺序执行

1
./wget_file
1
./backup_app
1
./release_app


通过扫码我的微信公众号来支持我吧

博客有任何问题可以在文章下留言给我,
也可以加入QQ群和邮件博主反馈 ·
QQ群:187701731
Eamil:Admin#skyshe.cn(来信时记得把#改成@)
微信公众号:skyshecn

文章目录
  1. 1. 下载前端静态文件
  2. 2. 备份之前文件
  3. 3. 发布更新包
  4. 4. 按顺序执行