General log compression script

1. Description

Universal log compression script usage: 4 parameters need to be passed. The first is to process the logs under that directory, for example: /app/logs, and the second is to process what log format (what keywords are included) For example: log, the first Three are to process logs a few days ago, and the fourth is to process compressed files a few days before deletion

[root@gtcq-gt-resource2-01 app]# more  /usr/local/shell_script/logs_compression.sh 
#!/bin/bash
#
#####################################
#@brief 功能:1、日志压缩 2、日志备份删除
#@author jing
#@version 1.0
#@date 2021/01/13
#@log no
#####################################
#shell Env
source /etc/profile
#cd /app/logs/app
echo $#
if [ $# -lt 3 ]
then
  echo "请输入3-4个参数:第一个是处理的目录;第二个是日志后缀名称;第三个是多少天之前的日志压缩;第四个是压缩日志保留多少多少天如果不传就不处理"
  exit 1
fi
#日志目录
echo  $1
logs_dir=$1
#日志后缀名称
echo $2 
type_log=$2
#日志保留时间
echo $3
mtime=$3
cd $logs_dir
logs_ar=( $(find $logs_dir   -name "*.${type_log}*" -a ! -name '*.gz'  -type f -mtime +${
     
     mtime}) )
for (( i = 0; i < ${#logs_ar[@]}; ++i ))
 do
         ls -l  ${logs_ar[i]}
         gzip ${logs_ar[i]} 
 done
tar_mtime=$(($4+0))
echo $tar_mtime
if [ $tar_mtime -eq 0 ]
then
  echo "没有输入第4个参数,脚本不处理.gz文件删除的工作"
  exit 1
fi 
find $logs_dir -name "*.${type_log}*.gz"  -type f -mtime +${tar_mtime}   -exec rm -rf {
    
    } \;
[root@gtcq-gt-resource2-01 app]# 

Guess you like

Origin blog.csdn.net/qq_31555951/article/details/114598052