scripts/shell/archivecatalina.sh
2024-03-20 11:28:46 -05:00

182 lines
4.2 KiB
Bash
Executable file

#!/usr/bin/env bash
# shellcheck disable=SC2086
#
# archive catalina.out
#
# SPDX-License-Identifier: MIT
## SETTINGS MUST BE CHANGED TO USE THIS SCRIPT ##
# This script will operate on the already rotated catalina.out logfiles
# generated by the changing catalina.sh to use a piped logging method
# such as:
#
# org.apache.catalina.startup.Bootstrap "$@" start \
# |/usr/bin/rotatelogs "$CATALINA_BASE"/logs/catalina.out.%Y%m%d-%H%M 200M >> /dev/null 2>&1 &
# Run this from crontab nightly at an off hour,
# as gzip can suck up some serious CPU on huge logs:
# 0 2 * * * /opt/sbin/archivecatalina.sh
########## SETTINGS BEGIN ##########
# ARCHIVE_METHOD settings
# 0 = unset (default)
# 1 = compress in place
# 2 = move to another location
# 3 = compress and move
ARCHIVE_METHOD=0
# DURATION_KEEP setting
# <n> = number of days prior to keep around
DURATION_KEEP=28
# CATALINA_LOGS setting
# path to source catalina.out.* files
CATALINA_LOGS=/usr/local/tomcat/logs
# CATALINA_ARCH setting
# path to store older logs based on ARCHIVE_METHOD=2 or 3
# NOTE: this directory is NOT autocleaned on purpose!!
CATALINA_ARCH=/mnt/server/arclogs
# DURATION_WORK setting
# <n> = number of days prior to start working (default 1 day)
DURATION_WORK=1
########## SETTINGS END ##########
# What's our log name
LOG_BASE="catalina.out"
# Errorlevels
ERR_ERROR=33
ERR_PANIC=66
# Default RedHat program locations
MV=/bin/mv
DT=/bin/date
RM=/bin/rm
ID=/usr/bin/id
GZ=/usr/bin/gzip
# Are we root?
USER=$($ID -u)
if [ "X$USER" != "X0" ]; then
echo "PANIC: Only root can run this script!"
exit $ERR_PANIC
fi
# Sanity checks
if [ $ARCHIVE_METHOD -eq 0 ]; then
echo "PANIC: ARCHIVE_METHOD is set to 0, please configure the script."
exit $ERR_PANIC
fi
if [ $DURATION_WORK -lt 1 ]; then
echo "PANIC: DURATION_WORK is set to less than one day."
exit $ERR_PANIC
fi
if [ $DURATION_KEEP -le $DURATION_WORK ]; then
echo "PANIC: DURATION_KEEP is less than or equal to DURATION_WORK."
exit $ERR_PANIC
fi
# Does the source path exist?
if [ ! -x "$CATALINA_LOGS" ]; then
echo "ERROR: $CATALINA_LOGS doesn't exist!"
exit $ERR_ERROR
fi
# Backup dir exists/writable?
if [ $ARCHIVE_METHOD -eq 2 ] || [ $ARCHIVE_METHOD -eq 3 ]; then
if [ ! -x "$CATALINA_ARCH" ]; then
echo "ERROR: $CATALINA_ARCH doesn't exist or isn't writable!"
exit $ERR_ERROR
fi
fi
# generate fromat stamps
DATE_WORK=$($DT --date "now - $DURATION_WORK days" +"%Y%m%d")
DATE_KEEP=$($DT --date "now - $DURATION_KEEP days" +"%Y%m%d")
# log actions below depends on formatting looking like:
# /some/path/catalina.out.20100520-1345 (YYYYMMDD-HHMM)
# (.gz is accounted for)
# ARCHIVE_METHOD=1
compress_keep (){
for logfile in "$CATALINA_LOGS/$LOG_BASE."*; do
# if it ends in gz it's already compressed
if [ "${logfile:(-3):3}" == ".gz" ]; then
continue
else
# grab the %Y%m%d out of the name
DTS=${logfile:(-13):8}
if [ $DTS -ge $DATE_KEEP ] && [ $DTS -le $DATE_WORK ]; then
# compress in place
$GZ "$logfile"
fi
fi
done
}
# ARCHIVE_METHOD=2
move_only (){
for logfile in "$CATALINA_LOGS/$LOG_BASE."*; do
# strip off .gz, we don't care if it's compressed
logtemp=${logfile%.gz}
DTS=${logtemp:(-13):8}
if [ $DTS -ge $DATE_KEEP ] && [ $DTS -le $DATE_WORK ]; then
$MV -f "$logfile" "$CATALINA_ARCH/"
fi
done
}
# ARCHIVE_METHOD=3
compress_move (){
for logfile in "$CATALINA_LOGS/$LOG_BASE."*; do
logtemp=${logfile%.gz}
DTS=${logtemp:(-13):8}
if [ $DTS -ge $DATE_KEEP ] && [ $DTS -le $DATE_WORK ]; then
# already compressed
if [ ${logfile:(-3):3} == ".gz" ]; then
$MV -f "$logfile" "$CATALINA_ARCH/"
else
$GZ -c "$logfile" >> "$CATALINA_ARCH/$logfile.gz"
$RM -f "$logfile"
fi
fi
done
}
# cleanup older than DURATION_KEEP
cleanup_older (){
for logfile in "$CATALINA_LOGS/$LOG_BASE."*; do
logtemp=${logfile%.gz}
DTS=${logtemp:(-13):8}
if [ $DTS -lt $DATE_KEEP ]; then
$RM -f "$logfile"
fi
done
}
# the main case
case $ARCHIVE_METHOD in
1)
compress_keep
;;
2)
move_only
;;
3)
compress_move
;;
*)
echo "PANIC: Invalid ARCHIVE_METHOD settting: $ARCHIVE_METHOD"
exit $ERR_PANIC
esac
# do a final cleanup
cleanup_older
exit 0