Rotar Logs de Apache 2 en Windows

Este post es una modificación de este post donde se muestra un archivo bat para rotar los logs de apache. Es más o menos la misma utilidad que existe en linux o unix conocida como logsrotate. En este se han agregado las siguientes cuestiones:

  • compatibilidad con apache 2
  • envío de un email al finalizar el proceso
  • disponible para todos los archivos logs de apache no solo access.log y error.log

7ziplogo Primero antes de ejecutar el bat es necesario descargar el programa 7-zip desde aquí, instalarlo y copiar el archivo 7z.exe del directorio donde lo has instalado al directorio system32 de Windows con el nombre 7za.exe.

Luego de este paso creamos el archivo svrlogmng.bat con el contenido del script y creamos la tarea diaria para ejecutarlo con Scheduled Tasks de Windows.

El código quedaría de la siguiente forma:

@echo off & setlocal
:: Name - svrlogmng.bat
:: Description - Server Log File Manager
::
:: History
:: Date         Author             Change
:: 22-May-2005  AGButler           Original
:: 04-Mar-2009  AGButler           Updates - thanks to Geoff Brisbine
:: 23-Dic-2009  Reynier de la Rosa        Updates
::

:: ========================================================
:: setup variables and parameters
:: ========================================================

:: generate date and time variables
:: may need to swap around the k j i variables to get the yyyymmdd format
for /F "tokens=6,7,8 delims=/ " %%i in ('echo.^|date^|find "current" ') do set trdt=%%k%%j%%i
:: the following should get the windows time to get the hhmmsstt format
for /F "tokens=5,6,7,8 delims=:. " %%i in ('echo.^|time^|find "current" ') do set trtt=%%i%%j%%k%%l
set nftu=%trdt%%trtt%

:: set the Number Of Archives To Keep
set /a noatk=7

:: ========================================================
:: turn over log files
:: ========================================================

:: change to the apache log file directory
cd "C:\Program Files\Apache Group\Apache2\logs\"

:: stop Apache Service, Move log files and restart Apache Service
NET STOP "Apache2"

echo %nftu% >> access.log
move "C:\Program Files\Apache Group\Apache2\logs\access.log" "C:\Program Files\Apache Group\Apache2\logs\%nftu%_access.log"

echo %nftu% >> error.log
move "C:\Program Files\Apache Group\Apache2\logs\error.log" "C:\Program Files\Apache Group\Apache2\logs\%nftu%_error.log"

echo %nftu% >> rewrite.log
move "C:\Program Files\Apache Group\Apache2\logs\rewrite.log" "C:\Program Files\Apache Group\Apache2\logs\%nftu%_rewrite.log"

echo %nftu% >> referer.log
move "C:\Program Files\Apache Group\Apache2\logs\referer.log" "C:\Program Files\Apache Group\Apache2\logs\%nftu%_referer.log"

echo %nftu% >> agent.log
move "C:\Program Files\Apache Group\Apache2\logs\agent.log" "C:\Program Files\Apache Group\Apache2\logs\%nftu%_agent.log"

NET START "Apache2"

:: ========================================================
:: zip todays Access and Error log files, then delete old logs
:: ========================================================

:: zip the files
7za a -tzip %nftu%_logs.zip %nftu%_access.log %nftu%_error.log %nftu%_rewrite.log %nftu%_referer.log %nftu%_agent.log

:: del the files
del /Q %nftu%_*.log

:: ========================================================
:: rotate the zip files
:: ========================================================

:: make list of archive zip files
type NUL > arclist.dat
for /F "tokens=1,2 delims=[] " %%i in ('dir /B *_logs.zip ^| find /N "_logs.zip"') do echo  %%i = %%j>> arclist.dat

:: count total number of files
for /F "tokens=1 delims=" %%i in ('type arclist.dat ^| find /C "_logs.zip"') do set tnof=%%i

:: setup for and create the deletion list
set /a negtk=%noatk%*-1
set /a tntd=%tnof% - %noatk%

type NUL>dellist.dat
for /L %%i in (%negtk%,1,%tntd%) do find " %%i = " arclist.dat >> dellist.dat

:: del the old files
for /F "tokens=3 delims= " %%i in ('find "_logs.zip" dellist.dat') do del /Q %%i

:: remove temp files
del /Q arclist.dat
del /Q dellist.dat

:: ========================================================
:: send email to admin
:: ========================================================

set tempmail=%temp%\tempmail.%random%.txt
echo To: "" ^<usuario@host.extensión^> > %tempmail%
echo From: "" ^<usuario@host.extensión^> >> %tempmail%
echo Subject: Rotar Logs y Reinicio Servicio Apache>> %tempmail%
echo.>>%tempmail%
echo Servicio Apache reiniciado en el servidor.>> %tempmail%
echo %var%>> %tempmail%
move %tempmail% c:\inetpub\mailroot\pickup
set tempmail=
endlocal

:end

Comentarios