压缩 IIS 日志的自动脚本?
我想编写一个脚本/批处理,将我的日常 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
从 http://blogs.msdn.com/daiken/archive/2007/02/12/compress-files-with-windows-powershell-then-package-a-windows- vista-sidebar-gadget.aspx
这是 powershell 的答案,效果惊人:
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:
您需要一个命令行工具来压缩文件。 我推荐 7-Zip,它是免费且易于使用的。 独立的命令行版本 (7za.exe) 是最便携的选择。
下面是一个两行批处理文件,它将压缩日志文件并随后删除它们:
第一个参数是 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:
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.
这是我的脚本,它基本上改编自 David 的脚本,并压缩上个月的日志,移动它们并删除原始日志文件。 这也可以适用于 Apache 日志。
唯一的问题是,如果您的 DOS 日期函数输出星期的日期,您可能需要编辑替换命令。
您还需要安装 7-zip。
您还可以下载 IISlogslite,但它会将每天的文件压缩为单个 zip 文件,我认为该文件没有用处。 网络上有一个 vbscript 可以做同样的事情。
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.
我们使用如下所示的脚本。 Gzip 来自 cygwin 项目。 我确信您可以修改语法以使用 zip 工具。 “skip”参数是不归档的文件数量——我们在“当前”目录中保留 11 天。
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.
您可以从 DotNetZip 获取命令行实用程序包,以获取从脚本创建 zip 的工具。 有一个名为 Zipit.exe 的不错的小工具,它在命令行上运行,将文件或目录添加到 zip 文件中。 它快速、高效。
更好的选择可能是仅从 PowerShell 中进行压缩。
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.
我们使用这个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
正则表达式可以解决这个问题...创建一个 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.