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:
for i in "$@"
echo "-- $i"
mkdir -p archived/$y/$m
mv *$y-$m* archived/$y/$m/
bzip2 -v archived/$y/$m/*$y-$m*
Then remember to make it executable.
To archive logfiles from June and July, you can then do the following:
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 🙂