Allow giving custom config for rsyslog and defining logs to rotate
This commit is contained in:
parent
8bcdebc69e
commit
16d9b2b42e
2 changed files with 87 additions and 38 deletions
|
@ -1,18 +1,21 @@
|
|||
#!/bin/sh
|
||||
ARCHIVEFILES="all.log"
|
||||
|
||||
LOGDIR="/srv/log"
|
||||
DATE=`date +%Y-%m-%d`
|
||||
YEAR=`date +%Y`
|
||||
ARCHIVEDIR="/srv/log/archive/" #archivedlogs will be in this
|
||||
#directory + $YEAR
|
||||
ARCHIVE="${LOGDIR}/archive"
|
||||
|
||||
DATE="`date +%Y-%m-%d`"
|
||||
YEAR="`date +%Y`"
|
||||
|
||||
umask 027
|
||||
|
||||
myerror(){
|
||||
myerror()
|
||||
{
|
||||
echo "Error: $*" 1>&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
archive_log(){
|
||||
archive_log()
|
||||
{
|
||||
FILE="${1}"
|
||||
DEST="${2}"
|
||||
|
||||
|
@ -21,44 +24,54 @@ archive_log(){
|
|||
else
|
||||
echo "Archiving file ${FILE} to ${DEST}"
|
||||
mv "${FILE}" "${DEST}"
|
||||
touch ${FILE}
|
||||
touch "${FILE}"
|
||||
LOGS="${LOGS} ${DEST}"
|
||||
fi
|
||||
}
|
||||
|
||||
restart_syslog(){
|
||||
restart_syslog()
|
||||
{
|
||||
for i in syslog.pid rsyslogd.pid syslogd.pid ; do
|
||||
if [ -f "/var/run/$i" ]; then
|
||||
PIDFILE="/var/run/$i"
|
||||
break
|
||||
fi
|
||||
if [ -f "/var/run/$i" ]; then
|
||||
PIDFILE="/var/run/$i"
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ "blah${PIDFILE}" = "blah" ]; then
|
||||
myerror "Cannot find syslog pid file" 1>&2
|
||||
myerror "Cannot find syslog pid file"
|
||||
fi
|
||||
kill -HUP `cat ${PIDFILE}`
|
||||
}
|
||||
archive(){
|
||||
[ -d ${LOGDIR} ] || myerror "No such direcroty: ${LOGDIR}"
|
||||
[ -d "${ARCHIVEDIR}" ] || myerror "No such archive directory: ${ARCHIVEDIR}"
|
||||
[ -d "${ARCHIVEDIR}/${YEAR}" ] || mkdir ${ARCHIVEDIR}/${YEAR}
|
||||
ARCHIVEDIR="${ARCHIVEDIR}/${YEAR}"
|
||||
|
||||
for logfile in ${ARCHIVEFILES} ; do
|
||||
[ -f "${LOGDIR}/${logfile}" ] || myerror "File not found: ${logfile}"
|
||||
archive_log "${LOGDIR}/${logfile}" "${ARCHIVEDIR}/${logfile}.${DATE}"
|
||||
done
|
||||
restart_syslog
|
||||
for zipfile in ${ARCHIVEFILES} ; do
|
||||
gzip -f "${ARCHIVEDIR}/${zipfile}.${DATE}" || myerror "Error while gzipping ${ARCHIVEDIR}/${zipfile}"
|
||||
done
|
||||
}
|
||||
|
||||
case "x$1" in
|
||||
"x-v"|"x--verbose")
|
||||
archive
|
||||
;;
|
||||
*)
|
||||
archive >> /dev/null
|
||||
;;
|
||||
esac
|
||||
[ $# -gt 0 ] || myerror "Usage: `basename $0` <file|dir> [file|dir] ..."
|
||||
|
||||
[ -d ${LOGDIR} ] || myerror "Not a directory: ${LOGDIR}"
|
||||
|
||||
while [ "$*" ]; do
|
||||
if [ -f "${LOGDIR}/${1}" ]; then
|
||||
dstdir=${ARCHIVE}/${YEAR}
|
||||
dstfile=${dstdir}/`basename ${1}`.${DATE}
|
||||
[ -d "${dstdir}" ] || mkdir -p ${dstdir}
|
||||
archive_log ${LOGDIR}/${1} ${dstfile}
|
||||
elif [ -d "${LOGDIR}/${1}" ]; then
|
||||
for f in ${LOGDIR}/${1}/*.log; do
|
||||
if [ -f "${f}" ]; then
|
||||
dstdir=${ARCHIVE}/${1}/${YEAR}
|
||||
dstfile=${dstdir}/`basename ${f}`.${DATE}
|
||||
[ -d "${dstdir}" ] || mkdir -p ${dstdir}
|
||||
archive_log ${f} ${dstfile}
|
||||
else
|
||||
echo "Skipping ${f}: not a file" 1>&2
|
||||
fi
|
||||
done
|
||||
else
|
||||
echo "Skipping ${1}: not a file or directory" 1>&2
|
||||
fi
|
||||
shift
|
||||
done
|
||||
|
||||
restart_syslog
|
||||
|
||||
for log in ${LOGS}; do
|
||||
gzip -f ${log} || myerror "Error while gzipping ${log}"
|
||||
done
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue