#!/usr/local/bin/ksh ############################################################################### # # Author: Nick Rogness (nick@rapidnet.com) # # Date: 4/26/00 # # Description: Program used to backup and then destroy log files by # backing them up (optional) then removing them # # Usage: Run in the crontab with the following syntax: # # rotatelog /full/path/to/log_files/root # # Notes: -This is not as complete and has little error # checking. A complete version is in the works # with a lot more functionality...believe me. # -This was implemented for snort # (http://www.clark.net/~roesch) # log files but should work on any file structure # -Make sure to set options in the section below # -Make sure to change path of Korn Shell # Interpreter (above) # if it is not correct (Written in FreeBSD pd-ksh package) # # # Exit Status: 0 => Completed successfully # 1 => Error on the remove # 2 => Error on the remove (invalid dir) # 3 => Error in switch ops # 4 => Invalid sytntax # # Credits: Jim Forster # (jforster@rapidnet.com, http://snort.rapidnet.com) # ################################################################################ # Set options below get_options () { #Location of files and switches #Argument passed to the program DIR=$1 # Date format: Year-month-day.Hour-Minute-Second DATE=`/bin/date "+%Y-%m-%d.%H-%M-%S"` # Backup before destroying (Either Y/N) BACKUP=Y # Where to store the backup Tarball BACKUP_PATH=/var/tmp #Unique Name for backup file BACKUP_FILE=$BACKUP_PATH/snort-backup.$DATE #Use gzip compression on backup (Either Y/N) USE_COMPRESSION=Y #Location of comon progs RM=/bin/rm TAR=/usr/bin/tar LS=/bin/ls GREP=/usr/bin/grep AWK=/usr/bin/awk GZIP=/usr/bin/gzip } # Shouldn't need to set anymore options cleanup () { $RM -rf $DIR return $? } main () { case $BACKUP in y|Y ) case $USE_COMPRESSION in y|Y ) $TAR -cpPf - $DIR |$GZIP > $BACKUP_FILE.tar.gz ;; n|N ) $TAR -cpPf $BACKUP_FILE.tar $DIR ;; * ) print 'Invalid options...exting' exit 3 ;; esac ;; n|N ) ;; # Fall Through * ) print 'Invalid options...exting' exit 3 ;; esac # Actually remove stuff cleanup E_VALUE=$? case $E_VALUE in 0 ) exit 0 ;; * ) exit $E_VALUE ;; esac } get_options $1 if [[ -z $1 ]]; then print "Usage: $0 /full/path/to/rooted/log_files" exit 4 fi if [[ ! -d $DIR ]]; then print "Invalid Directory: $DIR...exiting" exit 2 fi main