Batch Files

Backups Using Robocopy

Introduction

Everyone who uses their own computer should be making backups. Copying files from a backup is so much easier than trying to recover a lost or deleted file. Drives sometimes fail and getting data off of those is time consuming and expensive if you have to have them professionally recovered.

There are many backup solutions available and also utilities such as FastCopy, TeraCopy, and UltraFastCopy

I use Robocopy that has been around since 1996, included with most versions of Windows and been kept updated. This page explains how I do my backups.

Ideally backups should be done at least daily but what I do is do it roughly weekly or whenever I think of doing it.

I keep the backups on two separate USB 2Tb external hard drives with two copies of the files on each of them. The first copy is a full backup of the files on my computer. The second copy is a full copy and whatever files have changed. This means that this second copy of the files contain everything I have created since I started using the external hard drive. Both backup copies are then copied to a second 2Tb external hard drive.

my backup drives

My backup drives


3-2-1 Strategy

The 3-2-1 backup stategy has been around seemingly forever. Put very simply, keep 3 copies of your files, one original and the other two on different backup devices. As backups, you should be using 2 different types of media to store the backups, for example tape and and external hard drive. Finaly, at least 1 of the backups should be kept in a physically separate place than your original.

This strategy works well but I keep my backups on two external 2Tb hard drives of which I now have six of as I have filled four of them. The drives contain a lot of the same files but I would sooner keep them than delete something I later want or need. There are lots of different options available.

Only making a single copy of the files will work but if you delete a file from your computer then the only version of that file you have available is the one on your backup drive and that means there is no backup of that file. Drives fail, files get lost or deleted, so if something happens to that file then it needs to be recovered somehow and that might not be successful, in which case the file is gone for good.


Types of Backup

There are three types of backup that can be made:

Full

A full backup is a complete backup of all the files you have chosen to backup.

Incremental

An incremental backup is a copy of the files that were changed or created since the last backup.

Differential

A differential backup is a copy of the files that were change or created since the last full backup.


What to Backup

Do you want to backup the entire drive? Do you want to backup the Windows, appdata, and program files directories? Do you want to just backup the files you created?

If you wantt backup the entire drive, you may be better off with drive cloning or cloning software. Some free ones are Clonezilla, Cobian Backup, Disk Genius, Redo Rescue, and Rescuezilla.

What I decided to do is backup just the files I created or downloaded. I wanted a "current" backup of the files on the computer and another copy of everything I've created or downloaded on the computer no matter whether it was later moved or deleted. I also wanted to create a second copy of everything I had backed up on another disk. %USERPROFILE%, this includes the desktop, documents, downloads, music, pictures, and video folders.

What I use for my backups are two 2Tb USB external hard drives. I used the command line interface (CLI) command VOL to change their names to backupa and backupb. The backuped files are first written to backupa in a named backup-current and then copied to the complete named backup. The entire disk is then copied to backupb.

When the computer was almost new it took around 20 minutes to do a full backup, from start to finish. The complete backup now takes around a couple of hours. The longer you leave it between backups, the longer it takes. One advantage of using Robocopy is that the computer can still be used while the backup is being done.

The batch file I created first checks that the correct drives are connected to the computer before it starts copying the files. It then gets the drive letters. If the script cannot find the drives it displays a warning message and stops.


My Batch Files

Server Backup

I run two computers. One is a web server and that has programs installed on the root of the C: drive. Those need backing up as well as my user files.

The batch file checks the correct backup drive is attached to the computer. It checks if the robocopy log file exists and if it does, deletes it. The script then has a list of directories I want backed up and then works its way through them, backing them up to the attached drive. This set of files is a copy of whatever is on the C: drive. Once the list of directories is exhausted, it copies everything the script just copied. This copy contains everything that has been written to the C: drive, regardless of whether it has since been deleted or moved.

The batch file needs to be run as an administrator.

