Log File Archive Script Andrew Mallett | Revised September 2017

Log files contain a huge amount of valuable information about system activity, but over time can become unwieldly as their size grows. Some applications will auto-archive their logs. This script copies the day's Apache logs to a subdirectory and renames them based on the date..

/usr/local/apache/bin/apachectl stop

cd /usr/local/apache/logs

mv access_log ./bak/`date +%Y-%m-%d`_access_log

touch access_log

/usr/local/apache/bin/apachectl start

The first line stops the Apache Web service..

The script then changes to the directory containing the Apache logs.

The access_log is moved to the previously created bak directory and renamed based on the date, which looks something like 2010-05-01_access_log

A new access_log is created in the current directory using the touch command..

And the Apache Web server daemon is restarted. In reality this all happens within the blink of an eye.

This apachelogbak.sh runs from a cron job every night at 2359..

59 23 * * * /sc/apache/apachelogbak.sh >/dev/null

..so make sure full paths to all files are used in the script.

With modification, this script can be used with cron to backup or archive any files to any location on an automatic basis.


More custom scripts at Shell-scripts.com