I love good tools.
I recently wrote a log zipping script specific to the environment I currently work in. We don't remove logs from the servers (yet – that will change, but has not so far). The partitions on which the logs live are quite large. The servers' lifespan is short enough before rebuild or replace to make leaving logs in place possible. In many cases no compressing of logs is needed. On some servers the traffic and the application do create files that must be compressed to keep free space on the partition acceptable.
We had an ad hoc script to do this in detail. The complexity in it was in trying to work around not touching live logs that a process was writing to, tweaking and tuning the regular expressions to miss those active files and still catch the ones wanted. Lots of hand work.
#! /bin/bash
###
### version: 20100414.01
### author: dsm
###
### Note - "rm -f $i >> $LOG 2>&1" for deleting testing
### log files is commented out in 2 places
### to prevent mistake - uncomment if you really
### want to delete files. Until
### uncommented, files that would have been deleted
### will by logged but not actually
### deleted from the filesystem...
###
### VARIABLES
# file to alter retention for individual servers
# create the file via something like
# export MYHOST=`hostname -s`
# echo "3" > /etc/$MYHOST.AGE._gzip_and_cleanup_logs
# This would change the AGE variable to "3"
# this resolves having different files to
# propagate for individual servers or losing the settings
# when a new version is copied out...
MYSELF=`hostname -s`
MYSELF_AGE_FILE="/etc/gzip/$MYSELF.AGE.gzip_and_cleanup_logs"
DATE=`date +%y%m%d%H%M%S`
LOG="/var/logs/gzip_and_cleanup.${DATE}"
date > $LOG # initialize log
if [[ -f $MYSELF_AGE_FILE ]]; then
AGE=`cat $MYSELF_AGE_FILE`
else
AGE=7 # files must be older than $AGE days to be gzipped
fi
# debug
#echo "age set to: $AGE"
LSOF="/usr/sbin/lsof" #location of lsof binary...
if [[ -f $LSOF ]]; then
echo "found $LSOF, continuing..." >> $LOG
else
echo "FAILED TO FIND LSOF AT: $LSOF - EXITING" >> $LOG
echo "this will have to be corrected before this script will run" >> $LOG
exit
fi
GZIP="/usr/bin/gzip"
if [[ -f $GZIP ]]; then
echo "found $GZIP, continuing..." >> $LOG
else
echo "FAILED TO FIND gzip AT: $GZIP - EXITING" >> $LOG
echo "this will have to be corrected before this script will run" >> $LOG
exit
fi
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
TESTING="false" # this enables the delele function
# NOT TO BE ENABLED ON PRODUCTION MACHINES!
KEEP=30 # days to keep, older will be deleted if TESTING set to "true"
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
### MAIN
#
# If TESTING: Delete logfiles older than $KEEP
#
if [[ $TESTING == "true" ]]; then
export LOGDIR=/var/logs
cd ${LOGDIR}
date >> $LOG
echo "" >> $LOG
echo "-----------------------------------">> $LOG
echo "start $LOGDIR DELETE: TESTING is $TESTING" >> $LOG
echo "-----------------------------------" >> $LOG
for i in `ls --color=no | grep -i log | egrep -v .gz$`
do
if [[ `find . -name ${i} -mtime +${KEEP}` ]]; then
if [[ `$LSOF $i` ]]; then
echo "found $i older than $KEEP - but found to be opened by a process - NOT deleting" >> $LOG
else
echo "found $i older than $KEEP: deleting" >> $LOG
# rm -f $i >> $LOG 2>&1
fi
else
echo "$i found to be newer than $KEEP: keeping this file" >> $LOG
fi
done
export LOGDIR=/var/logs/http
cd ${LOGDIR}
echo "" >> $LOG
echo "-----------------------------------">> $LOG
echo "start $LOGDIR DELETE: TESTING is $TESTING" >> $LOG
echo "-----------------------------------" >> $LOG
for i in `ls --color=no | grep -i log | egrep -v .gz$`
do
if [[ `find . -name ${i} -mtime +${KEEP}` ]]; then
if [[ `$LSOF $i` ]]; then
echo "found $i older than $KEEP - but found to be opened by a process - NOT deleting" >> $LOG
else
echo "found $i older than $KEEP: deleting" >> $LOG
# rm -f $i >> $LOG 2>&1
fi
else
echo "$i found to be newer than $KEEP: keeping this file" >> $LOG
fi
done
else
echo "TESTING set to false: $TESTING: NOT deleting any log files..." >> $LOG
fi
#
# Begin cleanup with application server logs
#
export LOGDIR=/var/logs
cd ${LOGDIR} >> $LOG 2>&1
date >> $LOG
echo "" >> $LOG
echo "-----------------------------------">> $LOG
echo "start $LOGDIR cleanup" >> $LOG
echo "-----------------------------------" >> $LOG
echo "" >> $LOG
for i in `ls --color=no | grep -i log | egrep -v .gz$`
do
if [[ `$LSOF ${i}` ]]; then
echo "" >> $LOG
echo "found $i OPEN - not handling" >> $LOG
echo "" >> $LOG
else
if [[ `find . -name ${i} -mtime +${AGE}` ]]; then
if [[ -f ${i}.gz ]]; then
echo "found gzipped file of the same name: ${i}.gz - not gzipping ${i}..." >> $LOG
else
echo "gzipping $i as it is older than $AGE..." >> $LOG
$GZIP $i >> $LOG 2>&1
fi
else
echo "$i is not open but is newer than $AGE, so NOT being gzipped" >> $LOG
fi
fi
done
echo "completed cleanup of application logs..." >> $LOG
#
# Continue cleanup with web server logs
#
export LOGDIR=/var/logs/http
if [[ -d $LOGDIR ]]; then # these may not exist on some apps...
cd ${LOGDIR} >> $LOG 2>&1
echo "" >> $LOG
echo "-----------------------------------">> $LOG
echo "start $LOGDIR cleanup" >> $LOG
echo "-----------------------------------" >> $LOG
echo "" >> $LOG
for i in `ls --color=no | grep -i log | egrep -v .gz$ | egrep -v error_log$ | egrep -v access_log$`
do
if [[ `$LSOF ${i}` ]]; then
echo "" >> $LOG
echo "found $i OPEN - not handling" >> $LOG
echo "" >> $LOG
else
if [[ `find . -name ${i} -mtime +${AGE}` ]]; then
if [[ -f ${i}.gz ]]; then
echo "found gzipped file of the same name: ${i}.gz - not gzipping ${i}..." >> $LOG
else
echo "gzipping $i as it is older than $AGE..." >> $LOG
$GZIP $i >> $LOG 2>&1
fi
else
echo "$i is not open but is newer than $AGE, so NOT being gzipped" >> $LOG
fi
fi
done
echo "completed cleanup of apache logs..." >> $LOG
else
echo "-----------------------------------">> $LOG
echo "start $LOGDIR cleanup" >> $LOG
echo "-----------------------------------" >> $LOG
echo "" >> $LOG
echo "??? failed to find $LOGDIR - PROBLEM???" >> $LOG
echo "" >> $LOG
fi
#
# Cleanup logs once processed...
#
# these logs are post-processing - no need to check for lsof and no need to retain unzipped...
export LOGDIR=/var/logs/app
if [[ -d $LOGDIR ]]; then
cd ${LOGDIR} >> $LOG 2>&1
echo "" >> $LOG
echo "-----------------------------------">> $LOG
echo "start app $LOGDIR cleanup" >> $LOG
echo "-----------------------------------" >> $LOG
echo "" >> $LOG
for i in `ls --color=no | grep open0`
do
echo "looking through $LOGDIR/${i}/processed..." >> $LOG
echo "" >> $LOG
cd $LOGDIR/${i}/processed
for j in `ls --color=no | egrep -v .gz$`
do
if [[ -f ${j}.gz ]]; then
echo "found gzipped file of the same name: ${j}.gz - not gzipping ${j}..." >> $LOG
else
echo "gzipping $j app log..." >> $LOG
$GZIP $j >> $LOG 2>&1
fi
done
echo "COMPLETED $LOGDIR/${i}/processed..." >> $LOG
echo "" >> $LOG
done
echo "" >> $LOG
echo "Processing ${LOGDIR}/processed app logs..." >> $LOG
echo "" >> $LOG
cd ${LOGDIR}/processed
for k in `ls --color=no | egrep -v .gz$`
do
if [[ -f ${k}.gz ]]; then
echo "found gzipped file of the same name: ${k}.gz - not gzipping ${k}..." >> $LOG
else
echo "gzipping $k app log..." >> $LOG
$GZIP $k >> $LOG 2>&1
fi
done
echo "completed app logs cleanup..." >> $LOG
else
echo "" >> $LOG
echo "failed to find $LOGDIR - skipping cleanup of application logs..." >> $LOG
echo "" >> $LOG
fi
#### Changelog ####
dsm - edited for broad issue