How to force (or workaround) logrotate to move old logs to olddir on different physical disk?

Frodik picture Frodik · Mar 26, 2016 · Viewed 25.4k times · Source

I want logrotate to copy and truncate log file and use olddir on different physical disk. According to manual, olddir can't be on different physical disk because default behaviour of logrotate is to only rename original log file, which wouldn't be possible with different physical disk.

Well, but I am using copytruncate directive, which makes a copy of original file and then truncates original file. So there shouldn't be problem with moving newly copied file to different location on different physical disk.

But when I run logrotate, it still complains about logfile and olddir beeing on different devices.

Is there any way around it ? Possibly running some custom postrotate script which would move log files to desired location ?

Answer

Shubhangi picture Shubhangi · Mar 26, 2016

This is from logrotate man page.

  olddir directory <br>
        Logs  are moved into directory for rotation. **The directory must be on the 
        same physical device as the log file being rotated**, and is assumed to  be 
        relative  to  the  directory holding the log file unless an absolute path 
        name is specified. When this option is used all old versions of  the  log 
        end  up  in  directory.   This  option  may be overridden by the noolddir 
        option.

Existence of olddir on same physical device is limitation.

One can use below workaround.

set olddir to directory in same physical disk, and use postrotate script to move contents of olddir to directory on different physical device.

Example logrotate configuration file:

/var/log/httpd/*log {
        copytruncate
        olddir /var/log/httpd/olddir
        rotate 5
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        mv /var/log/httpd/olddir/* /vagrant/httpd/olddir/
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}