一个星期后如何删除日志文件? [英] How to delete log file after a week?

查看:78
本文介绍了一个星期后如何删除日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已计划每4小时运行一次批处理作业,以从数据库中获取最新记录.

我还要维护在批处理作业运行时将更新的日志文件.为了补偿存储空间,我需要每周删除日志文件.

我曾尝试使用批处理文件来删除任务,以删除N天之前的文件.

就我而言,文件日志文件将每4小时更新一次.

解决方案

不建议删除定期添加行的整个日志文件,因为这会导致在特定时间丢失最近N天的所有信息.例如,每个星期天删除日志文件,并在星期一早晨检测到上次备份发生了不好的事情,而您想找出发生了什么,您确实遇到了问题,因为该信息不再可用.

通常,保留日志和备份文件的时间跨度并不重要,因为存储介质的大小限制了日志文件可以有多大,或者在存储介质已满之前可以存储多少个文件大小为X的备份./p>

因此,在我看来,将日志和备份文件的使用重点放在特定时间段上是一种更好的策略.与仅具有几个GiB的大型备份相比,仅存储一些MiB的备份文件通常会更多.因此,对于小型备份,备份数量(=备份时间段)可能大于小型备份的数量.对于日志文件而言,在日志文件大小变得关键之前,每次执行中要添加多少数据,这要比在向日志文件中添加数据的时间段内备份执行的次数大.

因此,着重于日志文件的大小而不是时间段,我建议:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Define file size limit for the log file with 4 MiB.
set "SizeLimit=4194304"
set "LogFile=C:\BackupFolder\Backup.log"

for %%I in ("%LogFile%") do if %%~zI GEQ %SizeLimit% move /Y "%%I" "%%~dpnI_old%%~xI"

endlocal

每次执行此小批处理文件时,都会将指定日志文件的文件大小与该批处理文件中定义的文件大小限制进行比较.

如果日志文件的文件大小大于或等于定义的文件大小限制,则在文件扩展名覆盖可能已经存在的 _old 前,通过插入_old将当前日志文件移动到同一目录中.具有相同名称和文件扩展名的文件.

换句话说,只要日志文件具有4个或更多MiB,此批处理文件移动 C:\BackupFolder\Backup.logC:\BackupFolder\Backup_old.log并自动覆盖很可能已经存在的C:\BackupFolder\Backup_old.log.

结果是至少总是有1个日志文件(Backup.logBackup_old.log),其中包含最近N次备份操作的已记录消息.

在最坏的情况下,两个日志文件在存储介质上所需的总大小约为8 MiB,加上上次备份操作产生的KiB.

可以定义文件大小限制,具体取决于每个备份操作默认情况下附加多少数据.例如,如果每4小时执行一次典型的备份操作,则会将96 KiB的数据附加到日志文件中,则文件大小限制的公式为:

96 KiB x 6 backups per day x 7 days = 4032 KiB

4032 KiB比4096 KiB(= 4 MiB)小一点.因此,在下一次备份中,达到了4 MiB文件的大小限制,并且当前的日志文件被替换为较旧的日志文件.换句话说,在典型的备份中,每个日志文件都包含大约一个星期的日志消息,然后再创建一个新的日志文件.

这个简单的批处理文件也可以定义为从其他批处理文件中调用,以限制文件大小的各种日志文件的使用,从而可以将一个批处理文件用于服务器上的许多不同的日志文件.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if "%~1" == "" goto EndFileSizeCheck
set "LogFile=%~1"

rem Define default file size limit for the log file with 4 MiB.
set "SizeLimit=4194304"
if not "%~2" == "" set "SizeLimit=%~2"

for %%I in ("%LogFile%") do if %%~zI GEQ %SizeLimit% move /Y "%%I" "%%~dpnI_old%%~xI"

:EndFileSizeCheck
endlocal

上面的批处理文件要求第一个参数是日志文件的名称.作为第二个参数,可以选择指定文件大小限制.如果没有文件大小限制传递给批处理文件,则默认使用4 MiB.

重要提示::文件大小比较仅适用于最多2 GiB-1 = 2 ^ 31-1字节= 2147483647字节.因此,请使用一个文件大小限制,以确保或多或少保证日志文件的当前大小永远不会超过2 GiB.

以上批处理代码还可用于目录中的所有* .log文件.但是在这种情况下,将日志文件移动到另一个目录,或者在移动时更改文件扩展名,或者添加额外的代码以确保旧的日志文件不会一次又一次地移动是很重要的.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
if "%~1" == "" goto EndFileSizeCheck
set "LogFile=%~1"

rem Define default file size limit for the log file with 4 MiB.
set "SizeLimit=4194304"
if not "%~2" == "" set "SizeLimit=%~2"

for %%I in ("%LogFile%") do (
    set "FileName=%%~nI"
    if /I not "!FileName:~-4!" == "_old" if %%~zI GEQ %SizeLimit% move /Y "%%I" "%%~dpnI_old%%~xI"
)

:EndFileSizeCheck
endlocal

例如,可以从另一个批处理文件调用上述批处理文件变体

call FileSizeCheck.bat "C:\BackupFolder\*.log" 16777216

检查文件夹C:\BackupFolder中每个* .log文件的文件大小限制为16 MiB,而忽略该目录中已经存在的* _old.log文件.

注意:由于在按原样使用此批处理代码时使用延迟的环境变量扩展,因此日志文件名或其路径不能包含感叹号.

注意:

