ROBOCOPY - Create Backup Script

How to Create a Backup Script using ROBOCOPY Command


   Information
There are many paid and free software solutions available to backup critical data and files on a computer system. Many users, however, are unaware of an inbuilt Windows 7 command called ROBOCOPY (Robust File Copy) that allows users to create simple or highly advanced backup strategies.


In its simplist form, ROBOCOPY can be likened to an extension of XCOPY on steroids. Some of the more important features that make ROBOCOPY an attractive backup alternative are:
  • multi-threaded copying
  • mirroring or synchronisation mode between the destination and source
  • automatic retry and copy resumption
The examples shown below are primarily geared towards less experienced users, especially those that are unfamilar with batch file creation and running. More experienced users, however, are welcome to explore some of the advanced functionality offered by ROBOCOPY here:

http://technet.microsoft.com/en-us/library/cc733145(WS.10).aspx

and also here:

http://www.theether.net/download/Microsoft/Utilities/robocopy.pdf

or by simply typing robocopy /? at a cmd window prompt.


   Note
ROBOCOPY is a FOLDER copier, not a FILE copier - the source and destination syntax arguments in ROBOCOPY can only be folder names.


:note: Creating a BACKUP strategy

The easiest way to use the ROBOCOPY command to create a backup strategy is by creating a batch (.BAT) file. A batch file is a simple text file, that when executed, runs several commands one after the other.

Step 1

Click :orb: and in the search box, type notepad. Select Notepad to open up a new blank Notepad document.


Step 2

Type the ROBOCOPY commands into the Notepad document, save it as a .BAT file, and then execute it.

In the example below, I have 3 folders (Data1, Data2, and Data3) containing some data that I wish to backup. One folder is located on E: drive and the other two are located on F: drive. I wish to back these up as follows:

Data1 folder on E: backup to a folder called Backups on G: (external USB drive)
Data2 folder on F: backup to a folder called Backups on G: (external USB drive)
Data3 folder on F: backup to a folder called Backups on Q: (network storage drive)

The general format of the ROBOCOPY command is:

Code:
robocopy <source> <destination> <options>
In the empty Notepad document, the simplist form of the command would look like this:

Code:
robocopy E:\Data1 G:\Backups\Data1
robocopy F:\Data2 G:\Backups\Data2
robocopy F:\Data3 Q:\Backups\Data3
pause
   Tip
If source or destination paths contain spaces in them, enclose these in double quotes e.g. "C:\My Data\My Music"



Only the source
  • E:\Data1
  • F:\Data2
  • F:\Data3
and the destination
  • G:\Backups\Data1
  • G:\Backups\Data2
  • Q:\Backups\Data3
are mandatory inputs into the ROBOCOPY command.

   Tip
The PAUSE command at the bottom of the .BAT file allows the cmd window to stay open after it has completed to allow me to see the output from ROBOCOPY.


If I save the .BAT file to my Desktop, and run it by double-clicking it, then a cmd window is opened and the .BAT file executes the three ROBOCOPY commands as shown below.

R1.JPG

The same information is repeated for every ROBOCOPY line in the .BAT file.




In order to utilise some of the powerful functionality in ROBOCOPY, I need to utilise some options in the ROBOCOPY command line. In this next example I want to edit my existing backup strategy such that:
  1. All sub-folders within my data folders are backed up, even if they are empty.
  2. The backup only copies newer files to my existing backup - this means a faster backup time.
  3. The percentage progress counter for copying is not shown - this neatens the overall appearance of the ROBOCOPY information, and creates a smaller log file.
  4. The information normally echoed to the cmd window is saved to a log file that I can examine at a later stage.
In order to do this, I need to specify some additional options in my ROBOCOPY commands like this:

Code:
robocopy E:\Data1 G:\Backups\Data1 [B][COLOR=red]/e /mir /np /log:backup_log.txt[/COLOR][/B]
robocopy F:\Data2 G:\Backups\Data2 [B][COLOR=red]/e /mir /np /log+:backup_log.txt[/COLOR][/B]
robocopy F:\Data3 Q:\Backups\Data3 [B][COLOR=red]/e /mir /np /log+:backup_log.txt[/COLOR][/B]
pause
Where:
/e = copy all sub-folders, even empty ones
/mir = mirror (check the files in the destination, and only copy newer files)
/np = no progress counter
/log: = create a logfile

   Tip

Note the use of the /log+: option in the 2nd and 3rd line of the .BAT file. This option ensures that the results of the 2nd and 3rd ROBOCOPY are appended to the log file created in the 1st ROBOCOPY line, meaning I only need one log file to capture all the information I backup.

The log file is always saved to the same folder as the .BAT file - in my case, the folder is saved to my Desktop.


Since the output from ROBOCOPY is written to the log file, the cmd window will not display the output from ROBOCOPY. If I wish to have this information written to both the log file and the cmd window for visual tracking of the backup process, then I can add the /tee option to each line in the .BAT file, as shown below.

Code:
robocopy E:\Data1 G:\Backups\Data1 /e /mir /np [B][COLOR=red]/te[/COLOR][COLOR=red]e[/COLOR][/B] /log:backup_log.txt
robocopy F:\Data2 G:\Backups\Data2 /e /mir /np [COLOR=red][B]/tee[/B][/COLOR] /log+:backup_log.txt
robocopy F:\Data3 Q:\Backups\Data3 /e /mir /np[COLOR=red][B] /tee[/B][/COLOR] /log+:backup_log.txt
pause
This is an example of the typical output to the the log file - it looks exactly the same as what is echoed to the cmd window.

Code:
-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------
  Started : Sun Sep 18 23:35:01 2011
   Source : E:\Data1\
     Dest : G:\Backups\Data1\
    Files : *.*
 
  Options : *.* /S /E /COPY:DAT /PURGE /MIR /R:1000000 /W:30 
------------------------------------------------------------------------------
                    2 E:\Data1\
------------------------------------------------------------------------------
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         0
   Files :         2         0         2         0         0         0
   Bytes :   442.1 k         0   442.1 k         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : Sun Sep 18 23:35:01 2011
Since one of the data folders I am backing up is being copied across a network, I want to ensure that any possible network outages do not cause some critical files to be skipped in the backup. To do this, I can make use of the /z option in the 3rd line of my .BAT file (backup to my network storage) as shown below.

Code:
robocopy E:\Data1 G:\Backups\Data1 /e /mir /np /tee /log:backup_log.txt
robocopy F:\Data2 G:\Backups\Data2 /e /mir /np /tee /log+:backup_log.txt
robocopy F:\Data3 Q:\Backups\Data3 /e /mir /np [B][COLOR=red]/z[/COLOR][/B] /tee /log+:backup_log.txt
This option implements a "retry" for the copying. If I were part way through the copying process, and I lost connection with the network, then ROBOCOPY would automatically restart the copying at the point of failure once the network connection was re-established. It would retrying a million times every 30 seconds (the default settings shown in the image above). The only drawback with this option, is that it can significantly increase the backup time.

ROBOCOPY also has the ability to perform faster multi-threaded copying by simply using the option /mt. I can choose the number of threads to use by specifying a number between 1 and 128 after the /mt option, or if I just specify /mt without a number then the it will use 8 threads by default. In the example below, I use 4 threads to copy to my USB drive, and 8 threads (no number) to copy to my network drive.

Code:
robocopy E:\Data1 G:\Backups\Data1 /e /mir /np /tee [B][COLOR=red]/mt:4[/COLOR][/B] /log:backup_log.txt
robocopy F:\Data2 G:\Backups\Data2 /e /mir /np /tee [COLOR=red][B]/mt:4[/B][/COLOR] /log+:backup_log.txt
robocopy F:\Data3 Q:\Backups\Data3 /e /mir /np [COLOR=black]/z[/COLOR] /tee [COLOR=red][B]/mt[/B][/COLOR] /log+:backup_log.txt
Below is a template that you can use to create your own backup strategy using ROBOCOPY. Simply copy and paste the lines into a blank text document, and then edit as appropriate.

Code:
rem --- Edit the lines below to create your own backup strategy
rem --- The /mir option has been left out for safety sake
rem --- Add more lines for each new folder requiring backup
rem --- Specified 4 threads to use for multi-threaded copying
rem --- The results of the backup can be found in my_backup_log.txt 
robocopy <source> <destination> /e /np /tee /mt:4 /log:my_backup_log.txt
robocopy <source> <destination> /e /np /tee /mt:4 /log+:my_backup_log.txt
robocopy <source> <destination> /e /np /tee /mt:4 /log+:my_backup_log.txt
pause
   Tip

The can automate your backup's by using the Windows 7 Task Scheduler to run the .BAT file at specific times.

For more information please refer to this tutorial by Orbital Shark:
http://www.sevenforums.com/tutorials/12444-task-scheduler-create-new-task.html


I hope this brief tutorial helps you access the power and simplicity of ROBOCOPY to create some effective backup strategies.

Regards,
Golden
 
Last edited by a moderator:
Locks on your front door can be a pain, too, especially when you have an armload of groceries. But, removing them entails risk.

Once you learn to manage UAC, it's really not a big deal unless you're constantly installing and uninstalling software.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell XPS 15 L502X
OS
Windows 10 Home, 64-bit
Thanks for this post Golden!!!
Great post and very useful!!! We need more post like this especially a template for daily, weekly Robocopy backup!!!
 

My Computer

OS
Windows 7 pro x64
I'm well aware that /MIR = /PURGE + /E. That will never delete files from source.

Microsoft Technet said:
/PURGE : Deletes destination files and directories that no longer exist in the source.
That means that Robocopy will delete files from destination, if you have deleted them from source.

Neither /PURGE nor /MIR will delete files from source. Please test it, you'll see.

I will NEVER use /MIR in a backup script. If I accidentally delete something but don't notice it, then run my script, where is the file/folder? Nowhere. Poof! Gone! I may not realize the problem for months.
In fact, this exact scenario came up at work just a couple weeks ago. A co-worker had written a robocopy script for a remote file share to backup one USB drive to another. Part of this script was /mir, /mon:1 and /mot:15. Thank whoever that the script wasn't running when I was emailed asking to restore an entire project folder with 3GB 's of data that was accidentally deleted.
People don't seem to realize we're not only backing up in case a drive fails, but also in case someone's brain fails. :)
 

My Computer

Computer Manufacturer/Model Number
Dell Optiplex 745
OS
Windows 7 Enterprise x64
CPU
Core2Duo
Memory
8GB DDR2
Graphics Card(s)
nVidia
Sound Card
on-board
Monitor(s) Displays
20"
Screen Resolution
1400x900
Hard Drives
160GB SATA
You miss the point. The tutorial says that MIR will delete files from source, which is not true. Neither /PURGE nor /MIR will delete files from source.

As for your situation, yes, what you want is a backup, not a mirroring. Mirroring is a very useful backup technique, but it doesn't replace having archived backups (which is what you want). A mirrored drive or directory ensures that if the source is damaged or fails, you can begin working again from the mirror which is very recent or even real time. Mirroring and point-in-time or archived backups are different techniques, both useful, for different needs.

You still need archived backups, which are backups of files as at a particular point in time. That way, when you realize that you deleted something last week that you now need, you can restore from a two-week-old backup.

But, the tutorial is clearly wrong. Neither /PURGE nor /MIR will delete files from source. Never.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell XPS 15 L502X
OS
Windows 10 Home, 64-bit
I didn't miss the point. I was round-a-boutly agreeing with you. My point was if /mir is used in a backup scheme, and I accidentally delete something from the source, then run the script, I'm screwed.
 

My Computer

Computer Manufacturer/Model Number
Dell Optiplex 745
OS
Windows 7 Enterprise x64
CPU
Core2Duo
Memory
8GB DDR2
Graphics Card(s)
nVidia
Sound Card
on-board
Monitor(s) Displays
20"
Screen Resolution
1400x900
Hard Drives
160GB SATA
Yep, that would be true. You don't want mirroring, at least not frequent mirroring. I use a combination of archived backups and a mirrored backup. Both are useful, for different goals.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell XPS 15 L502X
OS
Windows 10 Home, 64-bit
Hello, I cannot get this script to work???CMD prompts and just keeps running this data over and over? please help
robocopy E:\Data1 G:\Backups\Data1 robocopy F:\Data2 G:\Backups\Data2 robocopy F:\Data3 Q:\Backups\Data3 pause
 

My Computer

Computer Manufacturer/Model Number
HP p7-1262
OS
Windows 7 Ultimate x64
CPU
Intel Core i5-2400 (2nd generation)
Motherboard
Foxconn, 2ADA
Memory
6GB
Graphics Card(s)
Intel(R) HD Graphics, 1988mb
Sound Card
onboard
Monitor(s) Displays
LG E244
Hard Drives
1TB
Nice tutorial I will definitely use this!!!
 

My Computer

Computer Manufacturer/Model Number
HP p7-1262
OS
Windows 7 Ultimate x64
CPU
Intel Core i5-2400 (2nd generation)
Motherboard
Foxconn, 2ADA
Memory
6GB
Graphics Card(s)
Intel(R) HD Graphics, 1988mb
Sound Card
onboard
Monitor(s) Displays
LG E244
Hard Drives
1TB
Hello, I cannot get this script to work???CMD prompts and just keeps running this data over and over? please help
robocopy E:\Data1 G:\Backups\Data1 robocopy F:\Data2 G:\Backups\Data2 robocopy F:\Data3 Q:\Backups\Data3 pause

1. That won't work, AFAIK, you can't just string together separate commands/tasks and enter them all as a single line into CMD.

2. If you want to have all three of those tasks run, do it in a batch file, where each is on a separate line.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell XPS 15 L502X
OS
Windows 10 Home, 64-bit
Welcome to SevenForums,

Try this:

Code:
robocopy E:\Data1 G:\Backups\Data1
robocopy F:\Data2 G:\Backups\Data2
robocopy F:\Data3 Q:\Backups\Data3
pause

Regards,
Golden
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Golden Mk. I.4
OS
Windows 10 Pro x64 ; Xubuntu x64
CPU
Intel i7 860 @ 2.80 GHz O/C'ed to 4.0GHz
Motherboard
Gigabyte P55A-UD3R Rev.1. Award BIOS F13
Memory
16GB Corsair Vengance DDR3 @ 661 MHz Dual Channel (9-9-9-24)
Graphics Card(s)
EVGA NVidia GTX 560 1024MB
Sound Card
Realtek Integrated
Monitor(s) Displays
Dual Samsung SyncMaster 2494HS
Screen Resolution
1920*1080 and 1920*1080
Hard Drives
1*Samsung 840 EVO 120GB SSD;
1*OCZ Vertex 2 60GB SSD;
2*Samsung F3 SpinPoint 1TB in RAID0;
1*Samsung F1 SpinPoint 1TB;
2*Western Digital 1TB External USB 3.0
1*Western Digital 500GB External USB 3.0
1*Seagate 500GB External USB 2.0
PSU
Thermaltake ToughPower QFan 750W
Case
Thermaltake Element S VK60001W2Z
Cooling
Corsair H60 Water Cooling, 2*230mm and 2*80mm case fans
Keyboard
Logitech G110
Mouse
Logitech MX518
That is what i did, dis not work
 

My Computer

Computer Manufacturer/Model Number
HP p7-1262
OS
Windows 7 Ultimate x64
CPU
Intel Core i5-2400 (2nd generation)
Motherboard
Foxconn, 2ADA
Memory
6GB
Graphics Card(s)
Intel(R) HD Graphics, 1988mb
Sound Card
onboard
Monitor(s) Displays
LG E244
Hard Drives
1TB
Provide some more detail, perhaps the error message you get..............
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Golden Mk. I.4
OS
Windows 10 Pro x64 ; Xubuntu x64
CPU
Intel i7 860 @ 2.80 GHz O/C'ed to 4.0GHz
Motherboard
Gigabyte P55A-UD3R Rev.1. Award BIOS F13
Memory
16GB Corsair Vengance DDR3 @ 661 MHz Dual Channel (9-9-9-24)
Graphics Card(s)
EVGA NVidia GTX 560 1024MB
Sound Card
Realtek Integrated
Monitor(s) Displays
Dual Samsung SyncMaster 2494HS
Screen Resolution
1920*1080 and 1920*1080
Hard Drives
1*Samsung 840 EVO 120GB SSD;
1*OCZ Vertex 2 60GB SSD;
2*Samsung F3 SpinPoint 1TB in RAID0;
1*Samsung F1 SpinPoint 1TB;
2*Western Digital 1TB External USB 3.0
1*Western Digital 500GB External USB 3.0
1*Seagate 500GB External USB 2.0
PSU
Thermaltake ToughPower QFan 750W
Case
Thermaltake Element S VK60001W2Z
Cooling
Corsair H60 Water Cooling, 2*230mm and 2*80mm case fans
Keyboard
Logitech G110
Mouse
Logitech MX518
If in Vista or later, right-click the batch file and choose "Run as Administrator".
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell XPS 15 L502X
OS
Windows 10 Home, 64-bit
I have read the thread and haven't seen anyone else ask this. We are using Robocopy to back up and restore our users data in prep for either migration from Vista to Win 7 or lfe cycling old equipment.
Robocopy works great we just point the users to the batch file location and theyrun the back up. Later they run the restore again this works great.

