Logratation based on size

Here is a script to rotate any logfile. Optionally a filesize can be given. It contains some extra features that are not (yet) in the logrotate facility.

I am not sure it works, but hey, you can tweak it! ;-)

#!/bin/bash

defaultconfig() {
PATH=/usr/bin:/usr/local/bin
number=9
}

usage() {
echo "Usage: $0 [-h|--help] [-c [VALUE]] [-s VALUE] [-n VALUE] -f FILE"
echo ""
echo " -h, --help"
echo "     Display this screen."
echo " -c"
echo "     Use gzip to compress file."
echo " -s VALUE"
echo "     rotate the file is the size equals VALUE. Size in bytes."
echo "     If no size is specified, the file is rotated without"
echo "     checking its size."
echo " -n VALUE"
echo "     rotate VALUE number of file. Default is set to 9"
echo " -d DIRECTORY"
echo "     Move the rotated files to DIRECTORY."
echo " -f FILE"
echo "     FILE is the file to be rotated. This is an argument, not"
echo "     an option."
echo " -p COMMAND"
echo "     execute COMMAND before (pre) rotating."
echo " -P COMMAND"
echo "     execute COMMAND after (post) rotating."
echo ""
exit 1
}

readargs() {
while [ "$#" -gt 0 ] ; do
  case "$1" in
   -c)
    compress="yes" && extension=".gz"
    shift
   ;;
   -s)
    if [ "$2" ] ; then
     size="$2"
     shift ; shift
    else
     echo "No size specified."
     echo ""
     usage
    fi
   ;;
   -n)
    if [ "$2" ] ; then
     number="$2"
     shift ; shift
    else
     echo "Number of files to rotate not specified."
     echo ""
     usage
    fi
   ;;
   -f)
    if [ -f "$2" ] ; then
     file="$2"
     shift ; shift
    else
     echo "No such file: $2."
     echo ""
     usage
    fi
   ;;
   -d)
    if [ -d "$2" ] ; then
     directory="$2"
     shift ; shift
    else
     echo "No such directory: $2."
     echo ""
     usage
    fi
   ;;
   -p)
    if [ "$2" ] ; then
     command=`echo "$2" | awk '{print $1}'`
     if [ -x "$command" ] ; then
      precommand="$2"
      shift ; shift
     else
      echo "Command $precommand cannot be executed"
      echo ""
      usage
     fi
    else
     echo "No command specified"
     echo ""
     usage
    fi
   ;;
   -P)
    if [ "$2" ] ; then
     command=`echo "$2" | awk '{print $1}'`
     if [ -x "$command" ] ; then
      postcommand="$2"
      shift ; shift
     else
      echo "Command $postcommand cannot be executed"
      echo ""
      usage
     fi
    fi
   ;;
   *)
    echo "Unknown option $1"
    echo "You must specify at lease a file with the \"-f\" argument."
    echo ""
    usage
   ;;
  esac
done
}

precommand() {
if [ "$precommand" ] ; then
  $precommand
  if [ "$?" -gt 0 ] ; then
   echo "Command $precommand failed, aborting."
   echo
   usage
  fi
fi
}

postcommand() {
if [ "$postcommand" ] ; then
  $postcommand
  if [ "$?" -gt 0 ] ; then
   echo "Command $postcommand failed, retrying."
   sleep 3
   $postcommand
   if [ "$?" -gt 0 ] ; then
    echo "Command $postcommand failed, aborting."
    echo
    usage
   fi
  fi
fi
}

rotatefiles() {
if [ -f "$file" ] ; then
  while [ "$number" -ge 1 ] ; do
   if [ -f "$directory$file.$number$extension" ] ; then
    let numberplusone="$number + 1"
    mv "$directory$file.$number$extension" "$directory$file.$numberplusone$extension"
   fi
   let number="$number - 1"
  done
else
  echo "No file to rotate."
  echo ""
  usage
fi
}

firstmove() {
precommand
cp "$file" "$directory$file.1"
cp /dev/null "$file"
chmod 644 "$file"
postcommand
if [ "$compress" ] ; then
  gzip "$directory$file.1"
fi
}

startrotator() {
let number="$number -1" && rotatefiles
firstmove
}

checkrunning() {
mode="$1"
instances=`ps -ef | grep "$0" | grep -v "$$"`
numberofinstances=` echo "$instances" | wc -l`
if [ "$numberofinstances" -gt 1 ] ; then
  if [ "$mode" = "silent" ] ; then
   exit 1
  else
   echo "$0 is already running. Stopping this instance."
   echo "instances:"
   echo "---"
   echo "$instances"
   echo "---"
   echo '$0'": $0"
   echo '$$'": $$"
   exit 1
  fi
fi
}

checkrunning silent || ( sleep 60 ; checkrunning )
defaultconfig
readargs "$@"

if [ -f "$file" ] ; then
if [ "$size" ] ; then
  filesize=`ls -l "$file" | awk '{print $5}'`
  if [ "$filesize" -gt "$size" ] ; then
   startrotator
  fi
else
  startrotator
fi
else
echo "You did not specify a file."
echo ""
usage
fi

Comments

Thanks for this idea. I'll

Thanks for this idea. I'll try to use this script soon. - Fred Wehba

About Consultancy Articles Contact




References Red Hat Certified Architect By Robert de Bock Robert de Bock
Curriculum Vitae By Fred Clausen +31 6 14 39 58 72
By Nelson Manning [email protected]