Apache rotate Access and Error logs Windows

后端 未结 4 2221
醉梦人生
醉梦人生 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:33

    Here's the DOS batch file, modified as annotated. I run it weekly and it keeps 8 weeks of zipped backups. You'll need to install 7 zip.

    I haven't parametrised the paths, feel free to.


    @echo off
    
    :: Name - svrlogmng.bat
    :: Description - Server Log File Manager
    ::
    :: History
    :: Date         Authory    Change
    :: 22-May-2005  AGButler   Original
    :: 14-Jan-2008  AIMackenzie Changed net stops and paths where necessary
    
    :: ========================================================
    :: setup variables and parameters
    :: ========================================================
    
    :: generate date and time variables
    for /f "tokens=2,3,4 delims=/ " %%i in ('date /T') do set trdt=%%k%%j%%i
    for /f "tokens=1,2 delims=: " %%i in ('time /T') do set trtt=%%i%%j
    set nftu=%trdt%%trtt%
    
    :: set the Number Of Archives To Keep
    set /a noatk=8
    
    :: ========================================================
    :: turn over log files
    :: ========================================================
    
    :: change to the apache log file directory
    cd /D "D:\Program Files\Apache Software Foundation\Apache2.2\logs\"
    
    :: stop Apache Service, Move log files and restart Apache Service
    "D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -k stop
    
    echo %nftu% >> access.log
    move "D:\Program Files\Apache Software Foundation\Apache2.2\logs\access.log" "D:\Program Files\Apache Software Foundation\Apache2.2\logs\%nftu%_access.log"
    
    echo %nftu% >> error.log
    move "D:\Program Files\Apache Software Foundation\Apache2.2\logs\error.log" "D:\Program Files\Apache Software Foundation\Apache2.2\logs\%nftu%_error.log"
    
    "D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -k start
    
    :: ========================================================
    :: zip todays Access and Error log files, then delete old logs
    :: ========================================================
    
    :: zip the files
    "D:\Program Files\7-Zip\7z.exe" a -tzip %nftu%_logs.zip %nftu%_access.log %nftu%_error.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
    

提交回复
热议问题