Logrotate compress only last n logs

logrotate

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
    postrotate
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true    
    endscript 
}

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?

Best Answer

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.