[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Script to rotate virtual-server logs Apache2



Hello,

I the Apache 1.3 servers there was a way to rotate logs in the log
directories for each of the virtual servers. I could not find a way to do
this in the New Apache2 server. So I hacked up the old apache cron script
and put this into the /etc/cron.weekly/ directory and it seems to do the
trick now.

I thought I would post that script here for anyone that wants to use it.
Do as you like to it, if you make any improvements to it please post it
here.

Thanks,

Ken

---------------------------------------------------------------------------

#!/bin/sh
#
# apache-virtual-logs    Rotate virtual-logs on weekly basis. Put into the /etc/cron.weekly/.
#
#
#           Ken Rea - 6/07
#           This code is hacked from a previous apache log cron for version 1.3 ( I think),
#           The previous author is Johnie Ingram <johnie@debian.org>
#

APACHE_CHOWN_LOGFILES=1

# How many logs to keep
APACHE_OLD_LOGS=5


# This next command will read the links in the /etc/apache2/sites-enabled directory
# I made it so my links are domain names so it will not read the "000-default" link
# but will read a link "somedomain.com" This is because the rotate "logrotate.d/apache"
# script already takes care of this default directory.

    LOGS=$(awk '$1 ~ /^\s*[A-Za-z]*Log$/ && $2 ~ /^\// {print $2}' \
        /etc/apache2/sites-enabled/*.* | sort -u)

    if [ "$LOGS" = "" ]
    then
      exit 0
    fi


    if [ -f /var/log/apache2/rewrite.log ]
    then
        LOGS="$LOGS /var/log/apache2/rewrite.log"
    fi

    SERVERROOT=$(awk '$1 == "ServerRoot" { print $2; exit }' \
        /etc/apache2/*.conf)
    if [ "$SERVERROOT" != "" ]
    then
        SERVERROOT=$(echo $SERVERROOT | sed "s/\"//g")
        cd $SERVERROOT
    fi

    USR=$(awk '$1 == "User" { print $2; exit }' /etc/apache2/*.conf)
    if [ "$USR" = "" ]; then USR="root"; fi
    if [ "$USR" = "#-1" ]; then USR="root"; fi

    GRP=$(awk '$1 == "Group" { print $2; exit }' /etc/apache2/*.conf)
    if [ "$GRP" = "" ]; then GRP="www-data"; fi
    if [ "$GRP" = "#-1" ]; then GRP="www-data"; fi

    GRP=$(echo $GRP | sed "s/#//g")

    for LOG in $LOGS
    do
        if [ -f $LOG ]
        then
            if [ "$APACHE_CHOWN_LOGFILES" = "1" ]
            then
                savelog -c $APACHE_OLD_LOGS -m 664 -u $USR -g $GRP \
                    $LOG > /dev/null
            else
                savelog -c $APACHE_OLD_LOGS -m 644 -u root -g root \
                $LOG > /dev/null
            fi
        fi
    done

    # Send a reload signal to the apache server.
    if [ -x /usr/bin/killall ]
    then
        /usr/bin/killall -HUP apache2
    else
        /etc/init.d/apache2 reload > /dev/null
    fi





Reply to: