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

link|improve this question

Thanks for correcting the question, I've removed my inaccurate answer. – jscott Dec 23 '11 at 15:23
You can write a simple script that gzip the old files and move/copy them to a different location. This script can be invoked in a daily cron job. – Khaled Dec 23 '11 at 15:37
I'm not familiar with shell scripts. Specifically, I don't know how to generate filename string for yesterday's date. – Poma Dec 23 '11 at 15:39
feedback

2 Answers

up vote 2 down vote accepted

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
link|improve this answer
feedback

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/
link|improve this answer
1  
logrotate offers the olddir options which can move the logs -- see man logrotate. The OP edited the question to indicate he's using rotatelogs not logrotate. – jscott Dec 23 '11 at 17:46
Ah, that means I'm barking up the wrong tree. And I didn't even know about the olddir option. The shell script above seems like the best solution. – Ladadadada Dec 23 '11 at 18:01
feedback

Your Answer

 
or
required, but never shown

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