问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/38996848/how-to-delete-log-file-after-a-week