I have a log file, which is supposed to rotate after reaching a particular size (say 50MB). After the rotation, it should be compressed and date has to be appended to the rotated file name. Now, the rotated file must be copied to a file server.

For this, I have written a configuration which fails to work.

/var/log/test.log {
    size 50M
    rotate 6
        rsync -avzr /var/log/test.log* REMOTE-HOST:/path/to/directory/

When I would run ls /var/log/test.log* directory (on local system), I could find the rotated file test.log-20150211.gz and an empty file test.log. Where as, on REMOTE-HOST, the uncompressed version of rotated file test.log-20150211 and the empty log file test.log was found.

My concern is that,

  1. Why compressed log file was not transferred to the REMOTE-HOST?
  2. What should be done in order to transfer ONLY the rotated file?


I have also found that, after the logs were rotated, the service syslog stopped (logging process was found stopped, when check by running tailf test.log). The service was needed to be restarted again. Should I also add service syslog restart in the postrotate section?


Why compressed log file was not transferred to the REMOTE-HOST?

Compression is pretty much the last thing to happen so postrotate is still happening before logrotate has gotten that far. You can check the man page for their example of postrotate:

After the log file has been rotated (but before the old version of the log has been compressed), the command /sbin/killall -HUP syslogd [from the postrotate script] will be executed.

You may be better off running the rsync as a cronjob.

What should be done in order to transfer ONLY the rotated file?

Your rsync is just matching the original file name (since * will also match for files with no more characters) Use a more specific source name to eliminate that match:

rsync -avzr /var/log/test.log-* REMOTE-HOST:/path/to/directory/
| improve this answer | |
  • Is there something to be done in configuration, to avoid extra overheads (like cronjobs)? – Mandar Shinde Feb 11 '15 at 12:16
  • 1
    According to this lastaction runs after compression. You may try that. – Bratchley Feb 11 '15 at 12:30
  • I suppose, sharedscripts option would not work for lastaction, but it does for postrotate. Please clarify. – Mandar Shinde Feb 12 '15 at 3:49
  • Would you really need -r? A logfile is never a folder, or am I misunderstanding the option. – atripes Aug 17 '17 at 9:40
  • @AlexAffe I was just repeating their command, but it's possible for log files to be separated out into individual directories. In this case the file name makes it look like they're just files but ¯_(ツ)_/¯ – Bratchley Aug 21 '17 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.