How can I get logrotate dateext to reflect the log date rather than the rotation date?
My Apache logrotate config looks like this:
/var/log/http/*log {
monthly
dateext
dateformat .%Y.%m
[... rest stripped for brevity ...]
}
This works great, except that the date on the rotated filename is one period later than the period actually covered by the logs, for example error_log.2012.09
covers 2012-08-01 to 2012-08-31.
I realize that there are other options for Apache (eg. cronolog), but I have a bunch of other logs that I also need to rotate, and logrotate is really exactly what I need apart from this one issue.
Is there a way to get logrotate to use a date offset -- or, even better, figure out the previous time period -- when generating the rotated filename?
Added in 3.8.0 (although looking at the svn history, it looks like it was actually added in 3.7.9):
http://svn.fedorahosted.org/svn/logrotate/tags/r3-8-0/CHANGES
- added "dateyesterday" option (see man page)
if logrotate in your distrib doesn't have 'dateyesterday' option yet, you can use script like the following:
LOGFILE="$1"
LOGMTIME="$(env LC_ALL='C' stat $LOGFILE |awk '/^Modify/{print $2}')"
LOGSTART=$(( $(date +%Y%m%d -d "${LOGMTIME}") - 1 ))
mv $LOGFILE ${LOGFILE%-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]}-$LOGSTART
Notes:
- script will work right only in case of 'daily' option
- if operating on multiple files, wrap the code block in "for LOGFILE in $@" loop.
- sequence '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' is the logrotate's default glob pattern, see description of dateext option of logrotate man.