Archive Sitecore logs with 7zip and Task Scheduler
Posted 30 Dec 2018 by Marek Musielak
This article explains how to automatically zip your Sitecore log files with 7zip and Windows Task Scheduler.
First thing you want to do is downloading 7zip and installing it on your server. You can download it from www.7-zip.org site.
Next you add 7zip directory to Windows
Once it's all done, you need to create a directory where your zipped log files will be stored, e.g.
Now create a new
bat file in the directory above and name it e.g.
archiveLogs.bat. Edit the file and use code:
>FOR /f %%a IN ('WMIC OS GET LocalDateTime ^| find "."") DO SET 'CurrentTimeStamp=%%a" :: sets FormattedDate to current date in yyyy-MM-dd format SET "FormattedDate=%CurrentTimeStamp:~0,4%-%CurrentTimeStamp:~4,2%-%CurrentTimeStamp:~6,2%" :: replace path below with the folder containing log files SET "SourceDir=C:\inetpub\wwwroot\mysitecore.local\App_Data\logs" :: replace path below with the folder where zip files will live SET "ZipLocation=C:\mysitecore-logs-backup\logs-%FormattedDate%.zip" :: use the number below to set after how many days log files should be archived SET "LogLifetime=7" FORFILES /S /P %SourceDir% /M *.txt /D -%LogLifetime% /C "cmd /c 7z a -aoa -tzip %ZipLocation% @path -sdel"
You may notice there are some variables in the file. Set both
LogLifetime to the correct values. Save the
bat file and run it just to test if it works. You may want to set some higher value for
LogLifetime before testing just so there are some log files left for your further tests if needed.
The last parameter of
7z command (
-sdel) assures that after the file is successfully added to the archive, it's removed from the drive automatically.
When all works, start
Run and type
taskschd.msc to open Task Scheduler. Use the following settings:
- In General tab type the name of your task and select
Run whether user is logged in or notradio button.
- On Triggers tab create a schedule for your task, e.g.
At 2:00 AM every day.
- On Actions tab create a new
Start a programaction and select your
You may set more settings on other tabs but in most cases it should not be needed. Now save your task, refresh
Task Scheduler Library if your task haven't appeared in the list automatically, right click and run. This will start a test execution of your task. Again, you may want to set some higher value for
LogLifetime before testing. If everything went smoothly, that's all. Now your old log files will no longer clutter your drive and will be nicely archived.
Thank you for reading. You can find more Sitecore related articles here.