How to gzip logs created by 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)
-p program
If given, rotatelogs will execute the specified program every time a new log file is opened. The filename of the newly opened file is passed as the first argument to the program. If executing after a rotation, the old log file is passed as the second argument. rotatelogs does not wait for the specified program to terminate before continuing to operate, and will not log any error code returned on termination. The spawned program uses the same stdin, stdout, and stderr as rotatelogs itself, and also inherits the environment.
Example:
CustomLog "|bin/rotatelogs -p '/path/to/compress.sh' -l /var/log/logfile.%Y.%m.%d 86400"
compress.sh:
#!/bin/bash
file_to_compress="${2}"
compress_exit_code=0
if [[ "${file_to_compress}" ]]; then
echo "Compressing ${file_to_compress} ..."
tar --gzip --create --file "${file_to_compress}.tar.gz" "${file_to_compress}"
compress_exit_code=${?}
if [[ ${compress_exit_code} == 0 ]]; then
echo "File ${file_to_compress} was compressed."
else
echo "Error compressing file ${file_to_compress} (tar exit code: ${compress_exit_code})."
fi
fi
exit ${compress_exit_code}
I've came up with the following script
#!/bin/sh
for file in $(ls /var/log/apache2/*.$(date +"%y.%m.%d" --date="1 day ago").access.log); do
gzip $file
mv $file.gz /var/log/apache2/archive
done;
And following cron entry
15 0 0 0 0 root /mypath/myscript.sh
Logrotate will happily do the compressing for you. Just add:
compress
To the logrotate config for apache. There is also a neat option that delays the compressing by one day:
delaycompress
As for moving them, logrotate can't help you but a cron job like this can:
@daily mv /var/log/apache/*.gz /var/log/archive/