#!/usr/bin/bash
curr_dir="/data/update"
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"home_dir=""
dst_dir=""
#####日志格式定义及路径输出 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" }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 }#####压缩包目录判断,因有个目录比较特殊,其中有个子目录不更新,但是太大,所以单独区分,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 donefi
}
cd $curr_dir
rm -f *.txtjudge_dir