User Tools

Site Tools


mysql:logrotatorformysqlwindows

Log rotator for Mysql on Windows

This rotator can rotate logs and roll over 10 log files. Moreover, it can be easily configured to rotate as much logs as you want and to change the files to keep (10 by default).

'
' logrotate.vbs - file rotate for mysql 
'
option explicit
 
rotateFile 10, "PUT-HERE-YOUR-LOG-NAME.log"
rotateFile 10, "PUT-HERE-YOUR-LOG-NAME-2.err"
 
wscript.quit
 
 
 
function rotateFile( byval filesToKeep, byval logFile ) 
 
  dim index 
  dim fileFound 
  dim possibleNewName 
 
  index = 1 
  fileFound = true
  do while fileFound and index <= filesToKeep 
 
    possibleNewName = forgeRotateFile( logFile, index ) 
    fileFound = existsFile( possibleNewName ) 
 
    index = index + 1 
  loop ' fileFound 
 
  if fileFound then 
    ' destination file found, we have to discard the 
    ' last one and roll all the files 
    index = 1 
    deleteFile forgeRotateFile( logFile, index ) 
    do while index < filesToKeep 
      ' the file index + 1 will be renamed to index 
      renameFile forgeRotateFile( logFile, index + 1 ), forgeRotateFile( logfile, index ) 
      index = index + 1
    loop ' index <= 0 
  end if ' fileFound 
 
  ' move the file to the possibleNewName 
  ' is possible 
  renameFile logFile, possibleNewName
 
end function ' rotateFile
 
' creates a new file by 
' adding a number. Example:
' forgeRotateFile( "hello.log", 1 ) --> hello-1.log
function forgeRotateFile( byval filename, byval index ) 
 
    dim fso 
    dim path
    dim basename
    dim extension
 
   	set fso = createobject( "scripting.filesystemobject" ) 
 
    path = fso.getParentFolderName( filename ) 
    basename = fso.getBaseName( filename ) 
    extension = fso.getExtensionName( filename )
 
    ' append a "\" if the path is not empty
    if path <> "" then 
        path = path & "\"
    end if ' path <> ""
    ' append a "." if the extension is not empty 
    if extension <> "" then 
        extension = "." & extension
    end if  ' extension <> ""
    forgeRotateFile = path & basename & "-" & index & extension
 
end function ' forgeRotateFile
 
function renameFile( byval sourceFile, byval destFile ) 
	dim fso 
 
  on error resume next
 
	set fso = createobject( "scripting.filesystemobject" )
 
	if not fso.fileExists( destFile ) then 
		fso.moveFile sourceFile, destFile
	end if ' file exists
 
end function ' renameFile
 
 
 
function existsFile( byval filename )	
	dim out 
	dim fso 
 
  on error resume next
 
	set fso = createobject( "scripting.filesystemobject" )
 
	out = false 
	if fso.fileExists( filename ) then 
		out = true 
	end if ' 
 
	existsFile = out
end function ' existFile
 
 
function deleteFile( byval filename ) 
	dim fso 
 
  on error resume next 
 
	set fso = createobject( "scripting.filesystemobject" )
 
	if fso.fileExists( filename ) then 
		fso.deleteFile filename
	end if ' file exists
 
end function ' deleteFile
mysql/logrotatorformysqlwindows.txt · Last modified: 2014/12/24 10:42 (external edit)