Custom Backup Tool with Robocopy

Creating backups is an essential task people take for granted. I have lost count the amount of times people have said they lost all their data and I ask ‘did you have a backup?’…. Erm no.

So windows vista does come with its own backup tool, but it is somewhat primitive in features. However if you know a little bit about windows command line and scheduled tasks then ‘robocopy‘ is what you need.

Here is my command I run to back up all my personal data.

[sourcecode language=”sh”]
robocopy F:\Alex’s Z:\BACKUP\Alex’s /MIR /XA:SH /R:5 /V /NP /ETA /LOG:Backup_alexs.log
[/sourcecode]

First of all you choose what folder you want to backup, in this case its ‘F:\Alex’s‘ then you choose where you want to back it up to. In my case I have a mapped network drive to a Linux Samba file share on another computer ‘Z:\BACKUP\Alex’s

Note that ‘robocopy‘ copies the contents of the folder and does not include the top folder name. So you have to write ‘Alex’s‘ again in your backup location in order to copy the exact file structure or you will just get the contents of ‘Alex’s‘ spat out into the ‘BACKUP‘ folder.

Lets go through the parameters I set with my backup command.

  • /MIR – This mirrors the backup, so if you delete a folder it will also delete it on the backup on its next run. This is essential or your backup location will just become a mess of folders as time goes on and will make it very easy to find what you are looking for.
  • /XA:SH – This excludes the hidden system files.
  • /R:5 – Now, one of Robocopy’s features is that if it encounters a file that is in use, it will stop and wait for that file to be closed so that it can continue with the copy operation. It will retry to copy the file ever 30 seconds. The default number of retries is 1 million (no joke!). As this will most likely prevent the backup operation from ever completing, we set it to try only 5 times.
  • /W:15 – This changes the default wait time between retries from 30 seconds to just 15 seconds.
  • /V – This gives a verbose output of what robocopy is up to during the backup.
  • /NP – This will not show the percentage progress of each file in the output as I don’t really need to know that.
  • /ETA – This shows an Estimated Time of Arrival of copied files in the output.
  • /LOG:Backup_alexs.log – Finally I like to keep a log of how the backup went as I will not be staring at the command line during the backup.

Now you can put this command into a text file and save it as ‘backup_alex’s.cmd‘ All you have to do now is schedule when you want your backup to run using the Task Scheduler in windows.

I run my backup script every day at 20:00