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

2

12

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 ?

Medarda answered 26/3, 2016 at 12:4 Comment(0)
P
21

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
}
Planospore answered 26/3, 2016 at 14:34 Comment(4)
This is not a full workaround. I tried a postrotate move and then logrotate tries to work on logfiles in olddir which are not there anymore, moved away to the archive location. As I do not have enough space for both actual logs and archive on the original device I will now try to have both actual and archive on the archive device.Varipapa
Why not symlink "olddir" to a directory on another partition? Then logrotate will also understand, no postrotate workarounds required. askubuntu.com/questions/56339/…Multiphase
If you move the contents of the olddir to another location with mv, how do you prevent the logs in the new place from accumulating?Martellato
@ChrisWoods The symlink does not seem to work in my case.Martellato
C
2

Nowadays there is attributes copy, copytruncate and copyrename that can be used in combination with olddir to move a file to separate device.

Cairn answered 29/4, 2021 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.