Monday, January 7, 2013

logrotate with date extensions

My recent problem?  The version of logrotate (logrotate 3.7.1) I'm running on an old web server doesn't support the "dateext" option.  I wanted to have my Apache logs rotate with a date as its extension instead of ".#".  Here's a sample of my /etc/logrotate.d/httpd that does this, if you have the same problem.

# cat /etc/logrotate.d/httpd
/var/log/httpd/*log {
    rotate 5
    daily
    create
    missingok
    ifempty
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
    endscript
    lastaction
        DATE=`date +%F`;
        for i in `ls /var/log/httpd/*.1`
          do
            mv $i ${i%.1}-$DATE
          done
    endscript
}


Info on manipulating shell variable strings can be found via http://tldp.org/LDP/abs/html/string-manipulation.html.  You can see I'm removing the ".1" substring in the mv step so that my logs do not look like log.1-$DATE.