Microsoft KB Archive/302888

= How To Schedule Metabase Backups Using WSH =

Article ID: 302888

Article Last Modified on 8/8/2007

-

APPLIES TO


 * Microsoft Internet Information Services 5.0

-



This article was previously published under Q302888



SUMMARY
This article provides a sample script that can be used to back up the Internet Information Services (IIS) metabase as a scheduled task by using Windows Script Host (WSH).



MORE INFORMATION
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code &quot;as is&quot; without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. IIS supports programmatic administration of the metabase using the Active Directory Servies Interface (ADSI). The sample script that is listed below creates a backup of the metabase that will be stored in the following path:

%SystemRoot%\System32\InetSrv\MetaBack

NOTE: This folder is a fixed location and cannot be changed.

To create the sample WSH script and a scheduled task to run the script, follow these steps:   Save the following code in your C:\Inetpub\Adminscripts folder as Mbackup.vbs: '**************************************** ' Description: '  Metabase Backup Utility ' Syntax: '  CSCRIPT MBACKUP.VBS '****************************************

Option Explicit On Error Resume Next

' Declare variables. Dim strBackupName, lngBackupVersion, lngBackupFlags Dim objComputer Dim strDay, strMonth, strYear

' Get the current DD/MM/YY as strings. strDay  = Right(&quot;00&quot; & Cstr(Day(Date)),2) strMonth = Right(&quot;00&quot; & Cstr(Month(Date)),2) strYear = Cstr(Year(Date))

' Create a file name from the current date. strBackupName = strYear & &quot;-&quot; & strMonth & &quot;-&quot; & strDay

' Use the next available version number. lngBackupVersion = &HFFFFFFFF lngBackupFlags = 0 ' Output the backup message. Wscript.Echo &quot;Backing up metabase to file: &quot;&quot;&quot; & strBackupName & &quot;&quot;&quot;&quot;

' Get the ADSI object. Set objComputer = GetObject(&quot;IIS://LOCALHOST&quot;)

' Call the backup method. objComputer.Backup strBackupName, lngBackupVersion, lngBackupFlags

' Check for errors. If Err.Number <> 0 Then Wscript.Echo &quot;Error: &quot; & Err.Description & &quot; (0x&quot; & Right(String(8,&quot;0&quot;) & Hex(Err.Number),8) & &quot;)&quot; Else Wscript.Echo &quot;Success.&quot; End If NOTE: The following code can be appended to the previous sample to delete old backups. To customize this, change the value of the lngBackupToDelete variable in the script. '**************************************** ' The remaining code is optional and deletes a specific older backup. '****************************************

' The value of lngBackupToDelete determines which specific date to delete ' in this case. 28 days is four weeks ago if you are running a weekly backup. Const lngBackupToDelete = 28

' Declare variables. Dim strOldDay, strOldMonth, strOldYear, strOldBackupName

' Get the old DD/MM/YY as strings. strOldDay  = Right(&quot;00&quot; & Cstr(Day(Date-lngBackupToDelete)),2) strOldMonth = Right(&quot;00&quot; & Cstr(Month(Date-lngBackupToDelete)),2) strOldYear = Cstr(Year(Date-lngBackupToDelete))

' Create the file name from the old date. strOldBackupName = strOldYear & &quot;-&quot; & strOldMonth & &quot;-&quot; & strOldDay

' Output the deletion message. Wscript.Echo &quot;Deleting metabase backup file: &quot;&quot;&quot; & strOldBackupName & &quot;&quot;&quot;&quot;

' Call the deletion method. objComputer.DeleteBackup strOldBackupName, 0

' Check for errors. If Err.Number <> 0 Then If Err.Number = &H80070002 Then Wscript.Echo &quot;File not found.&quot; Else Wscript.Echo &quot;Error: &quot; & Err.Description & &quot; (0x&quot; & Right(String(8,&quot;0&quot;) & Hex(Err.Number),8) & &quot;)&quot; End If Else Wscript.Echo &quot;Success.&quot; End If                    Create a task to schedule the backup script. At a command prompt, type a command similar to the following:  For a task that runs every Monday at 6:00am:

AT 6:00am /every:M cscript.exe c:\inetpub\adminscripts\mbackup.vbs

 For a task that runs once at 9:00pm:

AT 9:00pm cscript.exe c:\inetpub\adminscripts\mbackup.vbs

 For a task that runs on the first of every month at 1:00am:

AT 1:00am /every:1 cscript.exe c:\inetpub\adminscripts\mbackup.vbs

 </ol>

Further Help
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

<div class="references_section">