#!/usr/bin/bashweb
curr_dir="/data/update"bash
Date=`date +"%Y%m%d"`
today=`date +"%Y%m%d-%H%M%S"`日誌
#comp_pg="web-0420.zip"
#comp_pg="web-0420v1.zip"
comp_pg="ceshi.zip"ip
home_dir=""awk
dst_dir=""date
#####日誌格式定義及路徑輸出
logdir="/data/log/back" #日誌路徑
log=$logdir/log"$today".log #日誌文件
is_font=1 #終端是否打印日誌: 1打印 0不打印
is_log=1 #是否記錄日誌: 1記錄 0不記錄
[[ -d $logdir ]]||mkdir -p $logdir #確保日誌目錄存在終端
datef(){
date "+%Y-%m-%d %H:%M:%S"
}grep
print_log(){
if [[ $is_log -eq 1 ]];then
[[ -d $logdir ]] || mkdir -p $logdir
echo -e "[ $(datef) ] $1" >> $log
fi
if [[ $is_font -eq 1 ]];then
echo -e "[ $(datef) ] $1"
fi
}sort
#####壓縮包目錄判斷,因有個目錄比較特殊,其中有個子目錄不更新,可是太大,因此單獨區分,tar格式用 tar -tf
judge_dir() {
wwwroot_sum=$(unzip -l $comp_pg |grep wwwroot|wc -l)
if [[ $wwwroot_sum -eq 0 ]];then
print_log "沒有wwwroot目錄,不須要備份他"
back_dir=$(unzip -l $comp_pg |awk '{print $4}' |grep /$|awk -F"/" '{print $1}' |sort -rn |uniq -c|awk '{print $2}')
for dir in $back_dir
do
#cp -ar $home_dir/$dir $dst_dir/$dir/$today
if [[ $? -eq 0 ]];then
print_log "$dir 備份成功"
else
print_log "$dir 備份失敗"
fi
done
else
print_log "wwwroot目錄存在,須要單獨備份他"
back_dir=$(unzip -l $comp_pg |grep -v wwwroot |awk '{print $4}' |grep /$|awk -F"/" '{print $1}' |sort -rn |uniq -c|awk '{print $2}')
for dir in $back_dir
do
# cp -ar $home_dir/$dir $dst_dir/$dir/$today
if [[ $? -eq 0 ]];then
print_log "$dir 備份成功"
else
print_log "$dir 備份失敗"
fi
done
unzip -l $comp_pg |grep wwwroot|awk '{print $4}' |grep -v /$ >$curr_dir/$today.txt
while read line
do
echo "${line%/*}" >> $curr_dir/"$Date"_bak.txt
done < $curr_dir/$today.txt
www_back_dir=`cat $curr_dir/"$Date"_bak.txt |sort -rn |uniq -c`
for www_back in $www_back_dir
do
# cp -ar $home_dir/$www_back $dst_dir/$www_back$today
if [[ $? -eq 0 ]];then
print_log "$www_back 備份成功"
else
print_log "$www_back 備份失敗"
fi
donedi
fi
}
cd $curr_dir
rm -f *.txt
judge_dir