压缩IIS日志的自动脚本?

时间:2008-08-27 04:08:40

标签: iis batch-file zip scripting logging

我想编写一个脚本/批处理,将每天的IIS日志收起来并按月压缩。

ex080801.log,格式为ex yymmdd .log

ex080801.log - ex080831.log被压缩并删除了日志文件。

我们这样做的原因是因为在一个繁重的站点上,一天的日志文件可能是500mb到1gb所以我们将它们压缩起来,将它们压缩98%并转储真实的日志文件。我们使用webtrend来分析日志文件,它能够读入一个zip文件。

有没有人对如何编写脚本有任何想法,或者愿意分享一些代码?

7 个答案:

答案 0 :(得分:12)

您需要一个命令行工具来压缩文件。我推荐免费且易于使用的7-Zip。自包含命令行版本(7za.exe)是最便携的选择。

这是一个两行批处理文件,它会压缩日志文件并在之后删除它们:

7za.exe a -tzip ex%1-logs.zip %2\ex%1*.log
del %2\ex%1*.log

第一个参数是4位数的年份和月份,第二个参数是包含日志的目录的路径。例如:ziplogs.bat 0808 c:\logs

可以更精细(即搜索文件名以确定要归档的月份)。您可能需要查看Windows FINDSTR命令,以便使用正则表达式搜索输入文本。

答案 1 :(得分:6)

这是我的脚本,它基本上适应了大卫的,并在上个月的日志中拉上,移动它们并删除原始日志文件。这也适用于Apache日志。 唯一的问题是,如果你的DOS日期函数输出一周的日期,你可能需要编辑替换命令。 您还需要安装7-zip。

你也可以下载IISlogslite,但它会将每天的文件压缩成一个我认为没用的zip文件。有一个关于网络的vbscript在做同样的事情。

-------------------------------------------------------------------------------------
@echo on

:: Name - iislogzip.bat
:: Description - Server Log File Manager
::
:: History
:: Date         Authory      Change
:: 27-Aug-2008  David Crow   Original (found on stack overflow)
:: 15-Oct-2008  AIMackenzie  Slimmed down commands


:: ========================================================
:: setup variables and parameters
:: ========================================================
:: generate date and time variables

set month=%DATE:~3,2%
set year=%DATE:~8,2%

::Get last month and check edge conditions

set /a lastmonth=%month%-1
if %lastmonth% equ 0 set /a year=%year%-1
if %lastmonth% equ 0 set lastmonth=12
if %lastmonth% lss 10 set lastmonth=0%lastmonth%

set yymm=%year%%lastmonth%

set logpath="C:\WINDOWS\system32\LogFiles"
set zippath="C:\Program Files\7-Zip\7z.exe"
set arcpath="C:\WINDOWS\system32\LogFiles\WUDF"


:: ========================================================
:: Change to log file path
:: ========================================================
cd /D %logpath%

:: ========================================================
:: zip last months IIS log files, move zipped file to archive 
:: then delete old logs
:: ========================================================
%zippath% a -tzip ex%yymm%-logs.zip %logpath%\ex%yymm%*.log
move "%logpath%\*.zip" "%arcpath%"
del %logpath%\ex%yymm%*.log

答案 2 :(得分:2)

我们使用如下脚本。 Gzip来自cygwin项目。我相信您可以修改语法以使用zip工具。 “skip”参数是未归档的文件数 - 我们在“当前”目录中保留11天。

@echo off
setlocal
For /f "skip=11 delims=/" %%a in ('Dir D:\logs\W3SVC1\*.log /B /O:-N /T:C')do move "D:\logs\W3SVC1\%%a" "D:\logs\W3SVC1\old\%%a"
d:
cd "\logs\W3SVC1\old"
gzip -n *.log
Endlocal
exit

答案 3 :(得分:1)

您可以从DotNetZip获取命令行实用程序包,以获取从脚本创建zip的工具。有一个很好的小工具叫做Zipit.exe,可以在命令行上运行,为zip文件添加文件或目录。它快速,高效。

更好的选择可能是在PowerShell中进行压缩。

function ZipUp-Files ( $directory )
{

  $children = get-childitem -path $directory
  foreach ($o in $children) 
  {
    if ($o.Name -ne "TestResults" -and 
        $o.Name -ne "obj" -and 
        $o.Name -ne "bin" -and 
        $o.Name -ne "tfs" -and 
        $o.Name -ne "notused" -and 
        $o.Name -ne "Release")
    {
      if ($o.PSIsContainer)
      {
        ZipUp-Files ( $o.FullName )
      }
      else 
      {
        if ($o.Name -ne ".tfs-ignore" -and
           !$o.Name.EndsWith(".cache") -and
           !$o.Name.EndsWith(".zip") )
        {
          Write-output $o.FullName
          $e= $zipfile.AddFile($o.FullName)
        }
      }
    }
  }
}


[System.Reflection.Assembly]::LoadFrom("c:\\\bin\\Ionic.Zip.dll");

$zipfile =  new-object Ionic.Zip.ZipFile("zipsrc.zip");

ZipUp-Files "DotNetZip"

$zipfile.Save()

答案 4 :(得分:1)

来自http://blogs.msdn.com/daiken/archive/2007/02/12/compress-files-with-windows-powershell-then-package-a-windows-vista-sidebar-gadget.aspx

的借用拉链功能

这是powershell的答案,可以创造奇迹:

param([string]$Path = $(read-host "Enter the path"))
function New-Zip
{
    param([string]$zipfilename)
    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    (dir $zipfilename).IsReadOnly = $false
}
function Add-Zip
{
    param([string]$zipfilename)

    if(-not (test-path($zipfilename)))
    {
        set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
        (dir $zipfilename).IsReadOnly = $false  
    }

    $shellApplication = new-object -com shell.application
    $zipPackage = $shellApplication.NameSpace($zipfilename)

    foreach($file in $input) 
    { 
            $zipPackage.CopyHere($file.FullName)
            Start-sleep -milliseconds 500
    }
}
$FilesToZip = dir $Path -recurse -include *.log
foreach ($file in $FilesToZip) {
New-Zip $file.BaseName
dir $($file.directoryname+"\"+$file.name) | Add-zip $($file.directoryname+"\$($file.basename).zip")
del $($file.directoryname+"\"+$file.name)
}

答案 5 :(得分:1)

我们使用此PowerShell脚本:http://gallery.technet.microsoft.com/scriptcenter/31db73b4-746c-4d33-a0aa-7a79006317e6

它使用7-zip并在删除文件之前验证文件

答案 6 :(得分:0)

正则表达式会做的诀窍......创建一个perl / python / php脚本来为你完成这项任务。
我很确定Windows批处理文件不能用正则表达式。