@echo off
setlocal EnableDelayedExpansion
set bkpdrv=""
set bkpname=brisray-server
set BkpVolName=RayBackupA
Set n=0
set ltrs=ABCDEFGHIJKLMNOPQRSTUVWXYZ

:fnddrv
	:Loop 
	call set ltr=%%ltrs:~%n%,1%%
  	set /a n +=1
  	vol %ltr%: 2>nul|find /i " %BkpVolName%">nul||if %n% lss 26 (goto :loop)
	if %n% equ 26 (goto error
	) else (set bkpdrv=%ltr%
		goto work)

:error
	echo Cannot find %1 volume
	set n=0
	echo Insert correct drive and run again
	pause
	exit

:work	
	echo Ready to start robocopy backup of the server
	echo.
	pause
	echo.
	if exist %USERPROFILE%\Documents\backup.log (del %USERPROFILE%\Documents\backup.log)
	for %%G in (Apache Certbot Dell Perl %HOMEPATH%\Desktop %HOMEPATH%\Documents %HOMEPATH%\Downloads %HOMEPATH%\Music %HOMEPATH%\Pictures %HOMEPATH%\Videos) do ( 
		echo Creating copy of current %%G directory on %BkpVolName% %bkpdrv%:
		robocopy C:\%%G %bkpdrv%:\%bkpname%-current\%%G /s /purge /COPY:dat /R:3 /W:10 /MT:10 /XF *.tmp /LOG+:"%USERPROFILE%\Documents\backup.log"
		echo.
	)
	echo Creating backup of all current files on %BkpVolName% %bkpdrv%: to main (all files) backup on %BkpVolName% %bkpdrv%:
	robocopy %bkpdrv%:\%bkpname%-current %bkpdrv%:\%bkpname% /S /R:3 /W:10 /MT:10 /COPY:dat /XF *.tmp /LOG+:"%USERPROFILE%\Documents\backup.log"	
:eof

%USERPROFILE% and %HOMEPATH% are computer environment variables. Both return the path to the user profiles. %USERPROFILE% contains the full path including the drive letter, %HOMEPATH% just contains the path, without the drive letter.

All environment variables can be seen by using the SET command at a command prompt.

%%G is a variable used to contain the elements of the directory list.

Robocopy Switches Used

/S - Copy subdirectories excluding empty directories.
/purge - Deletes destination files and directories that no longer exist in the source.
/COPY:dat - The default value for the /COPY option is DAT (data, attributes, and time stamps).
/R:3 - If a copy fails, then try 3 times to copy it
/W:10 - Wait for 10 seconds before retrying to copy the files if it fails
/MT:10 - Use multi-threading with 10 threads
/LOG+ - Append to a log file. If it does not exist, it is created
/MIR - Mirrors a directory tree
/XA:s - Exclude system files and directories
/XF *.tmp - Do not copy .tmp files

Computer Backup

My day to day computer backup is slightly different. All I want to backup are the files that I download or create. This part of the script is nearly the same as the one I use to backup the web server. One important difference is that all the files backed up on one of the backup drives is copied to another. This means I have four backups of the files on the computers on two different drives. These are wo sets of the current files on the server and my computer and two sets of full backups of every file I have ever downloaded or created on both computers.

The batch file also needs to be run as an administrator.

@echo off
setlocal EnableDelayedExpansion
set bkp1drv=""
set bkp2drv=""
set bkpname=brisray-laptop
set BkpVol1=RayBackup3a
set BkpVol2=RayBackup3b
set n=0
set ltrs=ABCDEFGHIJKLMNOPQRSTUVWXYZ

For %%A in (%BkpVol1% %BkpVol2%) do call :fnddrv %%A

goto :eof

:fnddrv
	set n=0	
	set volName=%1
	:Loop 
	call set ltr=%%ltrs:~%n%,1%%
  	set /a n +=1
  	vol %ltr%: 2>nul|find /i " %volName%">nul||if %n% lss 26 (goto :loop)
	if %n% equ 26 (goto error)
	if %1==%BkpVol1% (
		set bkp1drv=%ltr%
		goto eof)		
	if %1==%BkpVol2% (
		set bkp2drv=%ltr%
		goto work)

:error
	echo Cannot find %1 volume
	set n=0
	echo Insert correct drive and run again
	pause
	exit

:work	
	echo Ready to Robocopy drive C: to drive %BkpVol1% (%bkp1drv%:) and then
	echo create backup of all files on %BkpVol1% (%bkp1drv%:) on %BkpVol2% (%bkp2drv%:).
	echo This process will take approx. 20 minutes on a new Windows install.
	echo The process on this laptop takes several hours.
	echo.
	pause
	echo.
	if exist %USERPROFILE%\Documents\backup.log (del %USERPROFILE%\Documents\backup.log)
	echo Creating copy of current files on %BkpVol1% %bkp1drv%:
	echo.
	for %%G in (Desktop Documents Downloads Music Pictures Videos) do ( 
		echo Creating copy of current %%G directory on %BkpVol1% %bkp1drv%:
		robocopy %USERPROFILE%\%%G %bkp1drv%:\%bkpname%-current\%%G /s /purge /COPY:dat /R:3 /W:10 /MT:10 /XF *.tmp /LOG+:"%USERPROFILE%\Documents\backup.log"
		echo.
	)
	echo Creating backup of all current files on %BkpVol1% %bkp1drv%: to main (all files) backup on %BkpVol1% %bkp1drv%:
	robocopy %bkp1drv%:\%bkpname%-current %bkp1drv%:\%bkpname% /s /R:3 /W:10 /MT:10 /COPY:dat /XF *.tmp /LOG+:"%USERPROFILE%\Documents\backup.log"
	echo.
	echo Creating backup of all files on %BkpVol1% %bkp1drv%: on %BkpVol2% %bkp2drv%:
	robocopy %bkp1drv%: %bkp2drv%: /MIR /COPY:dat /R:3 /W:10 /MT:10 /XA:s /LOG+:"%USERPROFILE%\Documents\backup.log"
:eof


Multiple Backups

I mentioned using robocopy as a backup solution in a thread on Reddit. Someone mentioned that a danger of using it is that if a computer was attacked by ransomware, which will encrypt all the files, then there is a risk of copying the ruined files to the backup.

I think this is true of any backup solution, not only that but ransomware can also attack attached drives. A long time ago, one of my jobs was to place a daily backup tape into a machine and run a backup on that. The tapes were labelled Monday, Tuesday, Wednesday and so on. That got me thinking about doing the same sort of thing with hard drive backups. Can a batch file get today's day from the date and check the backup drive's volume label for that day?

Of course they can!

There are several methods of getting the date, I will look at just three of them.

Day of the Week

DATE

The date is kept in an environment variable called date. That can be seen by using:

ECHO %date%

Which returns the date in the form:

Fri 10/31/2025

The format may be different on differing operating systems and your location settings, so check first!

The day part of the date can be extracted by using:

set dow=%date:~0,3%

Another way of getting the day is to find the first space in the date, get everything up to that, then remove the last character which will be the space:

for /f "tokens=1 delims= " %%a in ("%date%") do ( set dow = %%a:~0,-1% )

The variable dow should now contain the first three letters of the day of the week. You could just label the backup drives Mon, Tue, Wed and so on, but to get the full day of the week the following can be used:

	@echo off
	setlocal EnableDelayedExpansion
	set dow=%date:~0,3%
	echo The day of the week is %dow%
	for %%a in (Monday Tuesday Wednesday Thursday Friday Saturday Sunday) do (
		rem the following intermediate step is necessary!
		set x=%%a
		set substr=!x:~0,3!
		if !substr!==%dow% set dow=%%a
	)
	echo The day of the week is now %dow%

