Rotate log files with timestamp in file name

log-fileslogginglogrotate

I have a log directory that my app writes into. It creates a log file like this:

2015-01-22-10-full-activity.log
2015-01-22-11-full-activity.log
2015-01-22-12-full-activity.log
2015-01-22-13-full-activity.log

I want to compress every file except the latest file and delete any file older than 3 days.

I thought logrotate would be able to do this for me, but I can't fathom it out and potentially it is because I'm naming my activity log with the time in its name.

Any ideas?

Best Answer

If you're already creating the files with the date in the name, logrotate isn't the answer; it is based around the idea of the application always writing to the same log file (e.g. /var/log/app/output.log), and then logrotate takes care of renaming/compressing the files and telling the app to re-open the original target file again.

In this case, perhaps a pair of cron jobs using "find" with -mtime, e.g.:

1 0 * * * root find /path/to/logs/*.log -mtime +1 -daystart -exec gzip {} \;

2 0 * * * root find /path/to/logs/*.log.gz -mtime +3 -daystart -delete

You may wish to fiddle with the numbers on -mtime and the use of -daystart (or not) to get the precise results you want (depends on how you want to count 'number of days' etc.)