How to backup a folder with Robocopy

HarddriveAs a simple way to keep my computer backed up I copy all that I care about onto external hard drives twice a day. I mostly do this in case my internal hard drives would die or something like that, so I haven’t cared too much about finding anything more fancy with incremental history and such. What I have is a simple batch file that I have set to run periodically using the Task Scheduler in Windows.

Note: If you want an easier solution which supports incremental backups to external drives, other computers and the cloud, you should check out CrashPlan. I personally pay for a CrashPlan+ Family Unlimited license and use it to back up all my computers. Very handy and has already prevented me from losing all my stuff once 🙂

The batch file simply looks like this:

@echo off

robocopy D:\ X:\D /COPY:DAT /MIR /B /XJ /R:0 /NP /LOG:"X:\D.log"
attrib -S -H X:\D

You can duplicate those two last lines for other folders you want to back up as well. I have one copying my D drive to external drive X, one copying my E drive to another external drive Y, and one copying some folders on my C drive to external drive Y.


Robocopy is of course the command that does the backing up. The options I use here are as follows:

  1. Source (directory we want to back up)
  2. Destination (where we want the backup to be stored)
  3. Copy Data, Attributes and Timestamps
  4. Mirror (Copy all subdirectories, even empty ones, and delete files no longer in the source directory)
  5. Copy files in Backup mode (assert the Windows NT “backup right” so an administrator may copy an entire directory, including files denied readability to the administrator)
  6. Exclude Junction points
  7. Retry zero times on failed copies (prevents spending lots of time on locked files for example)
  8. Don’t display any progress
  9. Log what happens to the given file instead of printing it out in the console

Don’t get stuck in a loop!

The robocopy switch, /XJ, is very important to remember if you copy for example your user directory. The reason is that some folders in Windows are something called junction points and some of these will cause robocopy to enter a never ending loop. The copying will basically never finish until the target device run out of space or something else happens. Happened to me. Not very fun to clean up 😛


In my case I am copying a whole drive, and the attributes on the resulting folder is a bit weird. So, to have the folder show up in Explorer without having to make it show system files and hidden files, I remove the system- and hidden-attribute from the folder.

How to run it

You can either run this yourself in a Command Prompt or schedule it using the Task Scheduler. What is important to remember is that you should run this using Administrator rights.

So, if you use a Command Prompt: Remember to use an elevated Command Prompt. And if you use the Task Scheduler, remember to select that the task should Run with highest privileges in the task properties.

If you just want to run it in the command prompt and see the progress a bit easier, you can remove the /NP and /LOG options. You will then see what happens as it goes through your files 🙂

  • Vegar

    ‘…and delete files no longer in the source directory.’ – So if you loose a file by accident early in the morning, but you ain’t aware until later that day, the file is gone?

    • Yep, except I would usually find it in the recycle bin if that happens. And besides, I don’t really delete files that often. The files that I work actively with are source files which are source controlled.

  • Eirik

    You could also use or something similar. That takes care of the lost file problem that Vegar mentioned.

    • Vegar

      If you consider Mozy, you should also consider CrashPlan. Its the only ‘online backup’-package that also gives you local backup. It also makes it possible to backup to friends instead of paid storage at CrashPlan Center. If you want online storage, Mozy gives you some space for free, but I think CrashPlan is a little cheaper if you exceed the initial 2 GB of storage.

      I use CrashPlan to backup my laptop to my WHS. I thinknNext version of WHS will backup macs, making CrashPlan redundant. I do not use its online storage facility though. I plan to use it for backing up the server eventually, but I haven’t got to it yet….

      • Yeah, have looked a bit on an online solution. Problem is that my uplink would be stuffed for the next half year or so. And also it costs money to back up that much, and I haven’t had much of that coming in.

        I have my documents “backed up” through Dropbox though. And they also supports versions, so if I delete a document, I can go online and get it back.

        Will probably look at options like that again later though, when/if I get a more stable and higher income and a better internet connection 🙂

        • Eirik

          I guess online backup is not a good solution if you don’t have much upload bandwith like I do. But for me it works very well. And I don’t consider 4,95$/month for unlimited storage to be very expensive either.
          What you (Vegar) say about Mozy not having local backup is no longer true. With v.2.0 it now has the ability to backup to an external disk as well.
          But after looking at CrashPlan, I have to admit that their product looks better, and it is cheaper as well. I didn’t know about them back when I purchased Mozy, I’m not sure they even existed back then (I think it was a couple of years ago).
          Ah well, I might have to reconsider what to use the next time Mozy is up for renewal. Thanks for the tip.

          • Vegar

            Then we both learned something new 🙂

        • Vegar

          Come visit for a week, and you can clog my uplink for free 🙂

          CrashPlan supports initial backup through mail, but that too cost money.. But as I said, CrashPlan is great because it also supports local backup, and friendship backups as well. You could upload to my server, and I could upload to yours.

          What I don’t like about CrashPlans local backup, though, is that you need their software to restore files. A couple of files, like photos and music, I would like to mirror between local and server, but that’s not possible through CrashPlan. RoboCopy would be a better solution there. I guess rsync would be a option for mac users.

  • Jorge

    Thanks for the instructions. They have been really helpful for copying files from an old hard disk. Any other option I used was leaving behind system and hidden files, permissions, attributes and also denying me to copy everything.

  • Ben Wong

    You can use this Robocopy command to backup data with incremental backup mode.

    robocopy %source% %dest% /S /COPYALL /NP /TEE /R:3 /W:3 /Log:c:Documentslogs.txt

    I use this command to backup data in my organization with incrmental mode. It work perfectly.

    If you want see more detailed, you can read this post:

    • It won’t be incremental in other ways than that it won’t re-copy everything though, right? Like, you won’t get incremental history diff type stuff.

      • Ben Wong

        Yes, It just copy data that have changed since the last backup.