19 Jan 2015

Robocopy - backup and file synchronisation PART 1

For Windows there are many options for backing up or synchronising files between folders. One of them is a nice little program called FreeFileSync that has a simple user interface but recently I've been using Robocopy.

Robocopy comes as part of Windows (Vista, 7, 8 and other versions). There's no graphical user interface but that's fine because you can easily automate your backup or file sync. In this article I'll explain how to use Robocopy in a simple way to backup files from your 'Documents' folder to another drive, perhaps a USB flash drive.


Command Line
The Robocopy command is like this:
ROBOCOPY <source> <destination> [options]
You can find full details on Microsoft's Technet:
http://technet.microsoft.com/en-us/library/cc733145.aspx



Example 1 - a simple start
Let's say you want to backup the Documents folder from your C: drive to a USB flash drive - here's what to do. When you insert the USB flash drive it'll be given a drive letter. Below in our example it is drive K: but yours might be something else.

Windows 7 - click Start | CMD [Enter]
Windows 8.1 - right click | Command Prompt

At the command prompt type the following:
ROBOCOPY "c:\users\michael\documents" "K:\backup" /MIR [Enter]
Replace michael with your username.
Replace K: with the letter of the USB flash drive you have plugged in.

The /MIR mirrors the files from the documents folder to the destination (K:\backup) folder. By 'mirror' it means to copy the files. The next time you run the same command line it'll copy any changes from the source to the destination. If you delete a file in the source then the same file will be deleted in the destination folder. This is folder synchronisation!


Example 2 - automate the above
Typing the above command is a bit of a pain, isn't it!?! Every time you want to backup your documents you have to remember all of that? There must be an easier way. The answer is to put this command line into a file that's called a command file (or batch file). You can double click the file to execute it.

We will create the command file on the USB flash drive, when you wish to run it, even if the USB flash drive has a different drive letter it'll still run, to do this the command line will be slightly different - here's what to do:

Open Windows Explorer and click the USB flash drive.

Create a new text file (open Notepad).

In Notepad enter the following single line:
ROBOCOPY %homedrive%%homepath%\documents \backup /MIR /LOG:\RoboLog.txt /TEE /R:0 /XF desktop.ini

Save the file as Robo.cmd on the USB flash drive.

Double click Robo.cmd, it'll copy all the files from documents to a folder called Backup on your USB flash drive.


Example 2 - explanation
Here I've replaced C:\Users\michael\documents with %homedrive%%homepath%\documents - this means I can use my command file on any computer (tested on Windows 7 but should work on other versions). There are two standard Windows variables:
  1. homedrive
  2. homepath
To tell the computer these are not just part of the folder/path name, add the percent signs % at the beginning and end of each variable. Add \documents on the end to complete it.

/LOG creates a log file called RoboLog.txt. Once the backup has finished you can open the log file in Notepad and see exactly what has happened, which files were updated, which were deleted, etc.

The /TEE parameter means the log file is updated and the results are also shown on the screen, in the command window.

The /R:0 is to set the retries to zero. This means if there is a problem with reading a file it'll move past it to the next file immediately. If you want to use the retry function then set it to /R:1 for one try or /R:2 for two, etc. For most people /R:0 makes sense though, using the multiple retries is mostly useful for reading files from network drives when files maybe shared.

The /XF means to exclude a file. In my example above I set it to exclude the desktop.ini file. The reason is that with this file present in the Documents folder, your 'backup' folder on the USB flash drive will appear as "My Documents". You can also use /XF to exclude other files you may not need in the backup.


Example 2 - finishing touch
Its' a good idea to add a pause command at the end of your command file. This will stop the command window from disappearing after Robocopy has finished. It's good to give you some on-screen feedback that it's finished. Your command file would have two lines:
ROBOCOPY %homedrive%%homepath%\documents \backup /MIR /LOG:\RoboLog.txt /TEE /R:0 /XF desktop.ini
PAUSE


Conclusion
Robocopy is a fast command line program. You can create a simple command file and run it whenever you need to backup. You could also schedule the command file to run at a predetermined time.

Because Robocopy only copies files, it doesn't compress or encrypt them, it is simple to restore them. This might be a limitation in that you may be worried about data security. On the other hand there are many times when a simple file copy makes more sense, fewer worries about your backup files being corrupted when you come to restore them.

Robocopy is reliable and easy to use. It's a good replacement for the old COPY and XCOPY commands. The folder synchronisation is such a welcome feature, it speeds up backups. Robocopy is also part of Windows so you know it's there - this makes it an ideal tool for those working in IT who don't want to use a third-party tool. In my experience using Robocopy I've not had any problem, it's a great little program, I recommend it.

PART 2 - I've written a second article that includes an example of an AutoIt script that, using Robocopy, will create a multi-copy backup. It's the next step, to make your backups a little more sophisticated.
http://mgxp.blogspot.ch/2015/08/robocopy-backup-and-file.html


Reference
Robocopy at Microsoft Technet:
http://technet.microsoft.com/en-us/library/cc733145.aspx [last accessed 19/01/2015]

Robocopy at Wikipedia:
http://en.wikipedia.org/wiki/Robocopy [last accessed 19/01/2015]


No comments: