Solved

logrotate for Windows 2003

Posted on 2007-11-21
6,997 Views
Last Modified: 2011-11-17
I am looking for some tool similar to logrotate in linux, but for Windows 2003 Server.

Does any one knows a tool like that?


Thanks in advance and best regards,
JJ.-
0
Question by:begar
    3 Comments
     
    LVL 86

    Accepted Solution

    by:
    AFAIK Apache for Win32 comes with a 'logrotate' that can be used on Windows, see http://httpd.apache.org/download.cgi
    0
     

    Expert Comment

    by:sas900
    Copy the following lines in a bat- or cmd-files and use scheduled tasks.

    I've checked it for german dates.
    @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
    

    Open in new window

    0
     

    Expert Comment

    by:sas900
    Sorry, i've forgotton...

    You need a 7za.exe at the same directory where your bat-file exists. For compression...
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
    Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
    Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

    703 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now