3

I have an application creating logfiles in a structure like this:

maillog/
 2012-07-02/
  production_environment-2012-07-02__23_51_50-1341265910-some name.log
  production_environment-2012-07-02__23_51_52-1341265912-some other name.log
  ...
 2012-07-03/
   production_environment-2012-07-03__04_26_47-1341282407-third name.log
   production_environment-2012-07-03__04_26_47-1341282407-more names.log
   ...
 2012-07-04/
  ...
 2012-07-05/
  ...

I am looking for a good way to rotate those logdirectories.

If possible I would like to compress the files in each directory (no problem with logrotate) and rotate the dated directories (not possible with logrotate?).

I am hesitant to script something like this myself since I am sure that there are some good tools out there to do this kind of job.

Any ideas how to approach this?

FadenB
  • 31
  • 2
  • What do you mean by "rotate the dated directories"? Remove any older than one month (or whatever period you specify)? – cjc Jul 06 '12 at 12:59
  • Yes this is what I mean with rotating the directories. I want to keep the last N directories (not every day mail logs are created). – FadenB Jul 08 '12 at 14:33
  • Hmm. In that case, it's probably better to set up your own daily cron job to sweep the directories. I can't think of anything off-hand that will do what you want, and, when I've had similar situations, I've tended to write my own script. – cjc Jul 09 '12 at 00:00

1 Answers1

0

A simple shell script scheduled as a crontab should work, given that LOG_DIR doesn't have other tarballs that would be unintentionally removed:

#!/bin/bash
DIR_ROTATE_DAYS=7
TARBALL_DELETION_DAYS=60
LOG_DIR=/var/log/<program>/

cd $LOG_DIR
log_line "compressing $LOG_DIR dirs that are $DIR_ROTATE_DAYS days old...";
for DIR in $(find ./ -maxdepth 1 -mindepth 1 -type d -mtime +"$((DIR_ROTATE_DAYS - 1))" | sort); do
  echo -n "compressing $LOG_DIR/$DIR ... ";
  if tar czf "$DIR.tar.gz" "$DIR"; then
    echo "done" && rm -rf "$DIR";
  else
    echo "failed";
  fi
done

echo "removing $LOG_DIR .tar.gz files that are $TARBALL_DELETION_DAYS days old..."
for FILE in $(find ./ -maxdepth 1 -type f -mtime +"$((TARBALL_DELETION_DAYS - 1))" -name "*.tar.gz" | sort); do
  echo -n "removing $LOG_DIR/$FILE ... ";
  if rm -f "$LOG_DIR/$FILE"; then
    echo "done";
  else
    echo "failed";
  fi
done
Z_K
  • 31
  • 2