Simple log file archiving

Published:

Some ESB servers at work produce log files for each day, and they can grow quite large. In addition the log directory becomes annoyingly full which makes it a bit "blah" to grep for recent stuff. So I figured I could save some space by compressing them.

Decided to experiment a bit and created a simple little script to archive the log files for certain months and figured I might as well share it. Especially since it might be handy to learn from later when I've forgotten how I did it 😛

Simply create a file called for example doarchive.sh in the log directory and stick this in it:

[ $# -eq 0 ] && { echo "Usage: $0 yyyy-mm [yyyy-mm] ..."; exit 1; }

for i in "$@"
do
  echo "-- $i"

  y=${i%-*}
  m=${i#*-}

  echo mkdir...
  mkdir -p archived/$y/$m

  echo mv...
  mv *$y-$m* archived/$y/$m/

  echo bzip2...
  bzip2 -v archived/$y/$m/*$y-$m*

  echo DONE
done

Then remember to make it executable.

$ chmod u+x doarchive.sh

To archive logfiles from June and July, you can then do the following:

$ ./doarchive.sh 2012-06 2012-07

This, of course, assumes the filenames of your log files contains the date in ISO format. For example stuff.log.2012-06-23.

Should probably have some error checking for malformed input and not do anything if there are no log files etc, but yeah... it works. 🙂