>

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

  • call /? ...解释%~1%~2
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • move /?
  • rem /?
  • set /?
  • setlocal /?

在前两个批处理文件中将不需要命令setlocal EnableExtensions DisableDelayedExpansionendlocal,因为在Windows上默认情况下启用了命令扩展名,并且默认情况下禁用了延迟扩展.但是,这样做可以确保调用批处理文件的环境变量不会被批处理文件检查文件大小并在达到文件大小限制时移动文件时所修改.

I have scheduled a batch job to run every 4 hour to fetch latest records from database.

Also I'm maintaining log file which will be updated whenever batch job runs. In order to compensate storage memory I need to delete log file every week.

I had tried to solve the task with Batch file to delete files older than N days.

In my case file log files will be updated every 4 hours.

解决方案

It is not advisable to delete the entire log file on which lines are appended regularly as this results in losing all information for the last N days at a specific time. For example deleting the log file every Sunday and detecting on Monday morning that something bad happened on last backup and you want to find out what happened, you have really a problem as the information is not available anymore.

In general the time span for keeping log and backup files does not really matter as the storage media size limits how large a log file can be or how many backups with file size X can be stored before the storage media is full.

So in my point of view it is a much better strategy to focus on storage size usage by log and backup files then on a specific time period. There can be most often more backup files with only some MiB stored than large backups with several GiB. So for small backups the number of backups (= backup time period) can be larger than for small backups. And for log files it matters how much data are appended on each execution before the log file size becomes critical than the the number of backup executions within a time period appending data to the log file.

So with focus on log file size instead of time period I suggest following:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Define file size limit for the log file with 4 MiB.
set "SizeLimit=4194304"
set "LogFile=C:\BackupFolder\Backup.log"

for %%I in ("%LogFile%") do if %%~zI GEQ %SizeLimit% move /Y "%%I" "%%~dpnI_old%%~xI"

endlocal

On every execution of this small batch file the file size of the specified log file is compared with the file size limit defined in the batch file.

If file size of the log file is greater or equal the defined file size limit, the current log file is moved into same directory with inserting _old before file extension overwriting a perhaps already existing _old file with same name and file extension.

In other words this batch file moves C:\BackupFolder\Backup.log to C:\BackupFolder\Backup_old.log with overwriting automatically a most likely already existing C:\BackupFolder\Backup_old.log whenever the log file has 4 or more MiB.

The result is that there is at least always 1 log file, either Backup.log or Backup_old.log with the logged messages of the last N backup operations.

And the total size needed on storage media for both log files is in worst case about 8 MiB plus some KiB from last backup operation.

The file size limit can be defined depending on how much data are appended by default on each backup operation. For example if a typical backup operation executed every 4 hours appends 96 KiB on data to the log file, the formula for the file size limit is:

96 KiB x 6 backups per day x 7 days = 4032 KiB

4032 KiB is a bit smaller than 4096 KiB (= 4 MiB). So with next backup the 4 MiB file size limit is reached and the current log file is moved replacing the older log file. In other words on typical backups each log file contains the log messages of about a week before a new one is created.

This simple batch file can be also defined for being called from other batch files for various log files with various file size limits which makes it possible to use 1 batch file for many different log files on a server.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if "%~1" == "" goto EndFileSizeCheck
set "LogFile=%~1"

rem Define default file size limit for the log file with 4 MiB.
set "SizeLimit=4194304"
if not "%~2" == "" set "SizeLimit=%~2"

for %%I in ("%LogFile%") do if %%~zI GEQ %SizeLimit% move /Y "%%I" "%%~dpnI_old%%~xI"

:EndFileSizeCheck
endlocal

The batch file above requires as first parameter the name of a log file. As second parameter the file size limit can be specified optionally. 4 MiB is used as default if there is no file size limit passed to the batch file.

IMPORTANT: The file size comparison works only for up to 2 GiB - 1 = 2^31 - 1 bytes = 2147483647 bytes. Therefore use a file size limit on which current size of the log file is more or less guaranteed never 2 GiB or more.

The above batch code could be also used for all *.log files in a directory. But in this case it is important to move the log file to another directory, or change the file extension on move, or add extra code to make sure the old log file is not moved again and again and again.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
if "%~1" == "" goto EndFileSizeCheck
set "LogFile=%~1"

rem Define default file size limit for the log file with 4 MiB.
set "SizeLimit=4194304"
if not "%~2" == "" set "SizeLimit=%~2"

for %%I in ("%LogFile%") do (
    set "FileName=%%~nI"
    if /I not "!FileName:~-4!" == "_old" if %%~zI GEQ %SizeLimit% move /Y "%%I" "%%~dpnI_old%%~xI"
)

:EndFileSizeCheck
endlocal

The batch file variant above can be called from another batch file for example with

call FileSizeCheck.bat "C:\BackupFolder\*.log" 16777216

to check each *.log file in folder C:\BackupFolder on file size limit of 16 MiB with ignoring already existing *_old.log files in that directory.

Note: The log file name or it's path cannot contain an exclamation mark because of using delayed environment variable expansion on using this batch code as is or otherwise the code does not work as designed.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains %~1 and %~2
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • move /?
  • rem /?
  • set /?
  • setlocal /?

The commands setlocal EnableExtensions DisableDelayedExpansion and endlocal would not be needed in first two batch files as the command extensions are enabled by default and delayed expansion is disable by default on Windows. But the usage makes sure that environment variables of calling batch files are never modified by the batch file checking file size and moving the file when file size limit reached.

这篇关于一个星期后如何删除日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