4

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?

2
  • What does your current (non-working) logrotate file look like?
    – etagenklo
    Jan 22, 2015 at 14:44
  • logrotate is definitely your answer.
    – Hyppy
    Jan 22, 2015 at 14:51

1 Answer 1

8

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

1
  • 2
    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? Jul 3, 2015 at 7:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .