Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|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

2 Answers 2

up vote 4 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
share|improve this answer

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/
share|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
    
logrotate is not rotatelogs! –  MUY Belgium Apr 26 '13 at 9:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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