Created
June 5, 2018 02:02
-
-
Save yezihack/6557a52450c61f97169b2e0f04d944b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
######################### | |
# 功能:实现自动备份nginx下的站点 | |
# 版本:v1.0 | |
# 时间:2018/06/04 | |
# 作者:百里 | |
# 最后修改时间:2018/06/05 | |
######################## | |
set +x | |
######################################自定义参数################################### | |
#定义备份的目录,无需/结尾 | |
back_dir=/data/back/site_root | |
#定义站点目录,无需/结尾 | |
vhost_dir=/usr/local/nginx/conf/vhost | |
#定义文件名格式 | |
back_file_name=`date +%Y%m%d` | |
#是否开启备份,默认不备份,只显示,传1则备份 | |
is_back=$1 | |
#定义排除站点,必须是.conf结尾的后缀 | |
exclude_site=("admin-laravel.sgfoot.com.conf" "car.cc.conf" "sync.conf" "jap.cc.conf" "wxadmin.sgfoot.com.conf") | |
#定义nginx配置文件含某关键字的不备份 | |
exclude_keyword=("test" "asset") | |
#定义root站点目录含某关键字的不备份 | |
exclude_root_keyword=("test" "sgfoot") | |
#设置保存多少天 | |
day=30 | |
######################################业务逻辑##################################### | |
#定义站点数组 | |
site_list=() | |
#获取所有正在运行的站点名称 | |
i=0 | |
for file_name in $vhost_dir/* | |
do | |
name=`basename $file_name` | |
ext="${name##*.}" #提取后缀 | |
if [[ $ext == "conf" && ${exclude_site[@]} != *$name* ]] #排除非conf文件名 | |
then | |
site_list[$i]=$name | |
let "i++" | |
fi | |
done | |
#排除含某关键字的站点 | |
i=0 | |
for file_name in ${site_list[@]} | |
do | |
for key in ${exclude_keyword[@]} | |
do | |
if [[ $file_name == *$key* ]];then | |
unset site_list[$i] | |
fi | |
done | |
let "i++" | |
done | |
#分析提取站点目录 | |
site_root_list=() | |
i=0 | |
for file_name in ${site_list[@]} | |
do | |
file=$vhost_dir"/"$file_name | |
if [ -f $file ];then | |
root=$(cat $file |grep "root"|awk '{print $2}') | |
root=${root%%;*} | |
if [ -e $root ];then | |
site_root_list[$i]=$root | |
let "i++" | |
fi | |
fi | |
done | |
#排除root站点含某关键字不备份 | |
i=0 | |
site_root_new=() | |
for file in ${site_root_list[@]} | |
do | |
flag=0 | |
for key in ${exclude_root_keyword[@]} | |
do | |
if [[ "$file" == *$key* ]];then | |
flag=1 | |
break | |
fi | |
done | |
if [ $flag -eq 0 ];then | |
file=${file%%public*} #解决laravel项目特殊情况 | |
file=${file%%/} #去掉路径后的/ | |
site_root_new[$i]=$file | |
let "i++" | |
fi | |
done | |
#去重 | |
len=${#site_root_new[@]} | |
for((i=0;i<$len;i++)) | |
do | |
for((j=$len-1;j>i;j--)) | |
do | |
if [[ ${site_root_new[i]} = ${site_root_new[j]} ]];then | |
unset site_root_new[i] | |
fi | |
done | |
done | |
#rd=$RANDOM | |
echo "自动备份,共${#site_root_new[*]}个站点" | |
echo "开始备份..." | |
back_dir=${back_dir%%/}"/"`date +%Y%m%d`"/" | |
if [ ! -e $back_dir ];then | |
mkdir -p $back_dir | |
fi | |
for root in ${site_root_new[@]} | |
do | |
name=`basename $root` | |
back_file=$back_dir$name"-"$back_file_name".tar.gz" | |
echo "正在备份: $root >> $back_file" | |
if [[ -n $is_back && $is_back == 1 ]];then | |
tar -czPf $back_file --exclude=.svn --exclude=.git $root/ | |
fi | |
done | |
#保留多少天的数据 | |
find $back_dir -mtime +$day -name "*.tar.gz" -exec rm -rf {} \; | |
echo "备份结束" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
测试备份: # ./autoback-nginx-site.sh
正式备份: # ./autoback-nginx-site.sh 1