logrotate entire directory containing log files logrotate entire directory containing log files linux linux

logrotate entire directory containing log files


Logrotate only operates upon individual files in directories, not the entire directory as a single entity. The most straight-forward solution would be a cronjob that calls something like gzip on that directory then moves/deletes files as you see fit.


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/bashDIR_ROTATE_DAYS=7TARBALL_DELETION_DAYS=60LOG_DIR=/var/log/<program>/cd $LOG_DIRlog_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";  fidoneecho "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";  fidone