Saturday, 21 January 2017

Creating date wise log file in Robocopy backup.

Or, How to take backup of shared or local folders using Robocopy?
Or, Robocopy command to take backup and exporting backup log files date wise.

Descriptions: Using Robocopy for shared folders or local folders backup is one of the good way to perform flat files/data backup in Windows. It’s an inbuilt utility in windows by Microsoft, no need to download or purchase it separately.

Normally, administrators do use robocopy with source, destination and export logs arguments in the robocopy command and rename the log files manually everyday... that’s waste of time frankly... J

Here in this article, we will see how we can set the robocopy command to take backup of data, export log in a txt file and keep the log file name with execution date included. We are going to get it done with batch script..

e.g. If the log file name is fileserverbackup.txt, it should be exported with name fileserverbackup-YYYYMMDD.txt

Please note, I have used some special switched to for best backup output. you should understand the behaviour of special switches before using them.

You can use Robocopy /? for more help on robocopy command and switches.

Commands and batch file:

General command with no special switches

robocopy C:\TestSourceFolder E:\DestFolder

Customized command as per suitable requirements with some special switches

robocopy C:\TestSourceFolder E:\DestFolder /E /ZB /SEC /R:3 /W:3 /TEE /UNILOG+:C:\FileServerBackup.txt

the above mentioned command will take backup well, but it will create a log file FileServerBackup.txt with no date inserted in the file name.

Recommended:

Batch script to take backup using robocopy command and then creating log files every day with date specified/added in the file name.

--------------------------------------------------------------------------------------------------------------------------
@echo off
echo wscript.echo year(date) ^& right(100 + month(date),2) ^& right(100+day(date),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yyyymmdd=%%a
echo Today is %yyyymmdd%
robocopy C:\TestSourceFolder E:\DestFolder /E /ZB /SEC /R:3 /W:3 /TEE /UNILOG+:C:\FileServerCopy-%yyyymmdd%.txt
--------------------------------------------------------------------------------------------------------------------------

Copy and paste the above given script in a text files > save as filename.bat > use this bat file in windows task scheduler.

That’s it, backup will be executed and file name will be created date wise automatically. Please make sure you tested the script carefully and deploy[ in production if it works 100% as per your requirement.

If you have any query or feedback, please write me back...

No comments:

Post a Comment