logrotate/apache and web hosting
Logrotate is a simple program that can perform simple but much-needed features, such as keeping log files from growing to 5gb. Heh. Anyhow, usually, it’s a simple matter of pointing it to the files and letting it do it’s thing.
What if you are a hosting company and you like to allow each user/domain you host access to their own logfiles in their own log directory? Well, without rotation, it’s possible that their logs can grow to gargantuan proportions, for a variety of reasons.
Manually adding and deleting the four or five line entries for each domain could get cumbersome, if your users add/delete domains as often as ours do. This shell script I wrote yesterday helps. It’s got a static section for the ‘main’ apache logs (like most non-hosting apache server setups), and then it dynamically generates the rest given a listing of directories in the main ‘www’ directory. This is not foolproof, and picks up some directories that do not correspond to domains, but there’s also a flag that tells logrotate not to bitch if it encounters them.
#
# ask ryan how this works
# or blame him if it’s broken
#
# if this script is to be cron’d
# just comment out the 3 lines with # SILENT after them
# so the script doesn’t spit out crap
# various variables, only edit the one beginning with ‘file’
file=/www/test2.txt # edit this to match logrotate file
backup=/tmp/test2-bak
short=`basename $file`
wwwdir=`ls -Ap /www/ | grep "/"`
# backup old logrotate file, create new one
mv $file $backup
touch $file
echo "—– populating $short —–" # SILENT
# add static header information
# this is constant between updates
echo "### Apache Access Log" >> $file
echo " " >> $file
echo "/www/logs/*log {" >> $file
echo " missingok" >> $file
echo " postrotate" >> $file
echo " /usr/bin/killall -HUP httpd 2> /dev/null || true" >> $file
echo " endscript" >> $file
echo "}" >> $file
echo " " >> $file
echo " " >> $file
echo "### WebSite Access Logs" >> $file
# now to read all the apparent directories in
# /www/ and treat them like domains, add them
# to the list! note to ryan: further filter later
for domain in $wwwdir
do
echo " " >> $file
echo "/www/"$domain"logs/access_log {" >> $file
echo " missingok" >> $file
echo " postrotate" >> $file
echo " /usr/bin/killall -HUP httpd 2> /dev/null || true" >> $file
echo " endscript" >> $file
echo "}" >> $file
echo " `basename $0`: Added ‘$domain‘ to log rotation!" # SILENT
done
rm -f $backup
echo "—– all done! —–" # SILENT
Where I work, after we were satisfied that it was doing it’s job when we ran it manually, it was added to cron.weekly.












