Computer Tips and Tricks
Get the latest posts via rss

Wednesday 10 March 2010

Simple File backup with Robocopy

One of the main issues when using any computer is regular backup of your precious data to protect against corruption and accidental deletion. Microsoft has always provided their "Windows Backup" utility and it is generally very good for full system backups and restores.

But what if you want to backup just your personal data or certain files or folders and not others? What if you just want to restore one particular file? There are plenty of third party tools that will do the job, but some can be rather expensive. Besides, why buy what you already have.

Supplied in Windows Vista, 7, Server 2008 and Server 2008 R2, as well as the previous resource kits is a utility called "Robocopy." This is a small command line based tool (a bit like XCopy) that can be scripted and configured with exceptions, to copy selective data either once or on a schedule and even write to a comprehensive log file.



What I wanted to do was backup a large directory on the C: Drive of my laptop to a hidden share on another PC (both of which only I have access to using NTFS permissions, with Full Control.) There was one particular folder in that directory that I did NOT want copied. Also I wanted to ensure my backup data was kept in sync (with roughly a 2 hour difference) without having to run the commands/script manually everytime.

I started first by configuring the file copy. I am using Windows 7 Enterprise and the Robocopy utility is located in c:\windows\system32.

I created a folder called "robocopy" to store my scripts and started with the following:
----------------------------------------------------------------------------------
@echo off
cls
c:
cd\
cd\windows\system32 
robocopy.exe "[source]" "[destination]" "*.*" /E /PURGE /XO /R:0 /W:0 /COPYALL
----------------------------------------------------------------------------------

The /R:0 and /W:0 tells the script to retry the copy if it fails on a file. The /W:0 is the wait time and the /R:0 is the number of retrys. I set this to 0 so that that hopefully the files will be copied on the next run.

This copied my data to the shared folder. /E copied any subdirectories and files and /PURGE would remove from the backup any deleted files or folders.

I then found /E and /PURGE could be replaced with /MIR (mirror) which does the same thing.

Next I needed to add a exception to prevent a folder from being copied I used the following command:

/XD dirs "[Folder Path to be excluded]"

If I had wanted to exclude certain files I could have used:

/XF file "[File Name and Path to be excluded]" Note: You can use wildcards with this switch.

I then added on a log file using:

/LOG+:"[location\backup.log]"

This log I created manually (blank) because I used the /LOG+ switch. This appends to the LOG file rather than creates a new log every time. NOTE: When copying a large amount of files and/or making alot of changes, this log will become rather large. It may be wise to clear this out once in a while. Or if you know the script is working, disable it completely.

Now to sort the scheduling. I wanted my script to backup any changed data every two hours. This would make sure my data is up to date and leave me time to copy back older versions of files if needed.

Just before the log command I used the following:

MOT:[SYNC TIME] (in minutes, I used 120)

MOT:m = Monitor source; run again in m minutes Time, if changed.

There is another switch /MON which launches the backup after so many changes have been made to the data to be backed up. I decided against this, but it is down to personal preference.

MON:[Number of changes]

My final script looked something like this:
----------------------------------------------------------------------------------
@echo off
cls
c:
cd\
cd\windows\system32
robocopy.exe "[source]" "[destination]" "*.*" /XD dirs "[to be excluded]" /E /PURGE /XO /R:0 /W:0 /COPYALL /MOT:[SYNC TIME] /LOG+:"[location\backup.log]"
----------------------------------------------------------------------------------

Now once this script is running it sits dormant in the background, and copies the changes to my data on my laptop to my shared folder every 2 hours, and writes the changes to a log file on my laptop.

However it DOES have to be launched manually to set it going. Easily solved with a scheduled task. I set mine to launch 15 minutes after "System Startup." I then exported the Scheduled Task to an XML File for backup and future use.

I also set this task to run with a different account, as you need to have the "Back Up Files and Directories" right to run Robocopy and the "Manage Auditing and Security Log" user right to run Robocopy with the /MIR switch. This is set in the local security policy. I am on a corporate network, so this was set via group policy.

In the local security policy these rights can be set in:

Security Setting --> Local Policies --> User Rights Assignment

Now you can use the command you have just created and call that from a batch file using the scheduled task. Or you can be a bit clever and create a Robocopy JOB file. This a configuration file created by Robocopy that contains all of your JOB settings. In your batch file all you do is call the JOB file. It is more efficient and the JOB file can be edited and reconfigured at any time to meet your needs.

To create the job file add: /SAVE:[location\jobname] to the end of your robocopy command line. This will generate a RCJ file. You only need to run this once.

Once created, to call the RCJ file in a batch file use: robocopy /JOB:[location\job name] instead of the long command we created above.

NOTE: Running this JOB through a scheduled task will make Robocopy run it's backup invisible, in the background without any user interaction.

The same can be used to backup to an External USB Drive. However, if you want the scheduling to work, you will need to keep the USB Drive plugged in all of the time that you have Robocopy running.

I suggest you test this on some fake files and folders before you go live. If there is a large amount of data to copy, you may wish to make the first run "out of hours." After the first copy, only new files/folders and changes will be copied.

For a full list of Robocopy commands I found them here.
There is further information on Microsoft Technet.

Robocopy can also be configured with a GUI. Find out more here.

0 comments:

Post a Comment