这个脚本的原型来自之前提到的db备份脚本,但做了一些改动略有不同。
这次的脚本支持逗号分隔的kv输入,具体输入文本格式如下
+------------------+--------------+----------------+ | folder full path | separator(,) | folder descript| +------------------+--------------+----------------+ | /var/aaaaa/ | , | aaaa11111 | +------------------+--------------+----------------+
发送到邮箱的标题命名:web_bak_${folder descript}_${data}
具体shell代码如下:
#!/bin/bash
#need 2 var
if (($# != 2)); then
echo "need input [Web Site Name Full Path in File] [mode]"
echo "mode -> 0, <Web Site Name Full Path in File> read!"
echo "mode -> 1, do Web Site bak!"
exit;
fi
if (($2 < 0 && $2 > 1)); then
echo "err mode!"
exit
fi
#filter some lines:
# 1. pure tab line
# 2. pure \r\n
# 3. pure space line
WebSiteFullPathInFilePath=$1
sed /^[[:space:]]*$/d $WebSiteFullPathInFilePath > $WebSiteFullPathInFilePath".tmp"
####################################################
#pre-info
#
# +----------+--------------+-------------+
# | fullpath | separator(,) | description |
# +----------+--------------+-------------+
#
####################################################
loop_count=0
bak_WebSiteFullPathInFilePath_target=1
bak_WebSiteDescriptionFilePath_target=1
echo "WebSiteFullPathInFilePath: "$WebSiteFullPathInFilePath" <<<>>> WebSiteFullPathInFilePath_tmp: "$WebSiteFullPathInFilePath".tmp"
echo ============ output all web site info $(date +"%Y%m%d") ==================
while read -r line
do
#echo $line
stringlen=${#line}
keyend=$(expr index $line ',')
key=${line:0:$keyend-1}
value=${line:$keyend:$stringlen-$keyend}
#echo "stringlen: "$stringlen" keyend: "$keyend" key: "$key" value: "$value
bak_WebSiteFullPathInFilePath_target[loop_count]=$key
bak_WebSiteDescriptionFilePath_target[loop_count]=$value
((loop_count++))
done < $WebSiteFullPathInFilePath".tmp"
i=0
for ((;i<$loop_count && $loop_count>0;i++))
do
echo "website name: "${bak_WebSiteDescriptionFilePath_target[$i]}
echo -e "\twebsite full path: "${bak_WebSiteFullPathInFilePath_target[$i]}"\n"
done
echo "total - "$i
echo ============ end ==================
if (($2 == 0)); then
echo "test over"
exit;
fi
if (($i<=0)); then
echo "don't have website full path!"
exit
fi
#do work
i=0
for ((;i<$loop_count && $loop_count>0;i++))
do
WebSiteFullPath=${bak_WebSiteFullPathInFilePath_target[$i]}
WebSiteDescription=${bak_WebSiteDescriptionFilePath_target[$i]}
BakName_tmp=website_bak_${WebSiteDescription}_$(date +"%Y%m%d")
#mkdir
mkdir /tmp/website_bak
mkdir /tmp/website_bak/${WebSiteDescription}
#del bak file before 3 days
rm -rf /tmp/website_bak/${WebSiteDescription}/website_bak_${WebSiteDescription}_$(date -d -3day +"%Y%m%d").tar.gz
#compact file
tar zcf /tmp/website_bak/${WebSiteDescription}/$BakName_tmp.tar.gz $WebSiteFullPath
#Send mail
mutt -s $BakName_tmp "email@163.com" -a /tmp/website_bak/${WebSiteDescription}/$BakName_tmp.tar.gz < /var/auto_script/emptytext.txt
done
同样作为定时任务,将命令执行放在crontab中