Most of the time system administrators are dealing with old log files like backing up the old logs manually and removing the original logs. To reduce their headache, here we have a small script, which provides flexibility to manage those old logs in very handy manner. Administrators just need to set the variables in the script as per there requirement and let the script do the magic.
About the script: This script takes the backup of all the log files older then specified number of days and after backup, it automatically deletes the original files and spare the disk space.
A job can be schedules either on daily or weekly basis to execute this script and manage old log files on the server.
Requirements:
a. Save the script with .bat extension like "7zscript.bat".
b. Installation of 7-zip software.
c. Set environment variable for 7-zip ‘C:\Program Files\7-Zip;’
d. Need to make changes in some script variables. Here is as the list of those variables.
NUMDAYS : Number of days, want to keep the logs.
(Example: If this variable is set to 5 then it will leave the logs of 5 days and will compress & delete the logs older than 5 days.)
SEARCHPATTERN : Need to specify what all kind of files you want to include in compress.
i. *.log* - It will perform operation only on log files.
ii. *.* - It will perform operation on all files.
SOURCE_LOCATION : Source Location of log files.
(Example: “D:\sourceLocation")
TARGET_ZIP_NAME : Name of archived file.
(Example: zip_%TODAY%.zip)
TARGET_LOCATION : Target Location for archived log files.
(Example: “D:\targetLocation")
After making the changes in mentioned variables, now this script is ready to manage your old logs on the server.
Executing script
To execute this script, follow the below mentioned steps:
a. Open command prompt.
b. Navigate to the directory where you have kept the script.
c. Write script name in command prompt and hit enter key.
Administrators sit back and let the script manage server logs :)
About the script: This script takes the backup of all the log files older then specified number of days and after backup, it automatically deletes the original files and spare the disk space.
A job can be schedules either on daily or weekly basis to execute this script and manage old log files on the server.
@ECHO OFF
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%
Set TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%
:: Setting up Logs SOURCE path and archived Logs path
Set NUMDAYS=5
Set SEARCHPATTERN=*.log*
Set SOURCE_LOCATION="D:\logs"
Set TARGET_ZIP_NAME=zip_%TODAY%.zip
Set TARGET_LOCATION="D:\archivedLogs\%TARGET_ZIP_NAME%"
:: Compressing logs files except specified days.
forfiles -p %SOURCE_LOCATION% -s -m %SEARCHPATTERN% -d -%NUMDAYS% -c "cmd /c 7z a -tzip %TARGET_LOCATION% @path -mx5"
:: Removing Original log files after compression.
forfiles -p %SOURCE_LOCATION% -s -m %SEARCHPATTERN% -d -%NUMDAYS% -c "cmd /c del /Q @path"
Here is the script. Just copy and paste this snippet in notepad file and save that file with .bat extension.Requirements:
a. Save the script with .bat extension like "7zscript.bat".
b. Installation of 7-zip software.
c. Set environment variable for 7-zip ‘C:\Program Files\7-Zip;’
d. Need to make changes in some script variables. Here is as the list of those variables.
NUMDAYS : Number of days, want to keep the logs.
(Example: If this variable is set to 5 then it will leave the logs of 5 days and will compress & delete the logs older than 5 days.)
SEARCHPATTERN : Need to specify what all kind of files you want to include in compress.
i. *.log* - It will perform operation only on log files.
ii. *.* - It will perform operation on all files.
SOURCE_LOCATION : Source Location of log files.
(Example: “D:\sourceLocation")
TARGET_ZIP_NAME : Name of archived file.
(Example: zip_%TODAY%.zip)
TARGET_LOCATION : Target Location for archived log files.
(Example: “D:\targetLocation")
To execute this script, follow the below mentioned steps:
a. Open command prompt.
b. Navigate to the directory where you have kept the script.
c. Write script name in command prompt and hit enter key.
Administrators sit back and let the script manage server logs :)
No comments:
Post a Comment