VMware PowerCLI

Backup VMware ESXi host with PowerCLI

VMware PowerCLI

Introduction to VMware ESXi

As you should know right know having backups of your system is a critial part of protecting your data. Although VMware ESXi host don’t contain data themselve it still is a good practice to backup your configuration to make disaster recovery alot easier and faster. In this blog post we are going to setup a script that retrieves the host configuration via PowerCLI. If you don’t know what PowerCLI is check out my previous post about installing it on Linux or macOS by click the link.

VMware PowerCLI

First we start by creating our VMware PowerCLI PowerShell script. This scripts connects to the VMware vCenter host and gets all the nodes in the cluster. For each host in the cluster it will get the configuration settings via the Get-VMHostFirmware command and outputs it to the desired location.

#/bin/pwsh

$output_directory = ""
$vcenter = ""
$cluster = ""
$user = ""
$password = ""

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

Connect-VIServer -Server $vcenter -User $user -password $password

cd $output_directory

$VMhosts = Get-Cluster  $cluster | Get-VMHost

foreach($VMhost in $VMhosts) {Get-VMHostFirmware -VMHost $vmhost -BackupConfiguration -DestinationPath $output_directory}

You need to fill in the output_directory, vcenter, cluster, user and password parameters for the script to work. You can then start up your PowerShell and run the script. The following output will be generated if ran correctly.

PS ./backup-vmware-hosts.ps1

Scope    ProxyPolicy     DefaultVIServerMode InvalidCertificateAction  DisplayDeprecationWarnings WebOperationTimeout
                                                                                                  Seconds
-----    -----------     ------------------- ------------------------  -------------------------- -------------------
Session  UseSystemProxy  Multiple            Ignore                    True                       300
User                                         Ignore                                               
AllUsers                                                                                          

IsConnected   : True
Id            : /VIServer=vsphere.local\user@vcenter:443/
ServiceUri    : https://vcenter/sdk
SessionSecret : "SessionSecret"
Name          : $vcenter-IP
Port          : 443
SessionId     : "SessionId"
User          : VSPHERE.LOCAL\user
Uid           : /VIServer=vsphere.local\user@vcenter:443/
Version       : 7.0.3
Build         : 19234570
ProductLine   : vpx
InstanceUuid  : InstanceUuid
RefCount      : 1
ExtensionData : VMware.Vim.ServiceInstance


Url  : http://esxi01/downloads/UID/configBundle-esxi01.tgz
Data : $output_directory/configBundle-$esxi01-IP.tgz
Host : $esxi01-IP


Url  : http://esxi02/downloads/UID/configBundle-esxi02.tgz
Data : $output_directory/configBundle-$esxi02-IP.tgz
Host : $esxi02-IP

Restoring the backup

Like any backup your only have a working backup if you can restore it. The configBundle that was been downloaded with the VMware PowerCLI script can be restored with the following steps

# Connect to the ESXi host
Connect-VIServer -Server ESXi_host-IP_address -User root -Password exampleRootPassword 

# First put the host in maintenance mode
Set-VMHost -VMHost ESXi_host_IP_address -State 'Maintenance'

# Upload the configuration to a location accessible by the ESXi host 

# Restore the configuration
Set-VMHostFirmware -VMHost ESXi_host_IP_address -Restore -SourcePath backup_file -HostUser username -HostPassword password

For example:

Set-VMHostFirmware -VMHost 10.0.0.1 -Restore -SourcePath c:\bundleToRestore.tgz -HostUser root -HostPassword exampleRootPassword

Conclusion

With the above steps and information you will be able to backup your VMware ESXi hosts and restore the configuration in a disaster recovery event or configuration error. Always make sure to check and verify the created backups. You only have a working backup when you test the restore procedure.

One comment

  1. Hiya very nice blog!! Man .. Excellent .. Superb .. I will bookmark your web site and take the feeds additionally厈I am glad to search out a lot of useful information here in the submit, we want develop more strategies on this regard, thanks for sharing. . . . . .

Leave a Reply

Your email address will not be published. Required fields are marked *