Daily Backup Using Robocopy

Learning that lasts. Online courses from $14.99
I wanted to use Robocopy, to backup a directory on a Windows Server 2022 system to an external USB drive attached to the server every weekday. The server is at a company where employees don't usually work on files at the office on weekends and I create a weekly backup archive on weekends using a different backup method. I opted to use Robocopy since it is a free command-line utility for replicating files and directories that is provided by Microsoft with current Windows operating systems. The directory I want to backup is D:\ACCI and I want to create the backup on the external E:\ drive in the existing directory E:\ACI\Backups\ACCI. I want that backup directory to always mirror the source directory, so any files added during the day should be added to the backup directory when the Robocopy command runs and any files that are deleted from the source directory during the day should be deleted from the backup directory when the Robocopy command runs. I.e., the backup directory should mirror the source directory, so I use Robocopy's /MIR option. The source directory is a directory shared over the local area network (LAN) and it is possible that an employee may leave for the day with one or more files in the shared folder on the server still open. I don't want Robocopy to wait and try again to backup an open file since the script will be running at night at a time employees aren't likely to be still working and so any file that is locked because it is still open will likely remain open until at least the the next day when employees resume work. So I use the /W:0 option to tell Robocopy to immediately retry to backup a file if it encounters a problem (by default it will wait 30 seconds and try again) and the /R:1 option to tell Robocopy to only retry backing up a file once rather than try repeatedly (it will try 1 million times by default). I want to record all output of the Robocopy daily backups in E:\ACI\Backups\RoboCopyBackup.log so I place that output path and filename after >> which will append the daily output to that file—if you just use one greater than sign rather than two, the file will be overwritten each time the Robocopy command runs. So I have the following Robocopy command, which I placed in a file I created with Windows Notepad named Robocopy_Daily_Mirror.bat (when you are saving a batch file in notepad change "save as type" from "Text Documents (*.txt)" to "All Files"):

robocopy D:\ACCI E:\ACI\Backups\ACCI /MIR /W:0 /R:1 >> E:\ACI\Backups\RoboCopyBackup.log

If there are files with specific attributes you want to exclude, e.g., hidden files, you can use the /XA option.

/XA:[RASHCNETO] :: eXclude files with any of the given Attributes set.

In this case, I know that the only files in the source directory that have the "hidden" attribute set are thumbs.db files, which are database files containing the small images that are displayed when you view a folder in Thumbnail view rather than Detail, Icon, List, or Tile view. The Windows operating system generates those files automatically, so I could exclude them, but I chose to include those as well. If I wanted to exclude them, I could add the option /XA:H to the command. You can see the options for the Robocopy command by issuing the command robocopy /? at a command prompt.

I wanted the backup script to run Monday through Friday at 8:00 PM, so I issued the command below at a command prompt:

schtasks /create /TN "Robocopy_Daily_Mirror" /TR "c:\users\administrator\documents\scripts\Robocopy_Daily_Mirror.bat" /SC Weekly /D MON,TUE,WED,THU,FRI /ST 20:00

The /create parameter indicates I want to create a new scheduled task and the /TN parameter identifies the name I want to give to the new task, in this case "Robocopy_Daily_Mirror." The /TR parameter is a "task run" parameter indicating the name and location of the batch file I want to run while the /SC option indicates it is to be run every week with the /D parameter specifying the days it should be run every week. The /T parameter specifies the time the task should be run, which is specified in 24-hour clock time, aka "military time," in this case at 8:00 PM every day.

You can see all of the options for the Windows Task Scheduler command by issuing the command schtasks /? at a command prompt. You can see all of the options for creating new tasks by issuing the command schtasks /create /?.

When I issued the command, I saw a message indicating the task was successfully created.

D:\ACCI>schtasks /create /TN "Robocopy_Daily_Mirror" /TR "c:\users\administrator\documents\scripts\Robocopy_Daily_Mirror.bat" /SC Weekly /D MON,TUE,WED,THU,FRI /ST 20:00
SUCCESS: The scheduled task "Robocopy_Daily_Mirror" has successfully been created.

D:\ACCI>

References:

  1. How to create a backup script using Robocopy
    By: Jacob Surland
    Date: August 23, 2013
    Caught In Pixels