VMM: Scripted Backup

Chris Wolf published an article on scripting backup of VMs here (vsbackup.vbs), based on Jeff Trumbull's VB backup script.  His vsbackup.vbs script is shown in the code here:

 Set objShell = CreateObject ("WScript.Shell")
'Load current date (formatted as mm-dd-yyyy) 
'into variable strToday
strTime = Month(Now) & "-" & Day(Now) & "-" & Year(Now) &_
  "_" & hour(now) & "-" &  minute(now)
' Backup target folder or UNC path
strBackupDir = "E:\Backup\VS\" & strTime & "\"
'Drive containing Virtual Machines
strVMdrive = "D:" 
'VM folder path
strVMfolder = "VS"
'available drive letter used to mount shadow copy
strTempDrive = "X:"

'Prepare shadow copy commands
sExCmd = "CreateVSS.cmd"
Set oFileSys = CreateObject("Scripting.FileSystemObject")
if oFileSys.FileExists(sExCmd) then oFileSys.DeleteFile(sExCmd)
set oExCmd = oFileSys.CreateTextFile(sExCmd, CopyOverwrite)

'create backup folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(strBackupDir)

' Create Shadow copy of VM drive
oExCmd.WriteLine "vshadow.exe -script=setvar1.cmd -p " &_
  strVMdrive
oExCmd.WriteLine "call setvar1.cmd"
oExCmd.WriteLine "vshadow.exe -el=%SHADOW_ID_1%," &_
  strTempDrive
oExCmd.Close
Result = objShell.run(sExCmd,vbMinimized, TRUE)

'Copy the virtual machine files from the shadow copy
strSource = strTempDrive & "\" & strVMfolder
objFSO.CopyFolder strSource , strBackupDir, TRUE

' Delete created shadow copy instance
if oFileSys.FileExists(sExCmd) then oFileSys.DeleteFile(sExCmd)
set oExCmd = oFileSys.CreateTextFile(sExCmd, CopyOverwrite)
oExCmd.WriteLine "Echo y | vshadow.exe -da"
oExCmd.Close
Result = objShell.run(sExCmd,vbMinimized, TRUE)

'Backup complete!
wscript.echo("Backup complete!")

In the VMM Scripting Guide, you will find a Windows PowerShell script that'll backup your VMM server.

Backup/Recover: What You Need to Know Before You Start

If you do not know whether the Virtual Machine Manager database is stored locally or on a remote server running SQL Server 2005, use the following procedure.

To determine whether the VMM database is stored locally or remotely

1.   Open the Registry Editor.

2.   Navigate to the following subkey:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager 2007 Server\Settings\Sql

3.   View the value for OnRemoteServer:

· If OnRemoteServer is set to 0, the database is on the local Virtual Machine Manager server.

· If OnRemoteServer is set to 1, the database is on a remote server running the SQL Server service.

BackupLocalVMM.ps1

You can use the BackupLocalVMM.ps1 script to back up the Virtual Machine Manager database if the database is stored locally on the Virtual Machine Manager server.

How BackupLocalVMM.ps1 Works

The first command in the script connects to VMMServer1 in the Contoso.com domain, retrieves the server object from the Virtual Machine Manager database, and stores the server object in variable $VMMServer:

$VMMServer = Get-VMMServer -ComputerName "VMMServer1.Contoso.com"

The second command in the script uses the Backup-VMMServer cmdlet to back up the Virtual Machine Manager database stored on VMMServer1 to C:\VMMBackups on VMMServer1:

Backup-VMMServer –Path "C:\VMMBackups" -VMMServer $VMMServer

The destination path (C:\VMMBackups in the following example) must be on a server that stores the Virtual Machine Manager database. The server must be running SQL Server 2005 or SQL Server 2005 Express Edition SP1. The following example assumes that SQL Server is installed on VMMServer1 rather than on a remote server.

BackupLocalVMM.ps1 - Complete Script

Copy the following complete version of BackupLocalVMM.ps1 into a Notepad file, and save it as BackupLocalVMM.ps1.

# Filename: BackupLocalVMM.ps1

# Description: Backs up the VMM database when the database

# is stored locally on the VMM server.

 

# DISCLAIMER:

# Copyright (c) Microsoft Corporation. All rights reserved. This

# script is made available to you without any express, implied or

# statutory warranty, not even the implied warranty of

# merchantability or fitness for a particular purpose, or the

# warranty of title or non-infringement. The entire risk of the

# use or the results from the use of this script remains with you.

 

# Substitute the name of your VMM server and domain in this command:

$VMMServer = Get-VMMServer -ComputerName "VMMServer1.Contoso.com"

 

# Substitute your backup folder path and name for C:\VMMBackups:

Backup-VMMServer –Path "C:\VMMBackups" -VMMServer $VMMServer