This returns:

The day of the week is Fri
The day of the week is now Friday

WMIC

WMIC is the Windows Management Instrumentation command-line. If you type

WMIC Path Win32_LocalTime

what you get is:

Day  DayOfWeek  Hour  Milliseconds  Minute  Month  Quarter  Second  WeekInMonth  Year
31   5          10                  15      10     4        14      5            2025

The day of the week in numeric form cn be gotten by getting the DayofWeek. Then using another for command, matching that number to a list of the days:

@echo off  
set daysofweek=Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday  
for /F "skip=2 tokens=2-4 delims=," %%a in ('WMIC Path Win32_LocalTime Get DayOfWeek') do set daynumber=%%a  
for /F "tokens=%daynumber% delims=," %%b in ("%daysofweek%") do set dow=%%b
echo %dow%

returns:

Friday

PowerShell

The day of the week can be gotten from PowerShell in a batch file very easily. This is done by using:

@echo off
for /f %%a in ('powershell ^(get-date^).DayOfWeek') do set dow=%%a
echo %dow%

which returns:

Friday

Check the Volume Label and get the Drive Letter

Now we have the day of the week, in the variable dow in the above examples, we can check each drive letter and its volume label to ensure the correct drive is being used.

for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (vol %%a: 2>nul | find "%dow%" >nul
if not errorlevel 1 set drvltr=%%a:)
if "drvltr%"=="" (echo Cannot find volume
) else (
echo Drive letter is %drvltr%)
)
pause

The robocopy commands can now be added after the pause command in the above script.


Logs

The log files for Robocopy can be large, several Mb in size. When using Robocopy for backups, both the destination drive and log files should be checked to ensure that everything was copied properly.

Robocopy is a powerful utility but with lots of switches which can be confusing to use. Then there are the Windows file permissioons to take into account, especially when copying logs from several computers to a master backup.

For example, I run a web server that has several directories written to the root of the C: drive that I backup. I also have a laptop that I backup. Both sets of backups are written to another external drive. Looking at the log summary, I found a lot of files were skipped and not copied. Looking through the log of all the files copied, I found a lot of messages saying ERROR 5 and "Access is denied".

This was because although an administrator on both machines, the file permissions were not the same. I found the solution on StackExchange and had to add /COPY:dat to the Robocopy command.


Copying System Drive Root Files - A Problem

Robocopy has a long known about strange and confusing behaviour when copying files from the root of a system drive. I don't know enough about the inner workings of Windows to say why it happens, but I can demonstrate it. Windows File Explorer (with show hidden files checked) shows my C: drive with three files in the root; dell.sdr, DumpStack.log, and FIOD.manifest. A simple command such as:

robocopy c:\ f:\backup /log:testlog.txt

appears to work, but both Windows and the command line dir command, both as user and administrator, show that there is no such directory as f:\backup. The log file created by Robocopy is even stranger. It says that the directory was created and that it found seven files in the root of the C: drive but only copied two of them. Of the five it did not copy it says it could not find DumpStack.log, DumpStack.log.tmp, hiberfil.sys, pagefile.sys, and swapfile.sys. How does it know the files are there, but then says it cannot find them?

Even odder is the fact that f:\backup was created as you can see it if you put f:\backup in Windows File Explorer or use the CD command at the command prompt. Windows shows two files in the directory; dell.sdr and FIOD.manifest. These files are not shown in the command prompt dir command.

What is Actually on the C: Drive?

Both the command prompt and PowerShell have some powerful commands to show everything on a drive. The CLI (command line interface) dir command has an /a switch that determines which file with which attributes to list in the results. Using this to show the files with the hidden (h) and system (s) bits set by giing the command:

dir /ahs c:\

returns the following result:

