code_junkie

Apache rotate Access and Error logs Windows

Nov 12th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. @echo off
  2.  
  3. :: Name - svrlogmng.bat
  4. :: Description - Server Log File Manager
  5. ::
  6. :: History
  7. :: Date Authory Change
  8. :: 22-May-2005 AGButler Original
  9. :: 14-Jan-2008 AIMackenzie Changed net stops and paths where necessary
  10.  
  11. :: ========================================================
  12. :: setup variables and parameters
  13. :: ========================================================
  14.  
  15. :: generate date and time variables
  16. for /f "tokens=2,3,4 delims=/ " %%i in ('date /T') do set trdt=%%k%%j%%i
  17. for /f "tokens=1,2 delims=: " %%i in ('time /T') do set trtt=%%i%%j
  18. set nftu=%trdt%%trtt%
  19.  
  20. :: set the Number Of Archives To Keep
  21. set /a noatk=8
  22.  
  23. :: ========================================================
  24. :: turn over log files
  25. :: ========================================================
  26.  
  27. :: change to the apache log file directory
  28. cd /D "D:Program FilesApache Software FoundationApache2.2logs"
  29.  
  30. :: stop Apache Service, Move log files and restart Apache Service
  31. "D:Program FilesApache Software FoundationApache2.2binhttpd.exe" -k stop
  32.  
  33. echo %nftu% >> access.log
  34. move "D:Program FilesApache Software FoundationApache2.2logsaccess.log" "D:Program FilesApache Software FoundationApache2.2logs%nftu%_access.log"
  35.  
  36. echo %nftu% >> error.log
  37. move "D:Program FilesApache Software FoundationApache2.2logserror.log" "D:Program FilesApache Software FoundationApache2.2logs%nftu%_error.log"
  38.  
  39. "D:Program FilesApache Software FoundationApache2.2binhttpd.exe" -k start
  40.  
  41. :: ========================================================
  42. :: zip todays Access and Error log files, then delete old logs
  43. :: ========================================================
  44.  
  45. :: zip the files
  46. "D:Program Files7-Zip7z.exe" a -tzip %nftu%_logs.zip %nftu%_access.log %nftu%_error.log
  47.  
  48. :: del the files
  49. del /Q %nftu%_*.log
  50.  
  51. :: ========================================================
  52. :: rotate the zip files
  53. :: ========================================================
  54.  
  55. :: make list of archive zip files
  56. type NUL > arclist.dat
  57. for /F "tokens=1,2 delims=[] " %%i in ('dir /B *_logs.zip ^| find /N "_logs.zip"') do echo %%i = %%j>> arclist.dat
  58.  
  59. :: count total number of files
  60. for /F "tokens=1 delims=" %%i in ('type arclist.dat ^| find /C "_logs.zip"') do set tnof=%%i
  61.  
  62. :: setup for and create the deletion list
  63. set /a negtk=%noatk%*-1
  64. set /a tntd=%tnof% - %noatk%
  65.  
  66. type NUL>dellist.dat
  67. for /L %%i in (%negtk%,1,%tntd%) do find " %%i = " arclist.dat >> dellist.dat
  68.  
  69. :: del the old files
  70. for /F "tokens=3 delims= " %%i in ('find "_logs.zip" dellist.dat') do del /Q %%i
  71.  
  72. :: remove temp files
  73. del /Q arclist.dat
  74. del /Q dellist.dat
  75.  
  76. option explicit
  77.  
  78. const DEBUG_MODE = false
  79. const MAX_ROTATIONS = 10
  80. const MAX_SIZE = 2097152 ' 2MB
  81. const WEB_LOGS = "c:pathtositelogs"
  82. const PHP_LOG = "c:pathtophplog"
  83. const APACHE_LOGS = "C:pathtoApache2logs"
  84. const APACHE_SERVICE ="Apache2.2" ' Name of the apache service for restart
  85. const ZIP_APP = "c:pathtozip.exe"
  86. const LOGROTATE_LOG = "c:tmplogrotate.log"
  87.  
  88. dim aLogs
  89. aLogs = Array("error.log","transfer.log","sec.log","phperror.log")
  90.  
  91. dim oFSO
  92. set oFSO = CreateObject("Scripting.FileSystemObject")
  93.  
  94. if (not DEBUG_MODE) then
  95. dim oLogFile
  96. set oLogFile = oFSO.CreateTextFile(LOGROTATE_LOG, 2, True)
  97. end if
  98.  
  99. dim bHasRotated
  100. bHasRotated = false
  101.  
  102. Print "Starting log rotation"
  103. Print "====================="
  104. ManageWebLogs()
  105. ManageApacheLogs()
  106. ManagePhpLog()
  107.  
  108. if (bHasRotated = true) then
  109. Print "====================="
  110. RestartService APACHE_SERVICE
  111. end if
  112.  
  113. Print "====================="
  114. Print "Log rotation finished"
  115.  
  116. if (not DEBUG_MODE) then
  117. oLogFile.Close
  118. set oLogFile = nothing
  119. end if
  120. set oFSO = nothing
  121.  
  122. '*****************************************************************************
  123. ' Loop through all the subfolders in the weblog directory
  124. sub ManageWebLogs()
  125. dim oLogDirs
  126. set oLogDirs = oFSO.GetFolder(WEB_LOGS)
  127. dim oLogDir
  128. for each oLogDir in oLogDirs.SubFolders
  129. Print "In " & oLogDir.Name
  130. RotateLogs(oLogDir)
  131. next
  132. set oLogDir = nothing
  133. end sub
  134.  
  135. '*****************************************************************************
  136. ' Loop through the log files in the Apache logs directory
  137. sub ManageApacheLogs()
  138. dim oLogDir
  139. set oLogDir = oFSO.GetFolder(APACHE_LOGS)
  140. Print "In " & oLogDir.Name
  141. RotateLogs(oLogDir)
  142. set oLogDir = nothing
  143. end sub
  144.  
  145. '*****************************************************************************
  146. ' Loop through the log files in the Apache logs directory
  147. sub ManagePhpLog()
  148. dim oLogDir
  149. set oLogDir = oFSO.GetFolder(PHP_LOG)
  150. Print "In " & oLogDir.Name
  151. RotateLogs(oLogDir)
  152. set oLogDir = nothing
  153. end sub
  154.  
  155.  
  156. '*****************************************************************************
  157. ' Traverse through each of the log file types and check if they need rotation
  158. sub RotateLogs(ByVal oFolder)
  159. dim sLog
  160. dim oLog
  161. for each sLog in aLogs
  162. if oFSO.FileExists(oFolder.Path & sLog) then
  163. set oLog = oFSO.GetFile(oFolder.Path & sLog)
  164. if (oLog.Size > MAX_SIZE) then
  165. RotateLog oFolder.Path & sLog
  166. ArchiveLog oFolder.Path & sLog
  167. bHasRotated = true
  168. end if
  169. end if
  170. next
  171. set oLog = nothing
  172. end sub
  173.  
  174.  
  175. '*****************************************************************************
  176. ' Rotates the given log, by incrementing the file name
  177. sub RotateLog(ByVal sLog)
  178. dim i
  179. dim sOldFile, sNewFile
  180. for i = MAX_ROTATIONS to 1 step -1
  181. sOldFile = sLog & "." & i & ".zip"
  182. sNewFile = sLog & "." & (i+1) & ".zip"
  183. if oFSO.FileExists(sOldFile) and i = MAX_ROTATIONS then
  184. ' Delete zipfile
  185. Print "-- Deleting " & sOldFile
  186. oFSO.DeleteFile(sOldFile)
  187. elseif oFSO.FileExists(sOldFile) then
  188. ' Rename zipfile
  189. Print "-- Renaming " & sOldFile & " to " & sNewFile
  190. oFSO.MoveFile sOldFile, sNewFile
  191. end if
  192. next
  193. end sub
  194.  
  195.  
  196. '*****************************************************************************
  197. ' Zips the current log
  198. sub ArchiveLog(ByVal sLog)
  199. Dim oShell
  200. Set oShell = CreateObject("WScript.Shell")
  201. dim sZipFile
  202. sZipFile = sLog & ".1.zip"
  203. Print "-- Archiving " & sLog & " to " & sZipFile
  204. oShell.Run "cmd /c " & ZIP_APP & " -jq " & sZipFile & " " & sLog, 0, true
  205. oFSO.DeleteFile(sLog)
  206. set oShell = nothing
  207. end sub
  208.  
  209.  
  210. ' ****************************************************************************
  211. ' Restarts a given service (in our case Apache)
  212. private sub RestartService( _
  213. ByVal sService _
  214. )
  215.  
  216. Dim oShell
  217. Set oShell = CreateObject("WScript.Shell")
  218.  
  219. ' Service stopped with 'Net' command
  220. oShell.Run "cmd /c net stop " & sService, 0, true
  221. Print sService & " service stopped"
  222.  
  223. ' Service started with 'Net' command
  224. oShell.Run "cmd /c net start " & sService, 0, true
  225. Print sService & " service restarted"
  226.  
  227. set oShell = nothing
  228. end sub
  229.  
  230.  
  231. '*****************************************************************************
  232. ' Echoes out the given message if in debug mode
  233. sub Print(ByVal sMsg)
  234. if (DEBUG_MODE) then
  235. wscript.echo sMsg
  236. else
  237. oLogFile.WriteLine sMsg
  238. end if
  239. end sub
Add Comment
Please, Sign In to add comment