29 June 2005

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

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.

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Andy HJ said:
Nice script John!
Just gave it a quick go and it works very well.
Like the error checking - I had been running a similar script (but as a .bat file) but yours is better.
Ntbackup is very verstile from the command line although I have noticed that the catalog switch seems to missing from 2K3 standard edition.
29 June 05 at 11:54 AM
# subject: exchange said:
01 July 05 at 6:15 AM
# subject: exchange said:
01 July 05 at 6:17 AM
# subject: exchange said:
01 July 05 at 6:18 AM
# subject: exchange said:
01 July 05 at 6:19 AM
# Exchange Cookbook said:
John Howard posted a nifty script for backing up Exchange servers.
18 July 05 at 9:03 PM
# Eileen Brown's WebLog said:
John has been nagging me to blog about this as he's from an Exchange developer background and gets excited...
19 July 05 at 12:42 PM
# Andrew Dugdell said:
Trackback: Are there any scripts to backup VMs?
09 August 05 at 7:23 AM
# jhoward said:
Hi Dugie - yes, have a look at the technet script center @ http://www.microsoft.com/technet/scriptcenter/scripts/vs/default.mspx
Cheers,
John
09 August 05 at 8:01 AM
# Martin said:
Great script John.
Thanks.
Notification through email would be super.
24 October 05 at 5:00 PM
# jhoward said:
Hi Martin - finally got round to it. Have a look at the revised script at http://blogs.technet.com/jhoward/archive/2005/11/04/413678.aspx
Cheers,
John.
04 November 05 at 2:02 PM
# John Howard said:

So after numerous emails about this, and some comments on my previous blog post back in June , I spent

19 July 07 at 11:50 AM
# Raul said:

Hello,

I had been looking for a script to backup data (SQL databases) on my Windows server 2003.

I am totally new to this. Will be great if someone can give me a little push so that I know from where to start.

Thank you!!!

17 April 08 at 8:35 PM
# jhoward said:

Raul - unfortunately, this really isn't my area of expertise. I would recommend you maybe ask on one of the SQL blogs such as http://blogs.msdn.com/psssql/

Alternatively, there's a few scripting and SQL forums/newsgroups around who will be able to provide far more help that I would be able to.

Thanks,

John.

17 April 08 at 8:58 PM
# Joe said:

John, I can't seem to get the script to work.  I don't quite understand the dependency.  I created the bks file and linked in the top section of your script, however when I run the script it does nothing.  

01 July 08 at 1:14 PM
# jhoward said:

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.

01 July 08 at 2:10 PM
# Clark said:

The link to the updated script is broken. :(

02 August 08 at 10:10 PM
# Richard Taylor said:

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

07 October 08 at 10:14 AM
# Paul said:

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

24 October 08 at 6:41 AM
# Paul Heidenreich said:

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

29 October 08 at 1:29 PM
# Paul said:

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

05 November 08 at 12:43 PM
# Paul said:

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.

06 November 08 at 3:09 AM
# jhoward said:

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.

10 November 08 at 10:06 PM
# backup linea di comando exch2003 | hilpers said:

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

21 January 09 at 10:25 AM
# Backup of Virtual Machines | keyongtech said:

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

21 January 09 at 8:29 PM

Leave a Comment

Comment Policy: No HTML allowed. URIs and line breaks are converted automatically. Your e–mail address will not show up on any public page.

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Page view tracker