Disk Imaging and File Backup for Windows 7, Vista, XP and Server 2003/2008 RSS 2.0
# Tuesday, June 10, 2008

How to use VBScript with Macrium Reflect


In part four of our series on working with Macrium Reflect, we look at using VBScript to handle a monthly backup cycle. I’ll show you how to generate a template VBScript source file and modify a couple of lines to enable selective execution of the XML definition files created in the previous tutorials.


So far we’ve created full and incremental image definitions and scheduled these to run in a weekly backup cycle. We’ve also used the disk space management option to ensure that our backup disk doesn’t overflow. But, what if you want to schedule your images using a monthly calendar cycle? Say you want to run a full image on the first Monday of every month and an incremental image for all other weekdays. How do you do that?

Macrium Reflect uses the standard Windows Scheduler to schedule all backups and images. This has the advantage of being a very well tried and tested scheduler, plus it reduces valuable program overhead by using an existing, running service. However, while the Windows Scheduler is great for repeating weekly pattern of days, it can’t handle monthly exclusions very well. In our above example we can schedule the full image to run on the first Monday of each month without a problem, but the incremental images would have to be scheduled for every weekday. This means that on the first Monday of each month you’d run both the full and incremental images. This isn’t a very elegant solution; so what do we do?

The answer is VBScript. With VBScript we have total flexibility to do anything we want. We can also reduce the number of tasks to schedule to just one.

If you’re unfamiliar with VBScript source, it can look rather daunting. The syntax seems unnecessarily complex and verbose. However, one thing to remember is that in most cases you can just copy existing code examples and modify them slightly to make them work for you. Macrium Reflect generates a great working template for you to modify, and I’ll show you a couple of simple code lines to add to achieve the scheduling scenario I’ve outlined above.

OK, let’s begin…

  1. Start Reflect, click the ‘XML Definitions’ tab, select the ‘C Full Image’ XML file and then click the ‘Generate VBScript’ button.



  2. The VBScript generation dialog is shown. Modify the VBScript file name to ‘Monthly Schedule’, and then click OK.



    This will generate a VBScript template file using the default options. The defaults will simply run the full image and create a log file in the directory where the XML definition is stored. A VBScript source file can be scheduled in exactly the same way as an XML file, but first we’re going to modify it to run both the full and incremental images on the correct days.

  3. Click the ‘VBScript files’ tab. You should see your new VBScript source file in the list of source files. If you select it with your left mouse you can see the automatically generated code in the lower half of the window. 



    This view has the advantage of syntax highlighting. This means that keywords, conditions and certain variables are displayed different colors, making viewing the source (and potential errors) much easier.

  4. Right click on the VBScript file and select ‘Edit’.



    This will open the source file in the default editor, Notepad.exe. Make sure that Word Wrap is turned off. This makes editing the source code much easier.



    Locate the following line of code:



    And replace it with:



    Note: Your XML file paths will be different to the paths above

    A quick summary of the functions used above:

    The Date keyword is the current system date.

    The Weekday function returns the day of the week as a number. 1 = Sunday, 2 = Monday etc.

    DateAdd simply subtracts 7 days from the current date. The “d” means that we are subtracting (or adding) days. The other possibilities are, “yyyy” - Year, “q” – Quarter, “m” – Month, “y” - Day of year, “w” – Weekday, “ww” - Week of year, “h” – Hour, “n” – Minute, “s” – Second.

    The Month function returns the month (1-12) of the DateAdd result and compares this with the current month using the ‘<>’ not equal to operator.



    So, if the current day is Monday and the Monday of last week is a different month, then this must be the first Monday of the current month. As you can see, the code then simply executes the XML definition file for the full image otherwise the incremental XML definition is executed.

    When you’ve made the changes, save the file and close Notepad.

  5. All that remains is to schedule the VBScript file to run every weekday.

    Right click on the VBScript file in the list and select ‘Schedule’. You’ve been through this wizard before, so I won’t go over it again. You could of course, select only weekdays or every day, but the important thing to understand is that whatever you choose, it’s the VBScript code that decides whether to run the full or incremental image not the scheduler.

You’ve done it! VBScript code that modifies the running order of your images. VBScript gives you complete flexibility for any situation you can think of.

This is just a very small example of the power of VBScript. If you want to delve further into VBScript, have a look at the options in the generator. It generates separate functions for each option and provides some handy comments to help you understand the code. An excellent reference of VBScript functions can be found at http://www.w3schools.com/VBscript/vbscript_ref_functions.asp . Here you’ll find, not just the date functions introduced in this article, but every function available to you.

In the next tutorial we’ll imagine that the worst has happened and your PC has been infected with a killer virus. Windows won’t start! How do you get your system back? Don’t panic, I’ll reveal all.


For more information about Macrium Reflect visit http://www.macrium.com.

Admin  Tuesday, June 10, 2008 12:32:55 PM (GMT Standard Time, UTC+00:00)  #    Comments [6] -
Tutorials
Monday, September 08, 2008 2:10:21 PM (GMT Standard Time, UTC+00:00)
This is pretty neat capability.

