EDDN/contrib/eddn-logs-archive

83 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# Add ' -x' above to debug
#
###########################################################################
# Configuration
###########################################################################
# Maximum age, in days, of log files to keep
MAX_LOGFILE_AGE=28
# Minimum size of live log before rotating, see find(1) -size for format
MIN_ROTATE_SIZE="100M"
###########################################################################
###########################################################################
# Helper functions
###########################################################################
##################################################
# Print program usage information.
##################################################
usage() {
echo "Usage: $(basename $1) [ live | beta | dev ]"
}
##################################################
###########################################################################
###########################################################################
# Check command line arguments
###########################################################################
EDDN_ENV="$1"
if [ -z "${EDDN_ENV}" ];
then
usage $0
exit 1
fi
###########################################################################
###########################################################################
# Perform rotation
###########################################################################
LOGS_DIR="${HOME}/${EDDN_ENV}/logs"
if [ ! -d "${LOGS_DIR}" ];
then
echo "$(dirname): Logs directory doesn't exist: ${LOGS_DIR}"
exit 2
fi
cd ${LOGS_DIR} || exit 3
for service in gateway monitor relay ;
do
echo "Service: ${service}"
echo " Expiring old logs..."
find . -name "${service}.log.*.gz" -a -atime +${MAX_LOGFILE_AGE} -exec rm -fv {} \;
echo " DONE"
echo " Checking if current logfile needs archiving..."
if [ ! -z "$(find . -name ${service}.log -a -size +${MIN_ROTATE_SIZE})" ];
then
echo " Archiving ${service}.log ..."
# We have no means to tell the service to close and re-open output, it's
# to stdout/err anyway. So we copy it.
COMPRESSED_NAME="${service}.log.$(date --iso-8601=seconds)"
cp ${service}.log "${COMPRESSED_NAME}"
if [ $? -ne 0 ];
then
echo " FAILED copying live log file to new archive!!!"
echo " Exiting from any further processing."
exit 4
fi
# Truncate the live file.
:> ${service}.log
# Now compress the newly archived log
gzip -9v "${COMPRESSED_NAME}"
echo " DONE"
else
echo " No"
fi
done
###########################################################################
# vim: tabstop=2 shiftwidth=2 expandtab wrapmargin=0 textwidth=0