Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Backup Apache log files UNIX
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:
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...]
  • Currently 2.67 / 5
  You rated: 2 / 5 (3 votes cast)

Backup Apache log files | 4 comments | Create New Account
Click here to return to the 'Backup Apache log files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
The easy way of doing this
Authored by: simonboyle on Aug 02, '02 02:51:22PM

I find that the easiest way to stop log files becoming unmanageable is to use the supplied log rotation script. To activate it, in the file
change the line

CustomLog "/private/var/log/httpd/access_log" common


CustomLog "|/usr/sbin/rotatelogs /private/var/log/httpd/access_log 86400" combined

(all on one line). This will start a new log file every 86400 seconds (1 day). The combined log format yields slightly more useful information for stats. See the supplied manual for details (/Library/Documentation/Services/apache/logs.html). On MacOS X, I find it's normally safer to use the sharing preference panel to start and stop the server unless you've compiled and installed your own version. Hope this helps

[ Reply to This | # ]
Backup Apache log files
Authored by: WAW401 on Aug 02, '02 11:16:31PM

Is there anything improper about using the hint script w/ "sudo apachectl graceful" as the last line rather than the piped /usr/sbin/rotatelogs - especially on a low traffic server? I like the idea of defining my own backup filename and frequency (like 1st of the month, rather than # of seconds). Also, if cron runs this script and the script is owned by root, does it need the sudo command?

[ Reply to This | # ]
Backup Apache log files
Authored by: eo on Aug 03, '02 11:07:51AM

Read: the log rotation section of Apache's manual.

Also check out cronolog.

[ Reply to This | # ]
Backup Apache log files
Authored by: simonboyle on Aug 03, '02 11:09:09AM

It's generally not a good idea to have automated scripts running/owned by by root unless you really have to, especially on a machine that you're exposing to the net. Change permissions on the directory to be writeable by a different user, who doesn't have permission to do much, and run the script as that. The filenames output by rotatelogs are the filename supplied with a time/date stamp in unix epoch format. From managing servers for a while, I found rotating daily/weekly is preferable to monthly, as you can miss occasional corruption if you let it go too long, and most stats packages are as happy working on collections of files as individual files, and you can write a script to compress the inactive files with gzip/perform backups on yesterdays files. Crucially, there's no need to interrupt the server if it's doing the rotation for you.

[ Reply to This | # ]