Deployment Solution

 View Only

Customizing the Barebones Backup Script 

Sep 11, 2008 02:59 PM

Late last week and earlier this week I posted two different Visual Basic scripts. Being someone who doesn't know much about VB scripting and knew even less when I started on this process I felt it would probably be beneficial for those like me to go over the scripts and give some examples of adding other files that may need to be backed up.

Again let me restate that these things can be done from within PC Transplant, but we are embarking on a major hardware refresh so being very specific on the files we capture and minimizing extraneous data was a priority in developing these scripts.

Modifying Barebones Backup Script

The first script I posted was listed as a "Barebones Backup Script" and can be downloaded from this article. Initially it was set to backup only two different items, Favorites, and the NK2 file from Outlook (a user's frequent contacts). We've found these are generally the two items that ease a transition to a new computer the most. However maybe you want to add another folder or file to backup. Here's how we'd need to do just that.

Backing Up Another File

Define your location

In our example we're going to add an additional file to backup for our document management product. First step is to define a variable for our subfolder where the data is stored. This product is called Document Locator so we'll follow our rough naming convention and call the variable strDocLocator, the str in this case denotes a string and DocLocator is just single word abbreviation for the product.

This variable will be placed in the first section of our script with the other Global Variables. I've placed it between our strNK2Folder and our strDocSettings string so that our strings now look like this:

strDestbase = "\\server\share\backups\" 
strDestfolder = ""
strSubname = ""
strComputerName = ""
strNK2Folder = "\Application Data\Microsoft\Outlook\"
strDocLocator = "\Application Data\ColumbiaSoft\"
strDocSettings = "C:\Documents and Settings\"

You will note that the whole path is not defined instead we have got a subpath beneath Documents and Settings. That is all that is required as we have already defined the path to Documents and Settings in another string.

Create a Sub Function

Since we're largely performing the same backup process as our NK2 file we can save a lot of time by simply copying the sub function called CopyNK2. In this case the target file we are backing up has an SES extension so I have chosen to name the function CopySES.

The 2nd line of our CopyNK2 is a notation of exactly what we're attempting to backup. You'll probably want to change it to be something meaningful so you can understand it later when you ultimately have to change it, because let's be honest what software vendor doesn't change these things just to mess with us?

Our third line will also need to be modified. Here we are bringing together all of the pieces of our puzzle to form a full path for the location of this file. The last part of that line should be changed from strNK2Folder to your new string specified above.

The next change required for this specific function is down three more lines. Here we are going to change the extension or names of the files we're backing up. In our example we changed the section in quotes to be "*.ses". After completed the new sub function looks like the one below.

Sub CopySES(strBaseName)
	'Backup Document locator repository catalog
	strSrcPath = strDocSettings & strBaseName & strDocLocator
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	on error resume next
	objFSO.CopyFile strSrcPath & "*.ses" , strDestFolder 
	on error goto 0
End Sub

Add New Sub Function to List

Now that we've created our new sub function we'll want to place it within our Main sub function. Since we copied the CopyNK2 sub function without any major changes to the way the process works we are able to create a new line within that Main sub function that matches the CopyNK2 sub function. The new line goes directly beneath the one we based it from.

CopyNK2(strSubname)
CopySES(strSubname)
	

Backing Up Another Folder

Backing up another folder and its contents actually turns out to be a little bit easier than capturing just one file. In our original file we have a function defined to capture the Favorites folder for each profile. We are going to expand our usage of the script to include some data in this example. Again I want to restate that if you are looking to capture much data beyond some specific folders you might want to consider PC Transplant it can be a much more comprehensive tool in capturing settings and data for use in a migration situation.

Because our target folder is directly under the profile folder I didn't create a Variable to store the path information. Depending on your preference or perhaps your organization's defined practices you may decide to do differently. Instead for this example we are going to skip forward to the next step.

Create a Sub Function

Again we have a perfect example of the code required to backup a folder located within a user's profile. You will need to begin by copying the sub function named CopyFavorites, and creating a new one from that information. Below I have created our new sub function with the name CopyMyDocs.

The first line you will want to modify will again be the line beginning with strSrcPath. The final line on that path will be changed from "\Favorites" to our target folder of "\My Documents". Again we will drop down three lines and change the other "\Favorites" entry to our target folder, in this case "\My Documents"

Here is what our new sub function looks like once we have finished with it.

Sub CopyMyDocs(strBaseName)
	'Backup My Documents Folder
	strSrcPath = strDocSettings & strBaseName & "\My Documents"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	on error resume next
	objFSO.CopyFolder strSrcPath, strDestFolder & "\My Documents"
	on error goto 0
End Sub

Add New Sub Function to List

Now that we have this new sub function to copy our folders we will want add this one as well. Just as the last one we will file this function into the order below the function we copied it from.

CopyFavorites(strSubname)
CopyMyDocs(strBaseName)
	

Testing Your File Backup

Now you'll need save and run the updated visual basic script again. If it's not working properly and you know your source file is in the right place I found I often had a problem getting the path to build properly. By inserting a new line within your new sub function under the line that begins "strSrcPath" you can display a window that will tell you what that string contains. That line is:

WScript.echo(strSrcPath)

If it's not correct take some time to look more in depth at those variables you defined and modify them as necessary.

If you are copying data from within a profile make sure that you have put the sub functions within the "for each . . . next" section of the script and also below the "fso.createfolder(strDestfolder)" line. That way it will copy data from under each profile found on the source computer.

It is also possible that you would need to backup a folder found elsewhere on the computer. If you do so make sure your statement is outside of the "for each . . . next" section so it doesn't repeat multiple times.

If anyone has other questions or suggestions I welcome them. I am not a master at Visual Basic scripting, by any measurement, but I will do my best to help answer questions and I will whole heartedly welcome advice to help me improve the script or my documentation of the script.

Also posted earlier was a script to restore items backed up using this script. A follow up article to describe how to modify the restored files script will follow in the near future. In the meantime that script can be found here.

Statistics
0 Favorited
0 Views
0 Files
0 Shares
0 Downloads

Tags and Keywords

Related Entries and Links

No Related Resource entered.