Apache rotate Access and Error logs Windows

后端 未结 4 2219
醉梦人生
醉梦人生 2021-02-14 18:09

How can I rotate the Apache Access and Error logs on a Window 2000 box?

I include my batch file below as an answer.

Is there a way of doing this directly via the

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-14 18:27

    I've a little bit extend the the bat-script. You can use it for english and german dates. You will need 7za.exe at the same directory like script. The log-dir and -files for rotation are explicitly settable.

    @echo off
    SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
    :: ========================================================
    :: setup variables and parameters
    :: ========================================================
    
    :: USE Service Display Name, Services are space-separated -> Syntax: "Display Name 1" "Display Name 2"
    SET ROTATE_SERVICES="Apache2.2"
    
    :: setting LOG-directory, log-files in this directory should be rotate
    SET ROTATE_LOGDIR=F:\xampp\apache\logs
    
    :: files which should rotate (space separated)
    SET ROTATE_FILES=access.log error.log ssl_request.log
    
    :: SET the Number Of Archives To Keep
    SET /a keptarchives=5
    
    :: SET delimiter for date format (english "/", german ".")
    SET DATEDEL=.
    :: ========================================================
    :: DO NOT CHANGE ANYTHING
    :: ========================================================
    
    :: Check for existing Log-directory
    IF NOT EXIST "%ROTATE_LOGDIR%" (
        CALL :LOG Please check your paths to Log Directory
        PAUSE
        GOTO :EOF
    )
    
    :: Check for existing Log-files
    FOR %%d IN (%ROTATE_FILES%) DO (
        IF NOT EXIST "%ROTATE_LOGDIR%\%%d" (
            CALL :LOG File %ROTATE_LOGDIR%\%ROTATE_LOGFILES% does not exist!
            PAUSE
            GOTO :EOF
        )
    )
    
    :: generate date and time variables for execution
    FOR /f "tokens=1,2,3 delims=%DATEDEL% " %%i IN ('date /T') DO SET execdate=%%k%%j%%i
    FOR /f "tokens=1,2 delims=: " %%i IN ('time /T') DO SET exectime=%%i%%j
    SET fullexectime=%execdate%_%exectime%
    :: ========================================================
    
    
    :: ========================================================
    :: Operations
    :: ========================================================
    
    FOR %%d IN (%ROTATE_SERVICES%) DO (
        NET STOP %%d
    )
    
    FOR %%d IN (%ROTATE_FILES%) DO (
        cd /d %ROTATE_LOGDIR%
        IF NOT EXIST OLD (MKDIR OLD) 
        move %%d %ROTATE_LOGDIR%\OLD\%fullexectime%_%%d
    )
    
    FOR %%d IN (%ROTATE_SERVICES%) DO (
        NET START %%d
    )
    
    :: ========================================================
    :: ZIP - LOGFILES
    :: ========================================================
    cd /d %ROTATE_LOGDIR%\OLD
    CALL "%~dp0\7za.exe" a %ROTATE_LOGDIR%\OLD\%fullexectime%_log.zip %ROTATE_LOGDIR%\OLD\%fullexectime%_*.log
        IF %ERRORLEVEL% NEQ 0 (
            CALL :LOG Error while compressing log-file. Log will not deleted and not rotated. Check your OLD-directory!
            PAUSE
            GOTO :EOF
        )
    del /Q %fullexectime%_*.log
    
    :: ========================================================
    :: ROTATE - ZIPPED LOGFILES
    :: ========================================================
    
    :: make list of archive zip files
    type NUL > arclist.dat
    for /F "tokens=1,2 delims=[] " %%i in ('dir /B *_log.zip ^| find /N "_log.zip"') do echo  %%i = %%j>> arclist.dat
    
    :: count total number of files
    for /F "tokens=1 delims=" %%i in ('type arclist.dat ^| find /C "_log.zip"') do set tnof=%%i
    
    :: setup for and create the deletion list
    set /a negtk=%keptarchives%*-1
    set /a tntd=%tnof% - %keptarchives%
    
    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 "_log.zip" dellist.dat') do del /Q %%i
    
    :: remove temp files
    del /Q arclist.dat
    del /Q dellist.dat
    
    
    GOTO :EOF
    
    :LOG
        SET MSG=[%DATE%, %TIME: =0%] %*
        ECHO.%MSG%
        SET MSG=
        GOTO :EOF
    pause
    

提交回复
热议问题