Backup a VM with PowerCLI

Here’s a script to perform an unattended backup from a specific VM just cloning it into a different DataStore. I’m sure it could be more complete and customizable but can be used as a backbone for a more complex porpuse.

The ps1 has the following features:

  • Need two parameters : The Virtual Machine Name we want to Bakcup and the Name of the DataStore on which the VM cloned will be kept.
  • Check the VM Name and DataStore Name are real and exist in vCenter
  • Clone the original VM from a previous Snapshot.
  • Remove the Snapshot created before.
  • Move the Cloned VM into a Backup Folder

##################################################
# Name: VMBackup
# Desc: Backups a specific VM cloning it from a previous Snapshot
# Date: 05/07/2010
# Auth: mail.al.virtu@gmail.com
##################################################

Param ($SourceVM , $TargetDS)

# Backup Folder to keep your Backups
$BACKUP_FOLDER = “Backup”

# vCenter Server
$vCenterServer=””

# Set Date format for clone names
$CloneDate = Get-Date -Format “ddMMyyyy-hhmmss”

# Check VM Parameter

If (($SourceVM -eq $Null ) -or ($TargetDS -eq $Null)) {
cls
Write-Host “Usage: ”
Write-Host “BackupVM ” -ForeGroundColor White
Exit }

# Functions to check whether a VM or DS Exists

function ExistVM([string] $VMName) {
Get-VM | Foreach-Object { $FullVMList += $_.Name }

if ( $FullVMList.Contains( $VMName ) ) {
$true
} else {
$false
}
}

function ExistDS([string] $DSName) {
Get-DataStore | Foreach-Object { $FullDSList += $_.Name }

if ( $FullDSList.Contains( $DSName ) ) {
$true
} else {
$false
}
}

# PowerCLI Header
Write-host “Loading PowerCLI …” -ForeGroundColor Red
$VMwareLoaded = $(Get-PSSnapin | ? {$_.Name -like “*VMware*”} ) -ne $null
If ($VMwareLoaded) { }
Else
{
Add-PSSnapin -Name “VMware.VimAutomation.Core” | Out-Null
}

# Connect vCenter Server

Write-host “Connecting vCenter …” -ForeGroundColor Yellow
Connect-VIserver -server $vCenterServer | Out-Null

cls

# Let’s Rock

if ( ExistVM( $SourceVM ) -and ExistDS( $TargetDS ) ) {

$VM = Get-VM $SourceVM

Write-Host -foregroundcolor Green ” + Creating SnapShot ” $SourceVM

$CloneSnap = $VM | New-SnapShot -Name “$CloneDate-$SourceVM-CloneSnapShot”
$VMView = $VM | Get-View

$CloneFolder = $VMView.Parent
$CloneSpec = New-Object Vmware.Vim.VirtualMachineCloneSpec
$CloneSpec.SnapShot = $VMView.SnapShot.CurrentSnapShot

$CloneSpec.Location = New-Object Vmware.Vim.VirtualMachineRelocateSpec
$CloneSpec.Location.Datastore = $( Get-Datastore -Name $TargetDS | Get-View ).MoRef
$CloneSpec.Location.Transform = [Vmware.Vim.VirtualMachineRelocateTransformation]::Sparse

$CloneName = “$VM-$CloneDate-BAK”

Write-Host -foregroundcolor Green ” + Cloning … ” $SourceVM “into” $CloneName

$VMView.CloneVM( $CloneFolder, $CloneName, $CloneSpec ) | Out-Null

Write-Host -foregroundcolor Green ” + Moving to Folder … ” $BACKUP_FOLDER

Move-VM $CloneName -Destination $BACKUP_FOLDER | Out-Null
Get-VM $CloneName | Out-Null

Write-Host -foregroundcolor Green ” + Deleting SnapShot .. ”

Get-Snapshot -VM $( Get-VM -Name $VM ) -Name $CloneSnap | Remove-Snapshot -confirm:$false

if ( ExistVM( $CloneName ) ) {

Write-Host -ForegroundColor Green ” + $SourceVM has been Cloned into $CloneName ”
} else {

Write-Host -foregroundcolor Red ” + ERROR: $SourceVM couldn’t be Backed Up!”
}
} else {
Write-Host -foregroundcolor Red ” + ERROR: Either VirtualMachine ($VM2Backup) or DataStore ($TargetDS) don’t exist!”

}

#Disconnect from vCentre
Write-host “Closing vCenter session …” -ForeGroundColor Yellow
Disconnect-VIServer -Confirm:$false

#####################################################

Advertisements

2 thoughts on “Backup a VM with PowerCLI”

  1. Hi,
    this backup mode/script, in case fault of a disk / datastore with inside a VM, can i restore this VM directly taking it from the datastore used as destionazione of cloning?

    Regards,
    Daniel

  2. Hi Daniel,

    I’m sorry but I’m not quite sure what your question is. But let me guess.
    If original VM fails, as long as your cloned VM is safe an accesible on a datastore, you could just power it on and restore the VM state in the momento the snapshot was taken.

    Regards,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s