How to create a backup script using Robocopy

Robocopy is a fantastic piece of software, there is only one major flaw. Which again also is the strength of it. It is a command line tool.

Command line tools seems to be hot again, and I really really hated them for a long period of time. But, I have come to love them again (?), even if it seems a super outdated technology in the age of the smartphone.

I must admit that Robocopy really does a more than a great job and I love it. And then it’s free – I love free software too.

Robocopy has always had an answer for a problem. Some guys at Microsoft made a graphical user interface for Robocopy, but it doesn’t give me the flexibility that I need. But if you feel like, you can have a look at them. The link is provided in the bottom of this article. In the end, they use the same command line tool.

Instead of spending a lot of time figuring out the syntax parameters you can use my commands.

Personally, I use Robocopy to take local backups. I use Robocopy in combination with online backup. I centralize a backup, from the various storage locations I have (NAS, data drives, system drive) and that backup I include in my Livedrive backup plan.

Anyway, one of the primary reasons for using Robocopy, rather than many other free backup products out there, is that it does a mirror backup. This means that it will make anything from the source directory to be reflected exactly the same way in the destination directory.

Just be sure that you do not get the source and destination the wrong way around. Robocopy can delete all your files extremely fast, if you mix up source and destination.

If you feel that your hand is shaking, start practicing with test directories. A mirror backup with the source and destination the wrong way around will erase your data faster, than you can spell ‘Oh Nooooo!’. This is not specific for Robocopy, it’s the nature (and point!) of a mirror copy tool. Do not switch source and destination!

The backup strategy

This is a basic backup strategy that you can modify for your exact needs. The  plan is this:

  1. Copy all necessary data from desktop computer to an external hard drive
  2. Copy all user settings from Windows to an external hard drive
  3. Copy all necessary data from a NAS to an external hard drive.

Command for step 1:

robocopy C:\Data T:\DesktopPC\C-Drive /MIR /XA:H /W:0 /R:1 /REG > C:\externalbackup.log

And the parameters explained for the nerds:

Parameter Description
C:\Data Is the source directory. I always store data that I backup in a folder called Data. It makes backup easy, just as well as it makes migration to a new computer very easy.
T:\DesktopPC\C-Drive Is the destination directory
/MIR This tells robocopy to make a mirror backup.
NB! Another important detail and word of warning, is that when you make a Mirrored backup, if you delete something in the Source, it will also be deleted in the Destination, when the script runs. This is also true if you delete something by accident.
/XA:H Leave out the hidden files. If they are hidden they are in my terminology not important.
/W:0 Wait time between retries, the default is 30 seconds. If a file is locked, because it is in use, the robocopy can wait and retry. I don’t want the backup to wait, it should continue straight away, otherwise it can take too long. I’ll get the file backed up the next time.
/R:1 The number of retries, if the file is locked. I set the to 1, the default is 1.000.000. If you combine that with with the /W paramater and a default of 30 seconds, that is potentially 30.000.000 seconds pr file permanently locked. The backup will never finish if you just have one single file that is permanently locked.
/REG This one saves new default values on /W and /R to the registry. In theory this means that I only need to use this parameter once, but I keep it around for when I restore Windows or get a new computer.
/XD Exclude Directories. This one is used to exclude directories like c:\temp. I only use it with fully qualified paths to be sure what it excludes.
/XF Exclude files. I use this to exclude certain file extensions, like *.lrprev
> C:\externalbackup.log This will start a new log file. I like to be able to see what the backup did.

Command for step 2:

robocopy C:\Users\Jacob T:\DesktopPC\C-Drive\Users\Jacob /MIR /XA:SH /W:0 /R:1 /REG /XJ >> C:\externalbackup.log

Add a line for each user using the computer. And the parameters explained for the nerds:

Parameter Description
C:\Users\Jacob Is the source directory and the directory in which my windows user profile is located. It contains your documents folder, favorites etc. A great folder to include in the backup.
T:\DesktopPC\C-Drive\Users\Jacob Is the destination directory
/MIR This tells robocopy to make a mirror backup.
/XA:SH Leave out the hidden files and the system files. The hidden files I don’t want and they are often part of the system and the system files and I don’t really need either in the backup. The system will generate them again, if it needs them. The S is important when you backup the user profile, because there’s a lot of system files.
/W:0 Wait time between retries, the default is 30 seconds. If a file is locked, because it is in use, the robocopy can wait and retry. I don’t want the backup to wait, it should continue straight away, otherwise it can take too long. I’ll get the file backed up the next time.
/R:1 The number of retries, if the file is locked. I set the to 1, the default is 1.000.000. If you combine that with with the /W paramater and a default of 30 seconds, that is potentially 30.000.000 seconds pr file permanently locked. The backup will never finish if you just have one single file that is permanently locked.
/REG This one saves new default values on /W and /R to the registry. In theory this means that I only need to use this parameter once, but I keep it around for when I restore Windows or get a new computer.
/XJ Exclude junction points. This is something funky in Windows. If you don’t include it, when you backup a user profile, it will run forever, because it enters an infinite loop. Just trust me in this one.
>> C:\externalbackup.log Notice the change from “>” to “>>”. This means that it will continue on the log from before. I used ‘>”, it would delete the log and start all over.

Command for step 3. Copy from the computer to the NAS, this one requires a special parameter, because the NAS is a LINUX based computer. In the beginning my backup kept copying files already backed up.

robocopy C:\Data\CDs \\NAS\Music\CDs /MIR /XA:H /W:0 /R:1 /REG /FFT >> C:\externalbackup.log

And the parameters explained for the nerds:

Parameter Description
C:\Data\CDs Is the source directory and the directory. I use Sonos for playing music in the house and I copy my ripped CDs to the NAS for both backup and for Sonos.
\\NAS\Music\CDs Is the destination directory. My Sonos music system points to this location.
/MIR This tells robocopy to make a mirror backup.
/XA:H Leave out the hidden files.
/W:0 Wait time between retries, the same as above.
/R:1 The number of retries, if the file is locked. Again the same as above.
/REG This one saves new default values on /W and /R to the registry. Same as above, once more.
/FFT This one is important when backing up to a NAS based on LINUX. I have two different brands and have hands on experience on a third, and they are all based on LINUX. If you leave out this parameter it will keep copying the files to you NAS, even though they are already present. This has to do with some timing issues – just trust me on this one.
>> C:\externalbackup.log Notice the change from “>” to “>>”. This means that it will continue on the log from before. I used ‘>”, it would delete the log and start all over.

Make a .bat file using Notepad.

Backup script using Robocopy

My personal backup script copies some stuff criss cross among my various locations. I have an external backup (well actually two, because one I store at my office and then swap them once every two weeks), two NAS (old and new one – I don’t trust the old one too much) and then my Desktop computer. I make sure that I have at least two copies of everything. But they all use the same commands as shown above, just with various source and destinations.

Set up the scheduling of the backup

I set up my backup at 7 pm, because that is likely that my computer is turned on by then. I don’t want to switch on my computer to get a backup.

Step 1: Find the control panel.

Step 1 - How to setup backup schedule

Step 2: Start the Schedule Tasks

Step 2 - How to setup backup schedule

Step 3: Create a new basic task

Step 3 - How to setup backup schedule

Step 4: Name the new task

Step 4 - How to setup backup schedule

Step 5: Select when to run the task

Step 5 - How to setup backup schedule

Step 6: Select what time to run the task.

Step 6 - How to setup backup schedule

Step 7: Select what to run – you want to run a program. A batch file is a program too.

Step 7 - How to setup backup schedule

Step 8: Find your backup batch file

Step 8 - How to setup backup schedule

Step 9: A summary – that looks just right.

Step 9 - How to setup backup schedule

Step 10: Done. The new task is in my scheduler.

Step 10 - How to setup backup schedule

And that’s it. You now have a high performance backup that cost you absolutely nothing!

Other useful parameters for Robocopy

Robocopy can be used for many great file operating tasks, not only mirror backup. Here’s a list of some of the useful copy, move or mirror parameters available for Robocopy. Just be sure only to use ONE of them at the same time, and always do a test on test data, before doing it on real data. Robocopy can delete data faster than anything if you do something the wrong way.

