3

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?

johnwards
  • 765
  • 1
  • 9
  • 13

1 Answers1

7

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.)

Craig Miskell
  • 4,216
  • 1
  • 16
  • 16
  • According to `find`'s manpage `[The -daystart] option only affects tests which appear later on the command line.`, so shouldn't this be `-daystart -mtime +1` and `-daystart -mtime +3`, respectively? – Adrian Frühwirth Jul 03 '15 at 07:03