I wrote a vbs for a Windows 2003 box once. It uses zip.exe for compression (found at info-zip.org) and rotates apache logs and php logs. Logs are rotated when larger than MAX_SIZE. It deletes the oldest logs if log count passes MAX_ROTATIONS.
Please provide feedback on the script.
option explicit
const DEBUG_MODE = false
const MAX_ROTATIONS = 10
const MAX_SIZE = 2097152 ' 2MB
const WEB_LOGS = "c:\path\to\site\logs"
const PHP_LOG = "c:\path\to\phplog"
const APACHE_LOGS = "C:\path\to\Apache2\logs"
const APACHE_SERVICE ="Apache2.2" ' Name of the apache service for restart
const ZIP_APP = "c:\path\to\zip.exe"
const LOGROTATE_LOG = "c:\tmp\logrotate.log"
dim aLogs
aLogs = Array("\error.log","\transfer.log","\sec.log","\phperror.log")
dim oFSO
set oFSO = CreateObject("Scripting.FileSystemObject")
if (not DEBUG_MODE) then
dim oLogFile
set oLogFile = oFSO.CreateTextFile(LOGROTATE_LOG, 2, True)
end if
dim bHasRotated
bHasRotated = false
Print "Starting log rotation"
Print "====================="
ManageWebLogs()
ManageApacheLogs()
ManagePhpLog()
if (bHasRotated = true) then
Print "====================="
RestartService APACHE_SERVICE
end if
Print "====================="
Print "Log rotation finished"
if (not DEBUG_MODE) then
oLogFile.Close
set oLogFile = nothing
end if
set oFSO = nothing
'*****************************************************************************
' Loop through all the subfolders in the weblog directory
sub ManageWebLogs()
dim oLogDirs
set oLogDirs = oFSO.GetFolder(WEB_LOGS)
dim oLogDir
for each oLogDir in oLogDirs.SubFolders
Print "In " & oLogDir.Name
RotateLogs(oLogDir)
next
set oLogDir = nothing
end sub
'*****************************************************************************
' Loop through the log files in the Apache logs directory
sub ManageApacheLogs()
dim oLogDir
set oLogDir = oFSO.GetFolder(APACHE_LOGS)
Print "In " & oLogDir.Name
RotateLogs(oLogDir)
set oLogDir = nothing
end sub
'*****************************************************************************
' Loop through the log files in the Apache logs directory
sub ManagePhpLog()
dim oLogDir
set oLogDir = oFSO.GetFolder(PHP_LOG)
Print "In " & oLogDir.Name
RotateLogs(oLogDir)
set oLogDir = nothing
end sub
'*****************************************************************************
' Traverse through each of the log file types and check if they need rotation
sub RotateLogs(ByVal oFolder)
dim sLog
dim oLog
for each sLog in aLogs
if oFSO.FileExists(oFolder.Path & sLog) then
set oLog = oFSO.GetFile(oFolder.Path & sLog)
if (oLog.Size > MAX_SIZE) then
RotateLog oFolder.Path & sLog
ArchiveLog oFolder.Path & sLog
bHasRotated = true
end if
end if
next
set oLog = nothing
end sub
'*****************************************************************************
' Rotates the given log, by incrementing the file name
sub RotateLog(ByVal sLog)
dim i
dim sOldFile, sNewFile
for i = MAX_ROTATIONS to 1 step -1
sOldFile = sLog & "." & i & ".zip"
sNewFile = sLog & "." & (i+1) & ".zip"
if oFSO.FileExists(sOldFile) and i = MAX_ROTATIONS then
' Delete zipfile
Print "-- Deleting " & sOldFile
oFSO.DeleteFile(sOldFile)
elseif oFSO.FileExists(sOldFile) then
' Rename zipfile
Print "-- Renaming " & sOldFile & " to " & sNewFile
oFSO.MoveFile sOldFile, sNewFile
end if
next
end sub
'*****************************************************************************
' Zips the current log
sub ArchiveLog(ByVal sLog)
Dim oShell
Set oShell = CreateObject("WScript.Shell")
dim sZipFile
sZipFile = sLog & ".1.zip"
Print "-- Archiving " & sLog & " to " & sZipFile
oShell.Run "cmd /c " & ZIP_APP & " -jq " & sZipFile & " " & sLog, 0, true
oFSO.DeleteFile(sLog)
set oShell = nothing
end sub
' ****************************************************************************
' Restarts a given service (in our case Apache)
private sub RestartService( _
ByVal sService _
)
Dim oShell
Set oShell = CreateObject("WScript.Shell")
' Service stopped with 'Net' command
oShell.Run "cmd /c net stop " & sService, 0, true
Print sService & " service stopped"
' Service started with 'Net' command
oShell.Run "cmd /c net start " & sService, 0, true
Print sService & " service restarted"
set oShell = nothing
end sub
'*****************************************************************************
' Echoes out the given message if in debug mode
sub Print(ByVal sMsg)
if (DEBUG_MODE) then
wscript.echo sMsg
else
oLogFile.WriteLine sMsg
end if
end sub