Parameter Description
/MOV This tells robocopy to move files from the source to the destination. Moving files include deleting the files from the source. Be sure you want to delete the files before using this parameter.
/MOVE This tells robocopy to move files and folders from the source to the destination. Notice the difference between the switches /MOV and /MOVE.  Moving files include deleting the files from the source. Be sure you want to delete the files before using this parameter. Do not combine with the switch /E, else it will not work as intended.
/E This tells robocopy to make a copy of all files recursively. This way robocopy will not clean up in the destination. Changed files at the source will be overwritten with new ones, new files will be added, but files deleted at the source will stay at the destination. For some this might be the right solution, for others it won’t.
/MIR This tells robocopy to make a mirror backup. Files deleted at the source will also be deleted at the destination.
/PURGE Will delete empty folders at destination. If you use /E /PURGE is effectively the same as /MIR.

Further reading

If you didn’t read my article on how to make your own backup strategy, you might like to read it here.

If you like to have a look at Microsofts own Graphical User Interfaces for Robocopy, you can read about them here.

 

37 thoughts on “How to create a backup script using Robocopy

  1. What if I have a robocopy file scheduled daily, but I will take the hard drive out. Will the process start at all, will it bring an error? What will happen when I plug the drive back in? Will it mirror all the files without a problem?

    • Hi Arek,
      This is no problem. The job will start and the commands that include the drive, will just fail and the script will continue to the next command line. I regularly unplug one of my hard drives too, and the scheduled robocopy script continues to run.
      –Jacob

    • Actually Jacob that is a great question! And the answer is maybe, but one thing I know for sure is this. Windows scheduled tasks can be configured to “Resume on restart,” so logically if the machine lost power the backup would run on the next boot. Not sure if that helps your cause, but the scheduled task would probably just fail and say so in the log.

      • Hi Sorry for not answering earlier. When the job restarts, robocopy will skip all files already synchronized. I want to restart the job on reboot, if terminated premature, that is probably possible, and for some probably essential and important. For my personal needs, once per day is good enough, and if one fails, the next day is fine enough. Thanks for your input.
        –Jacob

  2. Thought I would mention that in my experience if you use the /w:1 or /w:2 instead of /w:0 you eliminate /FFT option requirement. This may be because we are using robocopy scripts to backup Netware NSS volumes but when we use the /FFT command switch we get robocopy cannot find the specified file error.
    Just thought I would mention it.

  3. I enjoyed your well written post. Even though I have experience with robocopy, this was a good refresher and provided information I was not aware of. Armed with this info, I was able to create scripts and was backing user folders to my newly configured Linux server in a matter of minutes. It was nice to have something work “by the book”, especially after fussing around with Samba for a while to get the Windows share folders cooperating. I will definitely share this post when I create info online from my notes on file sharing between Linux and Windows.

    Erick

  4. “I really really hate command line tools, it’s sooo 80s and outdated in these smart phone ages”

    Command line tools remain extremely necessary and desirable for remote administration, interacting with embedded systems hat have no GUI, virtual machines, cloud-based systems, and a host of other applications. There are a lot of things that are still far easier to do at a command prompt. Non-technical users can easily avoid ever having to use them.

    • I agree, and exactly for that reason, I still use them, if necessary. But I still have the opinion (or ambition, being a developer too), that modern tools should be able to do things at least as well using command line tools.

    • Hi Tejinder,

      Sorry for way too long reply. I overlooked your message.

      The /mir will only copy changes. If you want and incremental backup, you should use the /m switch instead. It will only copy the files with the archive bit set (which are new or changed files). When the files have been copied, the bit will be reset, and will not get copied again.

      Hope this helps.

      –Jacob

  5. Great Post!

    Do you know if there is a way to edit the script to copy only changed files and not the entire folder structure? I’m setting up a back up system using dropbox, external drive and OneDrive, and I’m looking to create a script to copy from my resident dropbox and OneDrive files.

    Thanks, Miccal

    • Hi Miccal,

      Sorry for the way too long reply. Somehow I overlooked your message.

      When you use a mirror copy like I suggest, you only copy the changes. And on a daily basis, it is very fast.

      You can omit the /mir and use /m in instead. It will copy all files with the Archive bit switched on (new and changed files), and then it will reset the archive bit. So if you run it twice in a row, it will only copy the first time.

      If you need to copy to more destinations, use /a (it will copy all files with the archive bit set) to the first destinations, and for the last operation use /m, to reset the archive bit too.

      Hope this helps you!

      –Jacob

  6. Hello, Jacob:
    I have used robocopy in a similar way to backup several folders from servers (so I can work disconnected) or local folders to pen drives.
    What I have not been able to do is to copy image files from my phone. There is no drive letter or “\\computerName” associated with it. It appears as “This PC\phoneName” in file explorer, but this name does not work in batch files.

    Any idea?

    • Hi Simon,
      I gave it a quick look. You’re using an iPhone right? Apparantly Apple changed how the iPhone registers, and it no longer registers as a drive, and therefore Robocopy will not be able to access it. There exists third party software to turn the iPhones into a drive again, but I haven’t tried it.

      Please chip in, if any of you guys reading this post, has some additional info on this topic 🙂

      –Jacob

  7. Hi Jacob,
    Love the post. I create a .bat file for different types of robocopy tasks, here is one example. The variables makes it easy to change the source and destination with out any confusion, plus are really quick to change.

    @ECHO OFF
    SETLOCAL

    Set -source=”\\changeme-PC\sourcefolder\”
    Set -dest=”\\changeme2-PC\destinationfolder\”
    SET _what= /COPY:DAT /E /Z /M /TEE
    SET _options=/R:3 /W:3 /LOG:Transferlog.txt
    Robocopy %-source% %-dest% %_what% %_options%

    REM /E – Copy Subfolders, including Empty Subfolders
    REM /Z – Restartable

    Cheers,
    Denton

  8. If I run this command:

    @ECHO ON
    SETLOCAL
    SET SourceDir=C:\test1
    SET TargetDir=Z:\test2
    SET LogFile=C:\LogPath\Logfile.txt
    ROBOCOPY “%SourcePath%” “%TargetDir%” *.* /PURGE /S /NP /R:5 /LOG+:%Log% /TS /FP
    GOTO EOF

    How do I delete the source folder after copy/sync?

    • Hi Goulart

      You can use the Robocopy switch /MOVE to move files from the source to the destination or you can use /MOVE (notice the e) and move both files and folders. I think this is what you are looking for.

      As always – test thoroughly on test data BEFORE trying out on real data. Robocopy is fantastic at deleting stuff. Make sure you only delete what you want to delete.

      –Jacob

  9. Thak You! I came with this solution:

    @ECHO ON
    SETLOCAL
    SET SourceDir=C:\test1
    SET TargetDir=Z:\test2
    SET tempFolderName=@_Copy_in_progress_DO_NOT_USE_@
    SET LogFile=D:\LogPath\Logfile.txt
    ROBOCOPY “%SourceDir%” “%SourceDir%\%tempFolderName%” /E /MOVE /NS /NP /v /W:0 /R:0 /LOG:%LogFile% /XD “%SourceDir%\%tempFolderName%”
    sleep 5
    ROBOCOPY “%SourceDir%\%tempFolderName%” “%TargetDir%” /E /MOVE /NS /NP /v /W:0 /R:0 /LOG+:%LogFile%
    GOTO EOF

    But the folder “@_Copy_in_progress_DO_NOT_USE_@” still stay at SourceDir. 🙁

  10. Thank You! Now it is working like a charm! If someone need, there is the full script:

    @ECHO ON
    SETLOCAL
    SET SourceDir=C:\test1
    SET TargetDir=Z:\test2
    SET tempFolderName=@_Copy_in_progress_DO_NOT_USE_@
    SET LogFile=D:\LogPath\Logfile.txt
    ROBOCOPY “%SourceDir%” “%SourceDir%\%tempFolderName%” /MOVE /NS /NP /v /W:0 /R:0 /LOG:%LogFile% /XD “%SourceDir%\%tempFolderName%”
    :wait
    TIMEOUT /t 5
    ROBOCOPY “%SourceDir%\%tempFolderName%” “%TargetDir%” /MOVE /NS /NP /v /W:0 /R:0 /LOG+:%LogFile%
    GOTO :EOF

    :REPORT_ERRORLEVEL
    echo.
    if ERRORLEVEL 16 echo ***FATAL ERROR*** & goto :EOF
    if ERRORLEVEL 15 echo OKCOPY + FAIL + MISMATCHES + XTRA & goto :EOF
    if ERRORLEVEL 14 echo FAIL + MISMATCHES + XTRA & goto :EOF
    if ERRORLEVEL 13 echo OKCOPY + FAIL + MISMATCHES & goto :EOF
    if ERRORLEVEL 12 echo FAIL + MISMATCHES& goto end
    if ERRORLEVEL 11 echo OKCOPY + FAIL + XTRA & goto :EOF
    if ERRORLEVEL 10 echo FAIL + XTRA & goto :EOF
    if ERRORLEVEL 9 echo OKCOPY + FAIL & goto :EOF
    if ERRORLEVEL 8 echo FAIL & goto :EOF
    if ERRORLEVEL 7 echo OKCOPY + MISMATCHES + XTRA & goto :EOF
    if ERRORLEVEL 6 echo MISMATCHES + XTRA & goto :EOF
    if ERRORLEVEL 5 echo OKCOPY + MISMATCHES & goto :EOF
    if ERRORLEVEL 4 echo MISMATCHES & goto :EOF
    if ERRORLEVEL 3 echo OKCOPY + XTRA & goto :EOF
    if ERRORLEVEL 2 echo XTRA & goto :EOF
    if ERRORLEVEL 1 echo OKCOPY & goto :EOF
    if ERRORLEVEL 0 echo No Change & goto :EOF

  11. Nice guide, but you may want to let people know about the dangers of using MIR. If you effed up and used MIR against your on-going backup, your backup will be destroyed. Essentially you’ve created a guide for delayed manual “RAID-1”. They’re not backups, but “hardware” mirrors.

  12. All of those commands seem like an overkill. What’s wrong (if you don’t want a mirror backup–which is dangerous for networks where users delete files and then come running to the sysadmin days later that they need access to those deleted files) with something like this:

    c:\robocopy.exe c:\[directory to be backed up] e:\[target directory where the files will be backed up to] /b /e /log:e:\backup_log_file.txt

    ex: c:\robocopy.exe c:\home e:\ /b /e /log:e:\backup_log_file.txt

    Where e:\ is the external backup drive;
    c:\home is the folder to be backed up;

    ymmv (your mileage may vary). . . .

    P.S. Thanks for the info on scheduling. 🙂

  13. Hello,
    I have a little problem. I have a simple backup script:
    ———–
    robocopy C:\folder1 \\network_nas\Backup\folder1 /MIR /XA:H /W:0 /R:1 /REG /FFT >> C:\users\usernameX\Desktop\externalbackup.log
    ————-

    But in log, i see this:
    2017/12/04 17:55:06 ERROR 5 (0x00000005) Creating Destination Directory \\network_nas\Backup\folder1
    Access is denied.

    How to set credential to destination network server ? for example username, password?

  14. Hello Jacob,

    Nice article….

    I need a clarification…

    I want to schedule a robocopy scheduler job for copying updates from a different location at a specified time ( for ex everyday at 3:00AM).

    can I use the robocopy options and create the scheduler ( /RH option)?

    If Yes, Can you please send me a sample code?
    Thanks
    Jay

    • Hi Jay,
      Robocopy is “just” a program that you can launch and it will terminate when done. To relaunch it you will need a task scheduler.

      From your question I can not determine how dynamic your locations are. But if you are copying from a set of folders at various times, then I would make e.g. 4 different robocopy tasks in a scheduler, each doing a specific task.

      Otherwise, if your copying is very dynamic and folder names are based on for instance times, then you will have to launch ROBOCOPY from a PowerShell script or a batch shell. Use the script to set a variable with the location and then launch robocopy using the variable.

      In PowerShell it will look sort of like this:

      $source = YourDynamicLocation
      $destination = YourOtherDynamicLocation
      robocopy $source $destination -add-your-parameters-too

      The specifics on the PowerShell script you will need to figure out.

      –Jacob

Leave a Reply