Making logrotate remove old logs after reducing ‘rotate’ value

apache-2.2logginglogrotatepuppet

I'm trying to reduce the number of log files kept for Apache by reducing 'rotate 52' in /etc/logrotate.d/apache2 to 'rotate 8'. With weekly rotation this should keep 2 months of logs instead of a full year.

I deployed the new configuration via Puppet to our dozen servers and noticed it works differently than I expected. I expected it to remove the Apache error and access logs logs older than 8 weeks, i.e. 9-52. Instead it kept all old logs and simply doesn't create any new ones that would exceed that 8 week limit! Rotation 9 is now missing, but 10-52 still exist.

Searching the archive showed me this is known behaviour, e.g. Logrotate does not remove old logs

How do I get rid of the old logs easily? I want to avoid manually deleting them on each server. I don't have any orchestration software deployed yet, so Puppet will have to do.

My /etc/logrotate.d/apache2 file reads:

/var/log/apache2/*.log {
    weekly
    missingok
    rotate 8
    compress
    delaycompress
    notifempty
    create 644 root adm
    sharedscripts
    postrotate
        /etc/init.d/apache2 reload > /dev/null
    endscript
    prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
            run-parts /etc/logrotate.d/httpd-prerotate; \
        fi; \
    endscript
}

Best Answer

You could add a postrotate script to your current block in order to delete files older than n days with the following :

find /var/log/apache2 -maxdepth 1 -mtime +<n> -name "*.log.*.gz" -delete &>/dev/null

Wait for the next rotation then remove it and let logrotate do its normal job again.

Related Topic