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 https://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
Anonymous
January 01, 2003
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.Anonymous
January 01, 2003
PingBack from http://www.keyongtech.com/2373394-backup-of-virtual-machinesAnonymous
January 01, 2003
PingBack from http://www.hilpers.it/2636142-backup-linea-di-comando-exch2003Anonymous
January 01, 2003
John Howard posted a nifty script for backing up Exchange servers.Anonymous
January 01, 2003
John has been nagging me to blog about this as he's from an Exchange developer background and gets excited...Anonymous
January 01, 2003
The comment has been removedAnonymous
January 01, 2003
So after numerous emails about this, and some comments on my previous blog post back in June , I spentAnonymous
January 01, 2003
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.Anonymous
June 29, 2005
The comment has been removedAnonymous
August 09, 2005
Trackback: Are there any scripts to backup VMs?Anonymous
August 09, 2005
Hi Dugie - yes, have a look at the technet script center @ http://www.microsoft.com/technet/scriptcenter/scripts/vs/default.mspx
Cheers,
JohnAnonymous
October 24, 2005
Great script John.
Thanks.
Notification through email would be super.Anonymous
November 04, 2005
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.Anonymous
April 17, 2008
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!!!Anonymous
July 01, 2008
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.Anonymous
August 02, 2008
The link to the updated script is broken. :(Anonymous
October 07, 2008
The comment has been removedAnonymous
October 24, 2008
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 PaulAnonymous
October 29, 2008
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 PaulAnonymous
November 05, 2008
I've already tried to leave a comment twice here. Is it still working?Anonymous
November 06, 2008
The comment has been removed