6

I want to configure logrotate to handle /var/log/apache2/access.log separately from all other log files written to /var/log/apache2. Without specifying all the other logs by name explicitly, is there a way to get this to work?

I've read online that logrotate uses glob to pattern-match filenames, but I can't seem to write the correct pattern. I have tried many patterns, and they all error with:

considering log /var/log/apache2/!(access.log)
  log /var/log/apache2/!(access.log) does not exist -- skipping

The above pattern does work in bash, but only with shopt -s extglob. Is there a way to do this with logrotate, short of having apache write access.log to its own directory, or mangling the names of the non-access log files?

I have also tried getting this to work by using a prerotate script that fails when attempting to rotate access.log, but logrotate complains that I'm trying to rotate access.log twice with:

error: /etc/logrotate.d/apache2:16 duplicate log entry for /var/log/apache2/access.log

This is the closest I have gotten:

# rotate access.log every time logrotate is called
/var/log/apache2/access.log {
    missingok
    rotate 168
    compress
    create 640 root adm

    # rotate the log every time we call logrotate (size 0)
    size 0
    dateext
    dateformat .%Y%m%d.%s
    copytruncate
}

# rotate everything daily EXCEPT access.log
/var/log/apache2/*.log {
    daily
    missingok
    rotate 7
    compress
    create 640 root adm
    copytruncate

    # filter out access.log, which we want to rotate on its own schedule above
    nosharedscripts
    prerotate
        bash -c "[[ ! '$1' =~ /access.log ]]"
    endscript
}
Rob Crowell
  • 1,447
  • 3
  • 15
  • 25
  • I have a similar problem, arranging "sudo /etc/cron.daily/logrotate" to be a bash shell and using "shopt extglob" within that script did not work. logrotate -d reported `log /var/log/!(aaaa|bbb).log does not exist -- skipping` – ferg Sep 11 '18 at 15:54

1 Answers1

0

Are you running from cron? You could try to execute the cron script with bash -c ".." to make sure that logrotate is invoked from bash. Not sure if it helps tho.

Jonas Bergström
  • 741
  • 6
  • 15