清除一周日志

论坛 期权论坛 脚本     
匿名技术用户   2021-1-6 17:41   11   0
#!/bin/bash
#file name      :auto_clear_log.sh
#function       :清除一周前各个微服务的日志
#version        :V 1.0


#日志跟目录
log_root_path="/data/logs/tomcat"
#设置日志保留日期,七天内日志保留
last_date=$(date -d '7 days ago' "+%s")


echo "hello!${last_date}"


#获取文件的change时间
get_file_change_time(){
  echo $(date -d $(stat ${1}  |grep Change |awk '{print $2}') '+%s')
}

#判断是否可删除,可以就删除
judge_can_del_then_do(){
  file_change_time=$(get_file_change_time "${1}")
  #比较日期,早于指定日期则删除
  if [ ${file_change_time} -gt ${last_date} ]; then
    echo "==近7日=》${1},chanageTime:${file_change_time}" >> ./test.log
  else
    echo "==7日前=》${1},chanageTime:${file_change_time}" >> ./test.log
  fi
}

#遍历文件
traversal_all_file(){
 for f in $(ls ${1}) ;do
   if test -d "${log_root_path}/${f}" ; then
     echo "文件夹:${1}/${f}" >> ./test.log
     traversal_all_file "${1}/${f}"
   else
     echo "文件:${1}/${f}" >> ./test.log
     judge_can_del_then_do "${1}/${f}"
   fi
 done
}


traversal_all_file "${log_root_path}"

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP