压缩 IIS 日志的自动脚本?

发布于 2024-07-04 14:22:01 字数 302 浏览 16 评论 0原文

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

ex080801.log 的格式为 exyymmdd.log

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

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

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

I'd like to write a script/batch that will bunch up my daily IIS logs and zip them up by month.

ex080801.log which is in the format of exyymmdd.log

ex080801.log - ex080831.log gets zipped up and the log files deleted.

The reason we do this is because on a heavy site a log file for one day could be 500mb to 1gb so we zip them up which compresses them by 98% and dump the real log file. We use webtrend to analyze the log files and it is capable of reading into a zip file.

Does anyone have any ideas on how to script this or would be willing to share some code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

娇妻 2024-07-11 14:22:01

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)
}

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

Here is powershell answer that works wonders:

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)
}
潜移默化 2024-07-11 14:22:01

您需要一个命令行工具来压缩文件。 我推荐 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 命令,用于搜索输入文本常用表达。

You'll need a command line tool to zip up the files. I recommend 7-Zip which is free and easy to use. The self-contained command line version (7za.exe) is the most portable choice.

Here's a two-line batch file that would zip the log files and delete them afterwards:

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

The first parameter is the 4 digit year-and-month, and the second parameter is the path to the directory containing your logs. For example: ziplogs.bat 0808 c:\logs

It's possible to get more elaborate (i.e. searching the filenames to determine which months to archive). You might want to check out the Windows FINDSTR command for searching input text with regular expressions.

烟凡古楼 2024-07-11 14:22:01

这是我的脚本,它基本上改编自 David 的脚本,并压缩上个月的日志,移动它们并删除原始日志文件。 这也可以适用于 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

Here's my script which basically adapts David's, and zips up last month's logs, moves them and deletes the original log files. this can be adapted for Apache logs too.
The only problem with this is you may need to edit the replace commands, if your DOS date function outputs date of the week.
You'll also need to install 7-zip.

You can also download IISlogslite but it compresses each day's file into a single zip file which I didn't find useful. There is a vbscript floating about the web that does the same thing.

-------------------------------------------------------------------------------------
@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
天生の放荡 2024-07-11 14:22:01

我们使用如下所示的脚本。 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

We use a script like the following. Gzip is from the cygwin project. I'm sure you could modify the syntax to use a zip tool instead. The "skip" argument is the number of files to not archive off -- we keep 11 days in the 'current' directory.

@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
清君侧 2024-07-11 14:22:01

您可以从 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()

You can grab the command-line utilities package from DotNetZip to get tools to create zips from scripts. There's a nice little tool called Zipit.exe that runs on the command line, adds files or directories to zip files. It is fast, efficient.

A better option might be to just do the zipping from within 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()
凉墨 2024-07-11 14:22:01

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

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

We use this powershell script: http://gallery.technet.microsoft.com/scriptcenter/31db73b4-746c-4d33-a0aa-7a79006317e6

It uses 7-zip and verifys the files before deleting them

萌面超妹 2024-07-11 14:22:01

正则表达式可以解决这个问题...创建一个 perl/python/php 脚本来为您完成这项工作..
我很确定 Windows 批处理文件不能执行正则表达式。

Regex will do the trick... create a perl/python/php script to do the job for you..
I'm pretty sure windows batch file can't do regex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文