08/20/2025  08:50 PM    <DIR>          $Recycle.Bin
01/22/2020  03:06 AM    <JUNCTION>     Documents and Settings [C:\Users]
09/30/2025  04:31 AM            12,288 DumpStack.log.tmp
09/30/2025  04:31 AM     6,812,479,488 hiberfil.sys
09/30/2025  04:31 AM    13,958,643,712 pagefile.sys
09/30/2025  04:31 AM        16,777,216 swapfile.sys
10/02/2025  09:09 AM    <DIR>          System Volume Information

This now shows four of the seven files that robocopy knew about. What about the other three? To show all the files then PowerShell can be used with using the Get-ChildItem command with the -force switch to get everything that the command can find. The command:

Get-ChildItem -Path c:\ -force

returns the result:

-arh-    1/17/2020 12:36 AM          36122 dell.sdr
-a---     7/4/2025  1:17 AM          12288 DumpStack.log
-a-hs    9/30/2025  4:31 AM          12288 DumpStack.log.tmp
-arh-     1/2/2020  9:15 AM            836 FIOD.manifest
-a-hs    9/30/2025  4:31 AM     6812479488 hiberfil.sys
-a-hs    9/30/2025  4:31 AM    13958643712 pagefile.sys
-a-hs    9/30/2025  4:31 AM       16777216 swapfile.sys

which shows all seven files and their attributes.

What was Copied to the F: Drive?

The CLI dir command dir /ahs f:\ returns:

10/02/2025  08:56 PM    <DIR>  backup

The PowerShell command Get-ChildItem -Path f:\ -force returns:

d--hs     10/2/2025  8:56 PM     backup

Both commands show the backup directory was created and the PowerShell command shows the hidden and sytem bits for it were set.

To see what is inside the backup folder that was created the same commands can be used. The CLI dir command dir /ah f:\backup returns:

01/17/2020  01:36 AM  36,122 dell.sdr
01/02/2020  10:15 AM     836 FIOD.manifest

and the PowerShell command Get-ChildItem -Path f:\backup -force returns

-arh-      1/17/2020 12:36 AM     36122 dell.sdr
-arh-       1/2/2020  9:15 AM       836 FIOD.manifest

The PowerShell result is useful as it shows none of the system files were copied and neither was, for some reason, the DumpStack.log file.

Remediation

To show the backup directory then the attrib command can be used to unset the hidden and system attributes. The CLI command to do this is attrib -h -s f:\backup

After using this command the backup directory is viewable in both Windows File Explorer and at the command line. The command does not change the attrivutes of the files in the directory so both dell.sdr and FIOD.manifest are still both hidden.

Deleting these hidden files can be a problem. It cannot be done at the Windows CLI, so instead it has to done in Windows File Explorer (with show hidden files checked) or using PowerShell using Remove-Item -Path f:\backup\*.* -Force

The now empty backup directory can now be deleted.

Using PowerShell the backup directory can be deleted by using Remove-Item -Path f:\backup -Force

I have seen several suggested command switches to stop robocopy from hiding the backup directory such as /A-:SH, /A-:SHA, and /xa:sh but when copying files from the root of a system drive none seem to work and the backup directory is always hidden.

I have a web server tha has directories that need to be backed up in the main c: drive. Robocopy has an /xd switch to exclude directories but not a switch to only copy certain directories. This means I need several robocopy commands in a batch file to copy directories such as c:\apache, c:\certbot and so on.


Robocopy GUIs

For those more comfortable with a graphical interface there are several GUIs available for the utility:

Cinchoo - ChoEazyCopy, Yet Another RoboCopy GUI
RichCopy
Robo-Interface
RoboCop RoboCopy
RobocopyWin
RoboMirror
WinRoboCopy


Sources and Resources

21 Robocopy Examples With Screenshots (Active Directory Pro)
List of backup software (Wikipedia)
Robocopy (Microsoft Learn)
Robocopy.exe (SS64)
RRoboCopy - Ultimate Guide with Examples (Lazy Admin)
Robocopy command (Computer Hope)