One thing I promised myself was at some point (early on) I would create an automated backup solution for my home computers. I wanted an automated and easy to configure system that I was confident of backing up my most important files. The criteria for this operation were the following:
-
Must use a well known and free backup command-line tool that I could script out exactly how and what I wanted it to do
-
Must be capable of being run from a scripting language (preferably VBScript).
-
Should be able to copy both large files and large quantities of files that might crash less robust tools
-
Have the ability to copy from Windows’ default hidden shares like “C$\” and UNC paths like “\\Foo”
A friend of mine had mentioned a long-time windows utility called Robocopy (he also provided some of the VBScript that I used). This command line tool was designed by Microsoft for reliable mirroring of directories and directory trees. You can check out the wikipedia article for Robocopy if you are so inclined.
Robocopy meets all of the requirements mentioned above.
After setting up the computers on the workgroup with the required permissions and settings, the next step was to create a script file that would for now, manually backup files (I’ll cover an automated solution as well as a database configuration and logging solution in future articles).
I wanted to have a process that would eventually create multiple copies of all the files required for backup. There would be a separate copy of each file for each week of the month. This means I would only get a fresh backup of files once every week. For the types of stuff I wanted to backup (family pictures, music and movie files, code and design files) I felt this was adequate. When viewing the backup destination, I would see a maximum of 6 folders (since there could be 6 different weeks in a month) with names like Week1, Week2, Week3, etc. The directories and files would all be inside these folders. Here’s a copy of what that backup destination structure could look like:

For a script and Robocopy to do this, it should have the following steps:
-
Setup of global parameters including a collection of machine\directory paths for configuration
-
A process of adding the path information to the destination area including machine name and share name
-
From a script, Robocopy will only be provided one source and one destination at a time so there will be a primary loop in the script that loops through all specified source paths
-
A process to capture and write the results from the process so it may be reviewed at a later date.
-
Functions to determine the week of the month, attempt to create the week folder, and create a name for the results file
-
A method of executing the Robocopy tool with the proper commands and capturing the information it provides while executing. This is the key to the whole process!
Robocopy requires a command set like the following example:
“Robocopy \\foo\c$\documents D:\Backups\Week1 /S /NP /R:2 /W:5”
This will tell Robocopy to attempt to connect to the hidden share “c$” on machine “foo”, and copy everything under the directory “documents” to the backup destination “d:\Backups\Week1”. The stuff behind the destination are known as switches. The “/S” copies all subdirectories unless they are empty. The “/NP” tells Robocopy to not provide any progress information while copying files. The “/R” is tells the tool to retry 2 times before giving up when there is an error. Finally, the “/W” provides the time to wait before retrying, in this case 5 seconds. There are many other switches, but I’ll leave it to you to research all the unique ways Robocopy can be manipulated.
I won’t go into all of the code, but here is a chunk of code for specifying what source paths there are:
Dim array_Sources(3) ‘Source Paths Array
Dim array_Destinations(3) ‘Destination Paths Array
array_Sources(1) = “\\octavious\e$\Sierra”
array_Sources(2) = “\\spartan\c$\logs\”
array_Sources(3) = “\\Minimac\Lisa\”
As you can see one of the machines is a Mac Mini, which with SMB and the proper account settings, can be backed up as well.
Here’s the key to the whole VBScript and Robocopy marriage to get this system to work:
Set oWSH = WScript.CreateObject(”WScript.Shell”)
Set oCOMMAND = oWSH.Exec(sCommand)
Where sCommand is basically a string containing the Robocopy command line for that specific source and destination set as shown above.
You can probably find a copy of Robocopy somewhere on Microsoft’s site if you dig around a little.
There’s also a GUI for Robocopy that someone at Microsoft built along with all the files and documentation.
Next I’ll be looking at providing some hardware to this solution so I can easily switch out my backup media with another. Remember, this is only the first step in the overall solution…Eventually we’ll have a database tied to this so we can dynamically assign backup sources and log backup metrics for reporting and monitoring of the backup process.