The Macrium Reflect knowledgebase has been upgraded.
Please go here for the latest information.


v5: How to use VBScript to create a monthly backup cycle

Expand / Collapse

v5: How to use VBScript to create a monthly backup cycle

In this article, 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 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 ‘Backup Definition Files’ tab, select the ‘image of C’ example 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 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: 

ExitCode = Backup ("""C:\Program Files\Macrium\Reflect\reflect.exe"" -e -w <BACKUP_TYPE> ""C:\Users\Nick\Reflect\image of c.xml""")

And replace it with:

If Weekday(Date) = 2 AND Month(DateAdd("d", -7, Date)) <> Month(Date) Then
          ' Run the full image
          ExitCode = Backup ("""C:\Program Files\Macrium\Reflect\reflect.exe"" -e -w -full ""C:\Users\Nick\Reflect\image of c.xml""")
          ' Run the Incremental image
          ExitCode = Backup ("""C:\Program Files\Macrium\Reflect\reflect.exe"" -e -w -inc ""C:\Users\Nick\Reflect\image of c.xml""")
End If

        Note: Your XML file paths will be different to the paths above. We've replace the parameter '<BACKUP_TYPE>' with the type of backup we want to run. This could be either '-full, '-inc' or '-diff'

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 an incremental XML 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.

Note: When you schedule this script it doesn't matter whether you select 'Full', 'Incremental' or 'Differential' in the schedule dialog, it's the script that determines which type to run.

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.


1. How to run the Full backup on the 1st day of every month and increments on every other.

Change the execution condition in step 4 above to the following:

If Day(Date) = 1 Then

      ' Run the full image
       ExitCode = Backup ("""C:\Program Files\Macrium\Reflect\reflect.exe"" -e -w -full ""C:\Users\Nick\Reflect\image of c.xml""")
       ' Run the Incremental image
       ExitCode = Backup ("""C:\Program Files\Macrium\Reflect\reflect.exe"" -e -w -inc ""C:\Users\Nick\Reflect\image of c.xml""")
End If

2. How about running the full backup at a different time to the incremental backups.

For example: The full backup at 01:00 and the remaining increments at 09:00. For this you'll need to create two scheduled tasks.

1. First create create the full backup scheduled task by right clicking on the XML backup definition file and scheduling to run a 'Full' on the 1st of each month at 01:00

2. Then modify the VBScript as below and schedule it at 09:00 every day.

If Day(Date) <> 1 Then
       ' Run the Incremental image
       ExitCode = Backup ("""C:\Program Files\Macrium\Reflect\reflect.exe"" -e -w -inc ""C:\Users\Nick\Reflect\image of c.xml""")
End If

In the above example on the first day of each month nothing will happen when this VBScript is executed but the scheduled full backup will occur at 01:00. On other days of the month incremental backups will occur at the scheduled time, 09:00,

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 . Here you’ll find, not just the date functions introduced in this article, but every function available to you. 

Search terms
Last Modified:14 Mar 2013

Last Modified By:

Type: Tutorial

Article not rated yet.

Article has been viewed 17,682 times.