logrotate entire directory containing log files
Asked Answered
M

3

9

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
Mcduffie answered 2/11, 2017 at 1:18 Comment(2)
Maybe you just want a daily cron job that compresses your directory? I don't think logrotate can handle directories.Sansbury
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.Mcduffie
C
4

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.

Cyclopean answered 1/3, 2019 at 7:37 Comment(0)
V
1

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
Vera answered 25/10, 2021 at 18:40 Comment(0)
D
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
    }
Dissimilar answered 4/8, 2022 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.