2

Is it possible to get logrotate to keep the last week of logs in the current file?

for example :

errors.log contains 30 days of logs

after log rotate executes , I would have :

errors.log with the last 7 days of log

and

errors.log.1.gz compressed with the 23 other days.

Is that possible?

Ant
  • 193
  • 1
  • 9
  • Logrotate doesn't provide for this directly. You could write a small script to do it, but it would have to be able to parse the log format and filter out entries older than your threshold. (if there's no timestamp on the log lines, this would be impossible) Can I ask why you'd want to do it this way? – SmallClanger Sep 01 '11 at 08:41
  • Truncating the active logfile is much easier than rewriting it, so I would not do it. But why do you care? If you have 7day logfiles you can search them with `zgrep bla errors.log.*` – eckes May 08 '19 at 05:10

1 Answers1

0
weekly
rotate 4
compress

followed by a postrotate script doing something like (untested):

TMP=$(mktemp)
zcat /var/log/errors.log.{4,3,2,1}.gz > $TMP
mv $TMP /var/log/errors.log.1
gzip /var/log/errors.log.1

Cheers.

Alien Life Form
  • 2,309
  • 2
  • 21
  • 32