6

Is there a way using logrotate I can rotate entire directory and compress it instead of just the files in a particular directory? I tried experimenting using the config below but that does not work. Give the error message below:

Config:

/path/to/folder/test {
daily
rotate 5
missingok
compress
delaycompress
}

Error:

$logrotate -vf test.conf
reading config file test.conf
reading config info for /path/to/folder/test

Handling 1 logs

rotating pattern: /path/to/folder/test  forced from command line (5 
rotations)
empty log files are rotated, old logs are removed
error: error creating unique temp file: Permission denied
iDev
  • 2,163
  • 10
  • 39
  • 64
  • Maybe you just want a daily cron job that compresses your directory? I don't think logrotate can handle directories. – Benjamin W. Nov 02 '17 at 04:26
  • Thank you for your suggestion, I tried looking and experimenting and couldn't get it working. Before concluding, I thought of reaching out to folks if I am misinterpreting or trying something incorrect. – iDev Nov 02 '17 at 18:43

3 Answers3

2

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.

parttimeturtle
  • 1,125
  • 7
  • 22
1

You can put multiple paths, so you can use the same file for multiple individual logs in a directory. You could then write a script to prepend the log rotate file with the new paths and set it on a cron.

/path/to/folder/test/file1
/path/to/folder/test/file2
 {
    daily
    rotate 5
    missingok
    compress
    delaycompress
    }
R.Meredith
  • 184
  • 2
  • 10
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
  • 151
  • 1
  • 5