Here's the problem, one user got a little nosey and figured out that they could look into the files that had been backed up from another user.

So how can the files be secured, but still accessible for the user who they belong to?
 
Last edited:

My Computer

OS
Windows 7 Ultimate x64
As I see it, this isn't really a Robocopy issue. It's an issue that you're making your shares too broad, allowing everyone to read everyone else's folders.

Perhaps you could make the backup folders non-readable by standard users? Or, share each folder only to its user, so other users have no read rights.

You also have the ability, using Robocopy options, to preserve or eliminate file attributes such as ownership. If a user has no security rights to another user's files, he wouldn't be able to read them locally, though I'm uncertain if that changes over a network. As I say, limiting the network shares to each user's own folder may be the way to go.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell XPS 15 L502X
OS
Windows 10 Home, 64-bit
Thank you...giving this a run now.

Out of interest. I am rescuing data from a drive that is slowing down aka dieing. How does Robocopy identify this in regards to corrupt data. Does it skip over and move on, will it just simply hang and await a force closure or simply auto end the process?

I dont expect an issue as I believe the corrupt data area is actually in the repository recycle folder (hidden).

Thanks
 

My Computer

Computer Manufacturer/Model Number
EVGA X55
OS
Windows 7 Ultimate x64
CPU
i7920 2.66ghz
Motherboard
MSI
Memory
12gb Triple DDr
Graphics Card(s)
ATI HD4870
Sound Card
RME Fireface 800
Monitor(s) Displays
Samsung 22" / Philips 17''
Screen Resolution
1650x1080
Hard Drives
OCX SSD Vertex2
Maxtor Raptor
Samsung
PSU
Silent 650w
Case
Custom designed SilentCase
Cooling
Air
Keyboard
Microsoft Digital Media Pro Keyboard
Mouse
Logitech
Internet Speed
50MB Virgin
Other Info
I'm no Pro but try to learn quickly!
Robocopy works at file level, so if it cannot properly read a file I presume that particular one would be skipped and listed as failed.

Use Robocopy's logging options to create a log file. At the bottom you will see a column for "Failed, which will show how many files/folders failed to copy.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell XPS 15 L502X
OS
Windows 10 Home, 64-bit
There is a /R:n switch which determines the number of retries in the case of a copy failure. If you don't specify this switch, robiocopy uses a default of 1 million retries with 30 secs between retries.

I often use /R:3 - 3 retries with 30 secs between retries.
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Golden Mk. I.4
OS
Windows 10 Pro x64 ; Xubuntu x64
CPU
Intel i7 860 @ 2.80 GHz O/C'ed to 4.0GHz
Motherboard
Gigabyte P55A-UD3R Rev.1. Award BIOS F13
Memory
16GB Corsair Vengance DDR3 @ 661 MHz Dual Channel (9-9-9-24)
Graphics Card(s)
EVGA NVidia GTX 560 1024MB
Sound Card
Realtek Integrated
Monitor(s) Displays
Dual Samsung SyncMaster 2494HS
Screen Resolution
1920*1080 and 1920*1080
Hard Drives
1*Samsung 840 EVO 120GB SSD;
1*OCZ Vertex 2 60GB SSD;
2*Samsung F3 SpinPoint 1TB in RAID0;
1*Samsung F1 SpinPoint 1TB;
2*Western Digital 1TB External USB 3.0
1*Western Digital 500GB External USB 3.0
1*Seagate 500GB External USB 2.0
PSU
Thermaltake ToughPower QFan 750W
Case
Thermaltake Element S VK60001W2Z
Cooling
Corsair H60 Water Cooling, 2*230mm and 2*80mm case fans
Keyboard
Logitech G110
Mouse
Logitech MX518
If you're copying locally (e.g. second drive in another bay, or external USB/Firewire/eSATA drive) you might want to use something like /R:4 /W:2. That would retry each four times with a two-second interval. Robocopy's default of a 30-second interval is intended more for network access situations.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell XPS 15 L502X
OS
Windows 10 Home, 64-bit
Robocopy for Linked folders

Hi I want to use robocopy or xcopy to copy a HTM file with a linked folder. Its not copying the linked folder now. But when i manually copy either the HTM file or the folder, both of then get copied .
 

My Computer

Computer Manufacturer/Model Number
Dell Latitude
OS
Win7 32 Bit
Back
Top