PowerCLI – Backup and Restore ESXi Configuration (works with vSphere 7)

As you may recall from previous posts and general best practice in VMware environments, it is a good idea to perform a regular backup of both your vCenter and your ESXi host configuration.  Having these backups in place can certainly reduce the downtime in the event of an issue, and help to ease the recovery.

Other blog posts have taken a look at the process of performing a backup and restore of the ESXi configuration with PowerCLI, and many utilise the VMware documentation which is still quite a manual process.  I felt that it would be good to have some scripts available that could backup either a single ESXi host or backup all of the ESXi hosts on a vCenter or just the ESXi hosts in a cluster.  I then wanted to see how easy it would be to script the possible restoration of a number of ESXi hosts… the following scripts are what I came up with.

Quick disclaimer:  the scripts provided have been developed and tested on a lab environment running vCenter 7 and ESXi 7, utilising PowerCLI 12.0.0 build 15947286.  If you wish to utilise these scripts then please perform full testing of them in your own environments.  By utilising these scripts, you accept full responsibility for what the scripts may do to your environment and the data collected and restored by the scripts.  These scripts are provided as is.

 

Backup a Single ESXi Host

This was the first script that I started looking at for this process.  This is pretty basic, allowing the user to customise the ESXi host to be backed up, including the username and password – please note that in this version of the script, it requests the password when you run it.  The script will then use these user defined variables to, initially, connect directly to the ESXi Host, create the configuration backup to the location specified and then disconnect from the ESXi Host.  The resulting file will be a .tgz file using the naming convention:  configBundle-esxihostname.tgz  – so if the hostname was:  ESXi-01, then the backup file would be called:  configBundle-ESXi-01.tgz

# Script to backup ESXi hosts to a destination of your choice
#
# Created by Chris Bell, 2020
# Version 1.0
#
#------------------------------------------------------------#
# User defined variables start here
#
# Specify ESXi host to connect to, including username, password will
# be requested during the script run
$esxihost="enter name of esxi host"
$esxiusername="enter username (root)"
$esxipassword=Read-Host -Prompt "Input the password"</pre>

# Specify Backup Location
$backuploc="enter backup destination"

#
# End of user defined variables
#------------------------------------------------------------#

# Connect to ESXi host
Connect-VIServer -Server $esxihost -User $esxiusername -Password $esxipassword

# Create backup of ESXi host
Get-VMHostFirmware -VMHost $esxihost -BackupConfiguration -DestinationPath $backuploc

# Disconnect from ESXi host
Disconnect-VIServer -Server $esxihost -Confirm:$false

 

Backup Multiple ESXi Hosts

After performing that test of the process using a single host, it was time to move onto enhancing the script for multiple hosts using a vCenter connection.  Once again we have some user defined variables.  This time the first few variables are referencing the vCenter name, Username for the vCenter and Password.  The password is stored in plain text on this version of the script although I will look to utilise encrypted passwords in future versions.  We then have an option on whether to backup all ESXi hosts on the vCenter or just the hosts from a cluster.  If you wish to backup all hosts, then the $backupall entry should read “Yes”.  If you only wish to backup ESXi hosts from a cluster, then you should make sure that $backupall reads “No” and enter the name of the cluster for $cluster.  Finally we have the backup location specified again.  As there could be quite a few hosts involved for a whole vCenter, then you should make sure that your backup location has sufficient space for the backup files.  You may wish to utilise the previous script to backup a single host to understand the size requirements for a single ESXi configuration backup to know how much space is likely to be required for your number of hosts.  With the user defined variables in place, the script can then be run.

The script will take the backup location and create a new folder with the current date.  This allows for the script to be run on a schedule with multiple copies of the ESXi backup being performed.  Each time an ESXi backup is performed for a host, it utilises the same name and would therefore overwrite a previous version if it already existed.  After this, the script will connect to the vCenter server.  If the script has been configured to only backup up the ESXi Hosts in a cluster, then it will look for the cluster and collect the names of all of the ESXi hosts in that cluster.  After this it will create a backup of the configuration of each of the ESXi hosts in its list in turn.  If the script has been configured to backup all of the ESXi Hosts on the vCenter, then it will make a list of all of these Hosts and will then create a configuration backup of each of those hosts in turn.  The resulting file will be a .tgz file using the naming convention:  configBundle-esxihostname.tgz  – so if the hostname was:  ESXi-01, then the backup file would be called:  configBundle-ESXi-01.tgz

Each host backup will be created in turn and once completed, the script will disconnect from the vCenter server.

# Script to backup ESXi hosts to a destination of your choice
#
# Created by Chris Bell, 2020
# Version 1.0
#
#------------------------------------------------------------#
# User defined variables start here
#
# Specify vCenter to connect to, including username and password
$vcentername="enter vCenter name"
$vcenterusername="administrator@vsphere.local"
$vcenterpassword="enter vCenter password"

# Specify whether to backup all ESXi hosts or just from a cluster
$backupall="Yes"
# Change to No and specify the cluster to backup
# if only want cluster hosts to be backed up
$cluster="enter cluster name"

# Specify Backup Location
$backuploc="enter backup destination"

#
# End of user defined variables
#------------------------------------------------------------#