1. Could this be used to select a different destination if the default one is full? So e.g. the default could be "\\server\backups" and if that fails, try again on "\\server\backups2" (assuming the latter is on a separate partition), if that fails, try again on "\\server\backups3" etc.

2. Any chance you could support Python as scripting language? I would guess much more popular, certainly way cleaner syntax, and probably far better tools support. You could probably even just allow user to run the interpreter already installed on their system, no need to embed in your product, you would just have to export some of your DLL's so the Python interpreter could load them (gosh then you woulnd't even need the GUI for some batch tasks... just start the interpreter and import macrium.reflect ;)

3. How do you test the script for validity? E.g. in item 1 above, you would need to make Reflect believe that 1st and 2nd partitions are full to really test all branches. In Python, test stubs are easy to write: you would redefine the Backup() function to return true only on the third call, so you could have one line of code in the script that you set to either True or False to determine whether to rebind Backup() and you're in business! Is it equally easy in VBScript?

Thanks for a great product!
Monday, September 08, 2008 4:34:57 PM (GMT Standard Time, UTC+00:00)
Hi Oliver

Could this be used to select a different destination if the default one is full? So e.g. the default could be "\\server\backups" and if that fails, try again on "\\server\backups2" (assuming the latter is on a separate partition), if that fails, try again on "\\server\backups3" etc. ?


You would need to selectively test the free space on each destination and call the appropriate backup XML file dependant on the result.

Any chance you could support Python as scripting language?

It already does. Reflect uses Windows Scripting Host (WSH) as the interpreter for VBScript. As far as I'm aware WSH supports Python (or is it Python supports WSH). VBScript is installed by default on all supported operating systems (XP through to Windows Server 2008). Because of this it is the only language that we will generate templates for, but you can of course use any language supported by WSH.

How do you test the script for validity? E.g. in item 1 above, you would need to make Reflect believe that 1st and 2nd partitions are full to really test all branches. In Python, test stubs are easy to write: you would redefine the Backup() function to return true only on the third call, so you could have one line of code in the script that you set to either True or False to determine whether to rebind Backup() and you're in business! Is it equally easy in VBScript?

I think you'll find VBScript to be as flexible as Python in this regard. Both scripting language allow you to return true or false.

Hope this helps
Nick
Monday, September 15, 2008 5:05:26 AM (GMT Standard Time, UTC+00:00)
Nick,
Thanks for the reply.

Re Python via WSH: I'm not familiar with WSH. Google search results indicate that indeed the Python interpreter can be registered with WSH, and that any .pys file will be run via WSH. However, I'm not sure how that fits in to Reflect: how would Reflect know that the code in the VBScript panel is Python rather than VBScript? Or do you mean that I would create a .pys on file system, which would end up accessing Reflect through a COM interface (which I'm also only somewhat familiar with)?

Oliver
Thursday, September 18, 2008 4:31:03 PM (GMT Standard Time, UTC+00:00)
Hi Oliver

All the backup functionality of Reflect is accessed via the command line, passing an XML backup definition file as a parameter. This can be done from any scripting language that can access the Windows command line. If you generate a VBScript file within Reflect and look at the backup function you will see what I mean. WSH has complete control and calls reflect.exe to execute backups and images.
Nick
Sunday, November 02, 2008 5:55:24 PM (GMT Standard Time, UTC+00:00)
Hi Annette,

Thanks for all the scripting examples.
I'm an analyst/programmer myself, but not on Windows platform.
My platform is 15, iSeries, (AS/400). I know, I know. :)

I'm about to download the 30 day trial version.
I assume that there is an opposite to the backup() function, called restore().
If so, where do I find a "Reflect function reference" ?
Is it in the download file maybe ?

During the trial period, I would like to attempt scripting the following :
1. backup a partition (input or select a drive letter)
2. verify the backup file
3. restore the backup file to a dedicated partition specifically created for testing the ability to restore the backup
Step 3 only if step 1 and 2 were completed successfully.
(You may call me paranoid, but being able to actually restore from the backup image is the best verification.)

Thanks,
Robert
Robertino (InToGraphics)
Monday, November 03, 2008 8:27:27 AM (GMT Standard Time, UTC+00:00)
Hi Robert

Scripting is only available for backup. This is because restore is generally a one-off activity that involves using the rescue CD to boot your PC.

All functions/subroutines are generated using the VBScript generator dialog and are clearly named and commented. For a full description of all available VBScript commands have a look here: http://www.w3schools.com/VBscript/vbscript_ref_functions.asp.

Is the AS/400 still alive? I go way back to the S/36 and RPG II and I was an AS/400 consultant for many years using SYNON II/COOL 2E.

Kind regards
Nick
Comments are closed.
Categories
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Sign In
© Copyright 2010
Paramount Software UK Ltd
Sign In
Statistics
Total Posts: 26
This Year: 0
This Month: 0
This Week: 0
Comments: 143
All Content © 2010, Paramount Software UK Ltd
DasBlog theme 'Business' created by Christoph De Baene (delarou)