日志文件分割
#日志文件的目录
cd /mnt/nginx/log
for file in $(ls *)
do
YEAR=$(date +%Y)
MONTH=$(date +%m)
DAY=$(date +%d)
YESTERDAY=$(date -d "today" +%Y-%m-%d)
LOGS_PATH=/mnt/nginx/log
LOGS_BAK_PATH=/mnt/nginx/log_bak
# 得到1级目录名
if [[ $(($DAY)) -eq 1 ]]
then
if [[ $(($MONTH)) -eq 1 ]]
then
LOGS_BAK_PATH=$LOGS_BAK_PATH/${file%.*}/$((${YEAR}-1))-12
else
if [[ $(($MONTH)) -gt 10 ]]
then
LOGS_BAK_PATH=$LOGS_BAK_PATH/${file%.*}/${YEAR}-$((${MONTH}-1))
else
LOGS_BAK_PATH=$LOGS_BAK_PATH/${file%.*}/${YEAR}-0$((${MONTH}-1))
fi
fi
else
LOGS_BAK_PATH=$LOGS_BAK_PATH/${file%.*}/${YEAR}-${MONTH}
fi
# 创建目录
mkdir -p $LOGS_BAK_PATH
# 复制当前的日志文件到备份的目录
cp ${LOGS_PATH}/$file ${LOGS_BAK_PATH}/${file%.*}_${YESTERDAY}.log
# 清空日志
> ${LOGS_PATH}/$file
done
网友评论