Automating a robocopy backup process on Windows

  • Updated

Workflow Guide

Prerequisites:

Time required:

  • 30 minutes

Introduction

This article will guide you through the steps required to automatically run a robocopy process using batch scripting and Windows Task Scheduler

The Command and script

If you've prepared a robocopy script using the Using Robocopy for Filespace backup article, you can include it in a batch script to run at regular intervals inline with your required Restore Point Objective. 

The following script is provided as an example:

@echo off

setlocal

:: set source, destination and logfile location (replace as required)
set source="C:\Volumes\domain\filespace"
set destination="\\nas\backupDirectory"
set logFileDestination="\\nas\backupDirectory\BackUpLogs"

:: Set the date and remove forward slashes from string
set sdate=%DATE:/=-%

echo Filespace Backup to NAS process started at %sdate%-%TIME%...

:: Set the time and remove colons from string
set stime=%TIME::=%
set stime=%STIME:.=%

:: Define the log file name with the date and save in back up location
set "LogFile=%logFileDestination%\BackupLog_%sdate%-%stime%.txt"

:: Run the robocopy command with the dynamic log file name
robocopy %source% %destination% /E /COPY:DAT /DCOPY:T /XO /R:1 /W:1 /V /Z /MT /LOG:"%LogFile%"

:: Set the end date and time and remove forward slashes and colons from string
set edate=%DATE:/=-%
set etime=%TIME::=%
set etime=%etime:.=%

:: Add end date and time to log filename
rename "%LogFile%" "BackupLog_%sdate%-%stime%_%edate%-%etime%.txt"

endlocal

What does this script do?

  • Sets <source> and <destination> and backup log location.
  • Sets the start date and time to include in log file name
  • Starts a robocopy process detailed in <insert link here?
  • Sets the end date and time to include in log file name
  • Renames log file name to include start and end datetime

Saving the script

  • Copy the script above and paste it into a text editor such as notepad++
  • Set the source, destination and log file paths

You might want to test first with one smaller capacity folder on the Filespace to ensure the script logic is configured correctly and change the paths to be the full Filespace when testing is successful. 💡Remember to connect to the Filespace with a read-only user.

  • Save it as "robocopy_with_date_log.bat"

If you choose to store the log file on the destination, you can exclude it from the robocopy command with the addition of the /XD (exclude directory) option below:

/robocopy %source% %destination% /E /COPY:DAT /DCOPY:T /XO /R:1 /W:1 /V /Z /MT /XD "%logFileDestination%" /LOG:"%LogFile%"

Running the script

You can run the script by double clicking it.  You will see a batch file window with the output from the first echo command visible:

This window will stay visible until the robocopy command has completed, at which point you can check the log files and compare the source and destination folder properties. 

This first process will probably take a long time.  Approximate times can be checked using a bandwidth calculator and details of the total data size and bandwidth.

Automating with Windows Task Scheduler

Once the initial copy has completed and you're happy the process has worked, you can create a reoccurring task using Windows task Scheduler.

Search for Task Scheduler in the start menu and open the application:

Select Task Scheduler Library and click Create Task in the actions pane:

Enter an appropriate Name and Description in the Create Task window

Select the Triggers tab and click New to enter in your desired Trigger schedule.

The screenshot below sets the task to run every day, starting on the following day at 4am. 

Press OK when you are happy with the Trigger

Select Actions tab and click New to set the task action:

Choose Start a program and then click Browse...

 

Select the batch script you saved earlier and click Open

 

Press OK

Press OK

You will now see the task in the Task Scheduler Library.

The Status should be Ready and the task will be set to run at the time you specified.

 

Was this article helpful?

0 out of 0 found this helpful