#!/usr/bin/ksh # # Cleanup logfiles (daily) # # Usage: # # dailylogs.sh [ DEBUG | TRACE ] # # where adding the word DEBUG (all caps) will # confirm the logfile operations to stdout # by showing each logfile's changes. # # The word TRACE will turn on shell tracing # in all modules. # set -u MYNAME=$(basename $0) DEBUG=0 TRACE=0 if [ $# -gt 0 ] then case $1 in TRACE) set -x TRACE=1 ;; DEBUG) DEBUG=1 ;; esac fi ##################################################### function TrimLogFile { if [ $TRACE -eq 1 ] then set -x fi ## Send over the full pathname of a logfile as MYLOGFILE ## Creates $MYARCHIVES archives and zeros the original file ## Does not rename the existing logfile since it is most likely ## open. ## ## Example: MYLOGFILE=/var/adm/syslog/syslog.log ## MYARCHIVES=5 ## MYCHMOD=644 ## MYCHOWN=root:sys ## Produces: ## /var/adm/syslog/syslog.log.5.Z ## /var/adm/syslog/syslog.log.4.Z ## /var/adm/syslog/syslog.log.3.Z ## /var/adm/syslog/syslog.log.2.Z ## /var/adm/syslog/syslog.log.1.Z ## /var/adm/syslog/syslog.log (zero length) ## ## If DEBUG=1 then report on the transactions MYDIR=$(dirname $MYLOGFILE) MYLOG=$(basename $MYLOGFILE) # Go through the logs backwards starting with the oldest copy # (defined by $MYARCHIVES) NEXTLOG=$MYARCHIVES while [ $NEXTLOG -gt 1 ] do PREVLOG=$(( NEXTLOG - 1 )) # If the previous log exists (logs 1 through $MYARCHIVES-1) then # remove the $NEXTLOG so as to preserve the date/time/permissions # of the previous file. If the previous log does not exist, create # a zero length file and set permissions per $MYCHMOD and $MYCHOWN. if [ -f $MYDIR/$MYLOG.$PREVLOG.Z ] then rm -f $MYDIR/$MYLOG.$NEXTLOG.Z cp -pf $MYDIR/$MYLOG.$PREVLOG.Z $MYDIR/$MYLOG.$NEXTLOG.Z 2>&1 > /dev/null chmod $MYCHMOD $MYDIR/$MYLOG.$NEXTLOG.Z chown $MYCHOWN $MYDIR/$MYLOG.$NEXTLOG.Z else touch $MYDIR/$MYLOG.$PREVLOG.Z chmod $MYCHMOD $MYDIR/$MYLOG.$PREVLOG.Z chown $MYCHOWN $MYDIR/$MYLOG.$PREVLOG.Z fi NEXTLOG=$(( $NEXTLOG - 1 )) done # Now take care of the current logfile rm -f $MYDIR/$MYLOG.1 cp -pf $MYLOGFILE $MYDIR/$MYLOG.1 cat /dev/null > $MYLOGFILE compress -f $MYDIR/$MYLOG.1 chmod $MYCHMOD $MYDIR/$MYLOG.1.Z chown $MYCHOWN $MYDIR/$MYLOG.1.Z if [ $DEBUG -eq 1 -o $TRACE -eq 1 ] then echo "Archive $MYARCHIVES copies and zero $MYLOGFILE:" ll $MYLOGFILE if [ -f $MYLOGFILE.*.Z ] then ll $MYLOGFILE*.Z fi fi } ################## ## ## ## MAIN PROGRAM ## ## ## ################## ## cleanup /var/adm/wtmp and /var/adm/btmp..rather than play ## with fwtmp and trying to get a properly formatted file ## with start record, etc, just zero the file and save the ## last ones (test if btmp exists) MYARCHIVES=5 MYCHOWN=adm:adm MYCHMOD=664 MYLOGFILE=/var/adm/wtmp TrimLogFile MYLOGFILE=/var/adm/btmp MYCHOWN=adm:adm MYCHMOD=600 if [ -f $MYLOGFILE ] then TrimLogFile fi ## cleanup cron log MYARCHIVES=5 MYLOGFILE=/usr/lib/cron/log MYCHOWN=root:root MYCHMOD=644 TrimLogFile # syslog # # Send HUP to syslogd to force it to start using the newly zeroed log # and also record when the new log was started. MYARCHIVES=7 MYLOGFILE=/var/adm/syslog/syslog.log MYCHOWN=root:sys MYCHMOD=644 TrimLogFile kill -HUP $(cat /etc/syslog.pid) # Trim mail log. Stop sendmail just 'cause it's a good idea once a day # and restart after triming. MYARCHIVES=7 MYLOGFILE=/var/adm/syslog/mail.log MYCHOWN=root:sys MYCHMOD=644 /sbin/init.d/sendmail stop > /dev/null TrimLogFile newaliases > /dev/null /sbin/init.d/sendmail start > /dev/null