# Create a new folder in the backup location which will be the
# destination for the backups
$backupdate = Get-Date -Format "dd-MMM-yyyy"
New-Item -Path $backuploc -Name $backupdate -ItemType "directory"
$backupfinal = $backuploc + "\" + $backupdate

# Connect to vCenter
Connect-VIServer -Server $vcentername -User $vcenterusername -Password $vcenterpassword

if ($backupall -eq "No"){

# Get a list of all ESXi hosts in the specified cluster and then back them up
$esxilist = Get-Cluster -Name $cluster | Get-VMHost
foreach($esxi in $esxilist){
Get-VMHostFirmware -VMHost $esxi -BackupConfiguration -DestinationPath $backupfinal
}
}
elseif ($backupall -eq "Yes") {
# Get a list of all ESXi hosts in vCenter and then back them up
$esxilist=Get-VMHost
foreach($esxi in $esxilist){
Get-VMHostFirmware -VMHost $esxi -BackupConfiguration -DestinationPath $backupfinal
}
}
else {
# Display error message
Write-Host "You do not appear to have entered Yes or No for the backup setting. Please recheck your user defined variables"
}

# Disconnect from vCenter
Disconnect-VIServer -Server $vcentername -Confirm:$false

 

Restore ESXi Host Backups

So now we were at the point where we have a capability to perform a single host backup or multiple host backups from a whole vCenter or a cluster.  The backups are no good unless you can test them by performing a restore.  There are PowerCLI commands available for performing the restore element as well and therefore I thought that I would see if I could have a script that could perform the restore of a single host or multiple hosts based on the backups performed in the previous scripts.

The script once again allows you to define user variables for vCenter name, username and password.  Once again the password is stored in plain text in this version of the script.  You will also notice that there are also ESXi username and password entries, this is because the restore process requires that the ESXi Host and username are provided to perform the restore.  Again the password is stored in plain text.  Finally, you specify the location where the backup files are located.  When specifying the backup location, please remember that if you have utilised the previous script to create the backups, the backup files will be located in a subfolder based on the date, this will need to be added to the backup location to successfully restore those backups.

Please also note that if you wish to restore just a single backup, remove all of the other ESXi host backup files from the backup location as the script will take the names of the files in that folder to connect to the hosts.  When performing the restore of the hosts, please make sure that the hosts in question have been built with the same build of ESXi, have been given the same host name and IP Address and have been joined to vCenter.  The restore process should restore the rest of the configuration (including Standard vSwitch information but not Distributed vSwitch information).

The script will connect to vCenter.  It will then take a listing of the files located in the backup location.  As the ESXi host configuration backup files all follow the same naming pattern, we remove the last four characters of the filename (.tgz) and then the first thirteen characters of the filename (configBundle-) to leave us with the name of the ESXi Host.  This host name is then used to place the host into maintenance mode (required for the restore process), if you have DRS in place, then all of the VMs on this host will migrate to other hosts.  Once the host is in maintenance mode, the script pauses for a few seconds before performing the actual restore process.  During the restore process, the host will temporarily lose connectivity to the vCenter and will stop pinging for a few seconds, this is normal as the configuration is being restored.  With this in mind, I have included a sleep process within the script at this point for around 80 seconds to allow sufficient time for the host to recover it’s configuration correctly and become available again… if you experience errors during the next phase of the script then you may need to increase the sleep period.  The final part of the restore process, is to take the host out of maintenance mode again before repeating the process on the next host etc.

Once done, the script will disconnect from the vCenter.

# Script to restore a series of ESXi hosts previously backed up
#
# Created by Chris Bell, 2020
# Version 1.0
#
#------------------------------------------------------------#
# User defined variables start here
#
# Specify vCenter to connect to, including username and password
$vcentername="enter vcenter name"
$vcenterusername="administrator@vsphere.local"
$vcenterpassword="enter password"

#Esxi Host Credentials
$ESXiUsername="enter esxi username (root)"
$ESXiHostPassword="enter esxi password"

# Specify Backup file Location
$backuploc="enter backup file location"

#
# End of user defined variables
#------------------------------------------------------------#

$esxihost=""

# Connect to vCenter
Connect-VIServer -Server $vcentername -User $vcenterusername -Password $vcenterpassword

#Check backup location and extract all host names from files

$Filelist = Get-ChildItem -Path $backuploc | Select-Object -ExpandProperty Name
foreach ($file in $Filelist) {
$tempfile = $file.Substring(0,$file.Length-4)
$esxihost = $tempfile.substring(13)
$Fileloc = $backuploc + '\' + $file
# Place host into maintenance mode
Set-VMHost -VMHost $esxihost -State 'Maintenance'

Start-Sleep -Second 20

# Restore backup
Set-VMHostFirmware -VMHost $esxihost -Restore -SourcePath $Fileloc -HostUser $ESXiUsername -HostPassword $ESXiHostPassword

Start-Sleep -Second 80

# Take host out of maintenance mode
Set-VMHost -VMHost $esxihost -State 'Connected'

}

# Disconnect from vCenter
Disconnect-VIServer -Server $vcentername -Confirm:$false 

 

 

 

About the Author

Dinger

I'm a technical person who has held many IT related jobs for the past 20+ years. I am now working for VMware helping customers to maximise their investment in VMware technologies.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: