Tomcat log files periodically clean up the backup script

  • 2020-05-27 07:53:17
  • OfStack

The main backup log files of the following script are catalina.out, localhost_access_log.yyyy.mm.log and the project log files. The project log file format is "projectname-yyyy-mm-dd.log". The following is the backup script.


#!/bin/sh
######
#  Log scheduled running time for each day 0 point 1 points 
#  delete 20 Days before the log file is compressed 1 Log files before the week 
#  Log file time is calculated based on the date following the log name 
#  Run the script to see if there are other non-log files with the same suffix in the log file and if the log file name matches the requirements 
######
# The directory in which the log files are located 
path=/home/tomcat/apache-tomcat-project/logs
# Enter the log directory 
cd $path
#catalina.out Log file backup 
# The first 1 The date of the day 
bak_date=`date +%Y-%m-%d -d "1 days ago"`
# The backup catalina.out Log, followed by a date 
cp catalina.out catalina.out.$bak_date.log
# empty catalina.out The log file 
echo > catalina.out
#20 Log file deleted days ago 
# To obtain 20 Days before the date 
del_date=`date +%Y-%m-%d -d "20 days ago"`
# Get the date string in the file name, and then compare the time, localhost_access_log Suffixed filename 1 As is txt , which includes txt file 
for n in `ls *.log *.txt -1`;do
m=`echo $n | awk -F. '{print $(NF-1)}'`
m=`echo ${m:0-10}`
if [[ $m < $del_date || $m = $del_date ]];then
echo file $n will be deleted.
rm -rf $n
fi
done
#1 Week before file compression 
# To obtain 1 The date before the week 
zip_date=`date +%Y-%m-%d -d "7 days ago"`
# Get the date string in the file name, and then compare the time to do the corresponding operation 
for n in `ls *.log *.txt -1`;do
m=`echo $n | awk -F. '{print $(NF-1)}'`
m=`echo ${m:0-10}`
echo $n $m
if [ ! $m ]; then
echo "IS NULL"
continue
fi
if [[ $m < $zip_date || $m = $zip_date ]];then
echo file $n will be zip.
zip $n.zip $n
rm -rf $n
fi
done

Related articles: