日志的按天分类压缩保存

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#! /bin/sh
date1=$(date +%Y-%m-%d)
date2=$(date +%Y%m%d)
date3=$(date +%y%m%d)

work_dir='/home/project/'

#该步骤其实不需要,但仍保留
cd $work_dir
base_path='/home/project/logArchive/'

filename=$(find $work_dir -type f -name "*.log.*" ! -name "*.log.*bz2"\
         ! -name "*.log.*gz" ! -name "*$date1*" ! -name "*$date2*"\
         ! -name "*$date3*" ! -path "*$base_path*" | sort)
for log_name in $filename
do
        log_date=${log_name##*.}
        log_path="${log_name%/*}/"
        log_date_path=$(date -d $log_date +/%Y/%m/%d/)

        log_back_path=$base_path$log_date_path$log_path
        [ -d $log_back_path ] || mkdir -p $log_back_path
        mv $log_name $log_back_path
done

find $base_path -type f ! -name "*bz2" -exec bzip2 -9 {} \;