John Howard - Senior Program Manager in the Hyper-V team at Microsoft

Senior Program Manager, Hyper-V team, Windows Core Operating System Division.

Blogs

Generic Exchange Backup Script (Backing up Exchange 2003 Part 2)

  • Comments 25
  • Likes

Update: 4th Nov - See updated and improved script here

As part of my backup for Exchange I was playing with at the weekend, I ended up writing a pretty simple script to run as a scheduled task. Although my Exchange environment is  simple as well, the requirements and strategy I wanted were:

- Weekly full backup
- Differential daily backups (incrementals apparently don't work so well, according to my research)
- Robust and reliable (obviously)
- Free!!!

Optionally, I wanted it to notify me if the backup failed (through SMS or email), but I'll add that in another day - should be pretty simple.

The basic problem with just running NTBackup on its own is that it's impossible to get it to set the job names and output filename depending on the date. Hence, here's what I ended up with. A single simple VBScript that runs daily as a scheduled task as a delegated Exchange administrator. It has a bit of resilience in it for error checking, such as picking up whether the destination file already exists to determine whether to append or overwrite. It also gracefully handles object creation failure.

As for support - you're pretty much on your own. It works for me very nicely, and I've used the output files to test a restore using the fantastic new feature of Exchange 2003 in the Recovery Storage Group (RSG). If you want to find out more about that, have a look at Ewan's blogcast here.

' ************************************************************************************
' * Weekly Backup Script for Exchange
' * John Howard, Microsoft UK. Created 25th June 2005
' *
' * Feel free to use/modify for your own needs.
' * No guarantees though although it works for me :-)
' * However, if you can do better, contact me through
http://blogs.technet.com/jhoward
' ************************************************************************************

Option Explicit
On error resume next
Const NO_ERROR = 0
Const BACKUP_SHARE     = "
\\RemoteServer\ExchangeBackups"
Const BACKUP_SELECTION = "Exchange Backup Selection.bks"
Const BACKUP_PROGRAM   = "c:\windows\system32\ntbackup.exe "

Dim szYYWW                    ' Date in YYYY-WW format (Week of year)
Dim szYYMMDD                  ' Date in YYYY-MM-DD format
Dim szFlagsSelection          ' The backup selection script, prepopulated
Dim szSetDescription          ' The description of the backup set
Dim szDestinationFile         ' The destination file in the destination directory
Dim szFlagsJobName            ' Flags for the name of the job  [/j "jobname")
Dim szFlagsVerify             ' Flags for verify the backup    [yes|no]
Dim szFlagsRemoteStorage      ' Flags for remote storage       [/rs:no|yes]
Dim szFlagsHardwareCompress   ' Flags for hardware compression [/hc:off|on]
Dim szFlagsLogging            ' Flags for logging in ntbackup  [/l:f|s|n] Full SUmmary None
Dim szFlagsAppend             ' Flags for appending data       [/a] or nothing
Dim szFlagsRestrict           ' Flags for restricting access   [/r:yes|no]
Dim szFlagsType               ' Flags for backup type          [/m normal|Incremental|Differential...]
Dim oFSO                      ' File System Object to see if file already exists
Dim owShell                   ' To execute a shell command
Dim rc                        ' Return code
Dim szError                   ' If we have an error, record it in here
Dim szCommandLine             ' What we are going to run as a backup

Set oFSO        = Nothing
set owShell     = Nothing
rc              = NO_ERROR  ' OK So far
szszCommandLine = ""        ' Not sure what we're running yet
szError         = ""        ' Not had an error yet

' Setup our variables
if (NO_ERROR = rc) Then
    szYYWW                   = year(now()) & " w" & formatNumber(DatePart("WW",now()))
    szYYMMDD                 = year(now()) & "-" & formatNumber(month(now())) & "-" & formatNumber(day(now()))
    szFlagsSelection         = chr(34) & "@" & BACKUP_SHARE & "\" & BACKUP_SELECTION & chr(34)
    szFlagsJobName           = "/j " & chr(34) & "Exchange Backup" & chr(34)
    szFlagsVerify            = "/v:yes"        ' Verify YES|NO
    szFlagsRemoteStorage     = "/rs:no"
    szFlagsHardwareCompress  = "/hc:off"       ' Hardware compression off - this is to disk
    szFlagsLogging           = "/l:f"          ' f=full s=summary n=none
    szFlagsAppend            = "/a"            ' /a for Append or leave blank to overwrite
    szFlagsRestrict          = "/r:no"         ' no|yes Restrict access to administrators
    szFlagsTapeName          = "/t:Exchange " & szYYWW
    szDestinationFile        = "Exchange " & szYYWW & ".bkf"
    szSetDescription         = "/d " & chr(34) & "Created " & szYYMMDD & chr(34)
end if

' Instantiate File System Object
if (NO_ERROR = rc) Then
    err.clear
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    if (err.number) or (oFSO is nothing) Then
        rc = -1
        szError = "Failed Creating FSO: " & err.description & " -0x" & hex(err.number)
    end if
end if

' Look to see if the file exists to determine the backup type
if (NO_ERROR = rc) Then
    if not oFSO.FileExists(BACKUP_SHARE & "\" & szDestinationFile) then
        ' Normal | Copy | Differential | Incremental  Backup Type
        szFlagsType       = "/m normal "    
        szFlagsAppend     = ""  ' Don't Append if does not exist
    else
        ' File exists, so incremental backup. We are already in Append mode
        szFlagsType       = "/m incremental "  
        szSetDescription  = "/d " & chr(34) & "Inc " & szYYMMDD & chr(34)
    end if

    ' Release File SYstem Object
    set oFSO = Nothing
end if


' Create a Shell Object to be able to run the backup executable
if (NO_ERROR = rc) Then
    err.clear
    Set owShell = wscript.createobject("wscript.shell")
    if (err.number) or (owShell is nothing) Then
        rc = -2
        szError = "Failed Creating wscript.shell: " & err.description & " -0x" & hex(err.number)
    end if
end if

' Build the backup command and run it
if (NO_ERROR = rc) Then
    szCommandLine  = BACKUP_PROGRAM         & _
                     "backup"         & " " & _
                     szFlagsSelection      & " " & _
                     szSetDescription & " " & _
                     "/f " & chr(34) & BACKUP_SHARE & "\" & szDestinationFile & chr(34) & " " & _
                     szFlagsAppend           & " " & _
                     szFlagsLogging          & " " & _
                     szFlagsVerify           & " " & _
                     szFlagsRestrict         & " " & _
                     szFlagsRemoteStorage    & " " & _
                     szFlagsHardwareCompress & " " & _
                     szFlagsType             & " " & _
                     szFlagsJobName          & " "

    rc=owshell.run(szCommandLine,,True)
end if

set owShell = Nothing
wscript.quit(rc)


Function FormatNumber(szIn)
   FormatNumber = szIn
   if len(szIn) = 1 then FormatNumber = "0" & szIn
End Function

 

Save the script as something like "Exchange Backup.vbs" and add it to your scheduled tasks to run on a daily basis overnight. The script does contain one dependency, the backup set itself. To create this, start ntbackup, dismiss the wizard and select the backup tab. Ensure that the appropriate storage groups are selected, and save the selection to somewhere on disk. The output of this script will be files in the format "Exchange YYYY wnn.bkf" where YYYY is the year, and nn is the week number. For example, "Exchange 2005 w27.bkf" for this week. The file will have a single full backup at the start, and up to 6 differentials as well. Be sure to change the parameters in the first few lines of the script according to where you want to backup to.

Note also that if you want, you can backup to a local drive simply by using something like "c:\backup" instead of \\server\share in the BACKUP_SHARE definition at the top of the file.

I'm sure this will help someone (otherwise I've wasted my time....) Let me know if it does, or if you can do better without having to resort to more expensive backup solutions.

Comments
  • Joe - can you email me your version of the script (rename it to a .txt extension) and the output of cscript <scriptname>.vbs. Link is at the top. Hopefully something obvious. I'm actually on vacation right now, but will try to take a look. You might also want to try the "improved" version of the script rather than this one.

    Thanks,

    John.

  • The link to the updated script is broken. :(

  • I had to use Google's cache to find the new script

    http://66.102.9.104/search?q=cache:hAXzmKGWMycJ:https://blogs.technet.com/jhoward/archive/2005/11/04/improved-generic-exchange-backup-script.aspx+%22Weekly+Backup+Script+for+Exchange%22+howard&hl=en&ct=clnk&cd=2&gl=uk&client=firefox-a

  • Hi John,

    about 2 years ago I used your script to backup an exchange server 2003, and it worked great! Now I'm trying to accomplish the same thing again on an other exchange server, and I just can't get it to work. I've tried many things as using different paths, different backuplocations, etc. After every try I opended eventlog to have a look at the reports. And this is really interesting.

    When I have my BACKUP_SHARE set to "F:\" and my BACKUP_SELECTION set to "F:\ExchangeBackup.bks" (with no space in between) it will tell me "NTBackup-Fehler: 'Die gespeicherte Auswahldatei ""@F:\\F:\ExchangeBackup.bks" wurde nicht gefunden.", which means that it couldn't find the bks-file at that path.

    When I have my BACKUP_SHARE set to "F:\" and my BACKUP_SELECTION set to "F:\Exchange Backup.bks" (with a space in between) it will tell me "NTBackup-Fehler: 'Die gespeicherte Auswahldatei "Backup.bks" wurde nicht gefunden." which means that it just could find the bks-files, but it doesn't tell me where. Of course, here I also renamed the bks-files before I gave it try.

    Now when I have my BACKUP_SHARE set to "F:\" and my BACKUP_SELECTION set to "Exchange Backup.bks" (no drive letter given) it won't tell me anything. Here I can only see an empty txt-file in ntbackup reports.

    One thing is always the same. Every time I tried to backup the ntbackup window appeared shortly and was gone after a second again.

    What am I doing wrong here? I'm using the script above, since the link to the "improved" one doesn't work.

    Thanks and my best regards from Germany

    Paul

  • Hi John,

    about 2 years ago I used your script to backup an exchange server 2003, and it worked great! Now I'm trying to accomplish the same thing again on an other exchange server, and I just can't get it to work. I've tried many things as using different paths, different backuplocations, etc. After every try I opended eventlog to have a look at the reports. And this is really interesting.

    When I have my BACKUP_SHARE set to "F:\" and my BACKUP_SELECTION set to "F:\ExchangeBackup.bks" (with no space in between) it will tell me "NTBackup-Fehler: 'Die gespeicherte Auswahldatei ""@F:\\F:\ExchangeBackup.bks" wurde nicht gefunden.", which means that it couldn't find the bks-file at that path.

    When I have my BACKUP_SHARE set to "F:\" and my BACKUP_SELECTION set to "F:\Exchange Backup.bks" (with a space in between) it will tell me "NTBackup-Fehler: 'Die gespeicherte Auswahldatei "Backup.bks" wurde nicht gefunden." which means that it just could find the bks-files, but it doesn't tell me where. Of course, here I also renamed the bks-files before I gave it try.

    Now when I have my BACKUP_SHARE set to "F:\" and my BACKUP_SELECTION set to "Exchange Backup.bks" (no drive letter given) it won't tell me anything. Here I can only see an empty txt-file in ntbackup reports.

    One thing is always the same. Every time I tried to backup the ntbackup window appeared shortly and was gone after a second again.

    What am I doing wrong here? I'm using the script above, since the link to the "improved" one doesn't work.

    Thanks and my best regards from Germany

    Paul

  • I've already tried to leave a comment twice here. Is it still working?

  • I've tried to leave a comment severeal times. Somehow it doesn't seem to work anymore. I'm having problems with the script, and I would also like to download the "improved" version, but the link doesn't work anymore. All I get is a blank page.

    @Richard Taylor: your link Google's cache didn't work.

  • Paul - apologies for the delay in replying. I don't tend to look at comments on older posts. I'm not sure what's going on though in your case. Is this including if you run it from a command line? That tends to give a few more clues.

    The link does seem to be working - the formatting is dreadful, I admit, it needs a bit of revision since I change my blog template many months back. I'll go sort that out shortly.....

    Thanks,

    John.

  • PingBack from http://www.hilpers.it/2636142-backup-linea-di-comando-exch2003

  • PingBack from http://www.keyongtech.com/2373394-backup-of-virtual-machines

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment