Backup Apache log files

Aug 02, '02 09:14:55AM

Contributed by: juanfc

Apache writes down its events without tire and also regardless of the size and age of the log file. The file /var/log/httpd/access_log may reach indeterminate and unmanageable sizes. An easy way of controling the size is to back it up from time to time. Here is a script to make this very easy:

#!/bin/tcsh
if (! -e /var/log/httpd/weblogsbaks/ ) \
sudo mkdir /var/log/httpd/weblogsbaks/
sudo mv /var/log/httpd/access_log \
/var/log/httpd/weblogsbaks/`date +%y%m%d.%H%M%S`
Save the script somewhere on your path and make it executable (chmod 755 Script_Name). I decided to rename the log files (to yearMonthDay.HoursMinutesSeconds) inside of the dedicated backup folder for easy future reference. Obviously, inside that folder all the files are backups of access_log. To prove the script worked, just "ls" on both the new and the old directories and you'll see that the file has been moved.

Note that Apache must be restarted to open a new log file (instead of resuming writing to the previously moved file). You can do this either through the GUI or via the command line with "sudo apachectl gracefeul".

[Editor's note: There's a thread on this on the macosxhints' forum site, with a "new and improved" script that will lotate a number of log files...]

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20020802091455601