Routinely Rotate Web Logs with Archive

Simple script. Basically, you give it a folder (in this case /user/local/apache2/logs) and assuming you have all the logs for each web in its own folder, it’ll rotate them all, restart apache and then start gzipping them.

#!/bin/bash

for i in /usr/local/apache2/logs/* ; do
if [ -d $i ] ; then
mv $i/access.log $i/`date +%y%m%d`.log
touch $i/access.log

mv $i/error.log $i/`date +%y%m%d`e.log
touch $i/error.log
fi
done

mv /usr/local/apache2/logs/access_log /usr/local/apache2/logs/`date +%y%m%d`.log
mv /usr/local/apache2/logs/error_log /usr/local/apache2/logs/`date +%y%m%d`e.log

/usr/local/apache2/bin/apachectl graceful

for i in /usr/local/apache2/logs/* ; do
if [ -d $i ] ; then
gzip $i/`date +%y%m%d`.log
gzip $i/`date +%y%m%d`e.log
fi
done

gzip /usr/local/apache2/logs/`date +%y%m%d`.log
gzip /usr/local/apache2/logs/`date +%y%m%d`e.log

exit 0

Leave a Reply

Your email address will not be published. Required fields are marked *

*