I need to make a daily rotate of /var/log/messages to be kept for 60 days, where the last 30 logs must be compressed with bzip2.

This is my logrotate config:

/var/log/messages {                                                                                              daily
    rotate 60
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true    

but the result is keeping the last 60 daily not compressed logs. If add compress, then all 60 logs will be compressed.

So is there a way to make it compress ONLY the last 30 logs?


I think there is no such option for logrotate to do it directly. Only one relevant option is delaycompress which delay the compression by one log rotate cycle (one day in your case). Possibly, you can make use of the compresscmd option by writing a shell script to check how many log files you have so far and compress/uncompress the files you want.

Another possibility is to schedule a daily cron job to run a script to check your log files and compress/uncompress what you want.

  • delaycompress is exactly what I looking for, useful to keep one rotated log as uncompressed to allow manipulations with already rotated log by a side parsers, etc. Jul 27 '21 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.