#!/bin/ksh #----------------------------------------------------------------------------- # $Id: rotatelogs,v 1.1 1999/05/30 00:25:16 peters Exp $ # # SCRIPT: rotatelogs # # AUTHOR: Peter Sundstrom (peters@ginini.com.au) # # FUNCTION: Rotate log files and compress old copies by using a central # log definition table. # # Latest version available from # http://www.ginini.com.au/tools/rotatelogs # #----------------------------------------------------------------------------- trap 'echo $0 aborted;exit' 1 2 3 15 USAGE="Usage: `basename $0` [-Dhv] [-f ]" CONFIG=/usr/local/etc/rotate.conf # Rotate configuration file GZIP=/usr/local/bin/gzip # gzip location TODAY=`date +%A` # Name of Today DAYOFM=`date +%d` # Day of the month #------------------------------------------------------------------------- function help { cat <&2 echo "USAGE: rotate [daily] [weekly ] [monthly ] [pre-processing] [post-processing]" >&2 return 1 else LOG=$1 COPIES=$2 TYPE=$3 DAY=$4 PRE=$5 POST=$6 fi if [ "$TYPE" != daily -a "$TYPE" != weekly -a $TYPE != monthly ] then echo "Incorrect rotation type: $TYPE" >&2 echo "Must be either daily, weekly or monthly" >&2 return 1 fi case "$TYPE" in daily) swap $LOG $COPIES ;; weekly) if [ -z "$DAY" ] then echo "No weekday specified for weekly rotation" >&2 return 1 fi if [ $TODAY = $DAY ] then swap $LOG $COPIES fi ;; monthly) if [ -z "$DAY" ] then echo "No day of the month specified for monthly rotation" >&2 return 1 fi if [ $DAY -lt 1 -o $DAY -gt 31 ] then echo "Monthly day: $DAY out of range" >&2 return 1 fi if [ $DAY = $DAYOFM ] then swap $LOG $COPIES fi ;; *) echo "Incorrect rotation type, must be daily, weekly or monthly" >&2 return 1 ;; esac } #-------------------------------------------------------------------------- function swap { [ "$DEBUG" = TRUE ] && set -x LOG=$1 COPIES=$2 typeset -i count=$COPIES-2 if [ -f $LOG ] then [ "$VERBOSE" = TRUE ] && echo "Rotating $LOG. Keeping $COPIES copies" [ "$PRE" != "" -a "$PRE" != "-" ] && eval "$PRE $LOG" while [ $count -ge 0 ] do if [ -f $LOG.$count.gz ] then let newlog=$count+1 mv $LOG.$count.gz $LOG.$newlog.gz elif [ -f $LOG.$count ] then let newlog=$count+1 $GZIP -f $LOG.$count if [ $? -eq 0 ] then mv $LOG.$count.gz $LOG.$newlog.gz else echo "$0: compress of $LOG.$count failed; moving uncompressed" >&2 mv $LOG.$count $LOG.$newlog fi fi let count=$count-1 done cp -p $LOG $LOG.0 if [ $? -eq 0 ] then >$LOG [ "$POST" != "" -a "$POST" != "-" ] && eval "$POST $LOG.0" $GZIP -f $LOG.0 else echo "$0: copy to $LOG.0 failed; log left intact" >&2 fi else echo "$0: $LOG does not exist" >&2 fi } #-------------------------------------------------------------------------- # END OF FUNCTIONS #========================================================================== # # Check for any flags parsed in # while getopts :Dhf:v opt do case $opt in D) set -x DEBUG=TRUE ;; f) CONFIG=$OPTARG ;; h) help ;; v) VERBOSE=TRUE ;; \?) echo "Invalid option" >&2 echo "$USAGE" >&2 exit 1 ;; esac done # # Sanity checks # if [ ! -f $CONFIG ] then echo "Error, $CONFIG does does exist" >&2 exit 1 fi if [ ! -x $GZIP ] then echo "$GZIP not found or not executable" >&2 exit 1 fi # # Make sure day is in lowercase for comparisons # TODAY=`lowercase $TODAY` # # Read files to rotate from configuration file # IFS=" " sed "/^#/D" $CONFIG|sed "/^$/D"| while read file copies when day pre post do when=`lowercase $when` day=`lowercase $day` rotate $file $copies $when $day $pre $post done # # Send a SIGHUP to syslog daemon # if [ -f /etc/syslog.pid ] then kill -1 `cat /etc/syslog.pid` else echo "Not known how to restart syslogd on this system" >&2 fi