mirror of
https://github.com/EDCD/EDDN.git
synced 2025-06-03 00:51:20 +03:00
contrib: Log archival script
This will rotate to datetime stamped filenames (not a serial), and compress. Will also only keep 28 days of logs at most.
This commit is contained in:
parent
1476ff824d
commit
f9cf414255
86
contrib/logs-archive
Executable file
86
contrib/logs-archive
Executable file
@ -0,0 +1,86 @@
|
||||
#!/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
|
Loading…
x
Reference in New Issue
Block a user