清理日志

#!/bin/env bash
#set -x
clear_time=date +%Y%m%d
LOG_FILELOG_FILE="/tmp/log_clean.{clear_time}.log"

#save clean log
write_log(){
now_time='['(date +"%Y-%m-%d %H:%M:%S")']'
echo {now_time}1 | tee -a {LOG_FILELOG_FILE}
}

#clear LOG_FILELOG_FILE
clear_LOG_FILELOG_FILE(){
cd /tmp && find . -type f -name "log_clean*log" -mtime +20 |xargs -i rm -f {}
}

#gzip log
gzip_log(){
zone_dir_name=ls -l /data/runtime/shai/backend|grep -E '^d'|awk '{printNF}'
for zone_dir in {zone_dir_name}
do
if [ -d /data/runtime/shai/backend/{zone_dir}/logs_{zone_dir} ]
then
log_file_list=cd /data/runtime/shai/backend/{zone_dir}/logs_{zone_dir}/ && find . -type f -name "*.log" -mtime +2
for log_file in{log_file_list}
do
write_log "start gzip {zone_dir}/logs_{zone_dir} log {log_file}"
cd /data/runtime/shai/backend/{zone_dir}/logs_{zone_dir}/ && gzip{log_file}
write_log "end gzip {zone_dir}/logs_{zone_dir} log {log_file}"
done
else
write_log "{zone_dir}/logs_{zone_dir}/ is not exist!!!"
fi
done
}

#clean log
clean_log(){
write_log "clean log"
zone_dir_name=ls -l /data/runtime/shai/backend|grep -E '^d'|awk '{printNF}'
for zone_dir in {zone_dir_name}
do
if [ -d /data/runtime/shai/backend/{zone_dir}/logs_{zone_dir} ]
then
write_log "begin clean{zone_dir}/logs_{zone_dir} "
cd /data/runtime/shai/backend/{zone_dir}/logs_{zone_dir}/ && find . -type f -name "*log.gz" -mtime +7 |xargs -i rm -f {}
write_log "{zone_dir}/logs_{zone_dir} clean end"
else
write_log "{zone_dir}/logs_${zone_dir} is not exist!!!"
fi
done
write_log " clean log end"
}

main(){
gzip_log
clean_log
clear_LOG_FILELOG_FILE
}
main

 

posted @ 2024-09-21 21:47  星星的小窝  阅读(3)  评论(0编辑  收藏  举报