I'm using rotatelogs to create my daily apache logs in format host.<day>.<month>.<year>.access.log
. Now I want to gzip and move log to different directory after it's been finished. How to do that?
Update: There was a little mistake. logrotate
-> rotatelogs
You could use rotatelogs option -p to use a program to compress the log after the rotation. (See for reference: https://httpd.apache.org/docs/2.4/programs/rotatelogs.html)
Example:
CustomLog "|bin/rotatelogs -p '/path/to/compress.sh' -l /var/log/logfile.%Y.%m.%d 86400"
compress.sh:
I've came up with the following script
And following cron entry
Logrotate will happily do the compressing for you. Just add:
To the logrotate config for apache. There is also a neat option that delays the compressing by one day:
As for moving them, logrotate can't help you but a cron job like this can:
I use find and crontab to accomplish this
# crontab -e
-mtime n
File’s data was last modified n*24 hours ago.! -name
"not ".*" hidden files! -name
"not ".gz" files which should already be compressed-print0 & -0
to ensure spaces or special characters are piped correctly to xargs -> gzip (for filename safety)find -exec could be used but has flaws where
| xargs
is more stable