ESXi host configuration backup and restore script using PowerCLI
After virtualising our entire server farm we were faced with rewriting the whole DR plan to reflect the Infrastructure in place.
Restoring VMs was the easy part and was tried & tested a good number of times before we actually took the plunge to virtualise so I focused some time on maintaining regular backups of our ESXi Host estate.
There were 3 paths we could take:
- Build brand new ESXi servers at the DR site during tests or DR invocation and keep the default build profile. This means we would be working with hosts that are built with a different configuration/profile that we aren’t used to.
- Build brand new ESXi servers at the DR site during tests or DR invocation and restore the previously backed up ESXi configuration. This would mean that the configuration is EXACTLY as it was in the live environment.
- If running ESXi on a USB stick then power off an ESXi host, unplug the stick and clone the USB stick using WinImage or something similar. Label the ESXi USB sticks and send them to be stored at the DR site. When invoking DR you can then optionally restore the previously backed up ESXi configuration if there have been changes after the USB sticks were built.
We were lucky enough to already be running our hosts as ESXi servers on USB sticks so path 3 was by far the best option as we would have a less stressful DR environment that was exactly the same in configuration as the live environment.
The only thing I needed to figure out was how to back up just the configuration of an ESXi host and find a way to schedule the backups.
Once again we call upon the awesome power of PowerCLI…
Script Info
The script allows us to back up the ESXi hosts during our backup window by using Windows Scheduler to open powershell and run this script as frequently as we need.
You can schedule the script to run as a user with only the permission required to run the task performed by this script.
To back up all your ESXi hosts in your vCenter environment then use script 1 below.
To restore one ESXi host then use script 2 below.
The scripts should work if the user you are running powershell as has the relevant permission in vCenter for the task it performs.
You only need to replace the string values in the VARIABLES section to tailor this to your own environment.
SCRIPT 1 – Back up ESX(i) Hosts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | function LoadSnapin{ param($PSSnapinName) if (!(Get-PSSnapin | where {$_.Name -eq $PSSnapinName})){ Add-pssnapin -name $PSSnapinName } } # Load PowerCLI snapin LoadSnapin -PSSnapinName "VMware.VimAutomation.Core" # Variables [string] $vCenter = "vcenter.domain.local" # vCenter FQDN [string] $BackupPath = "C:\Host_Backup" # Folder to copy the Host backups to # Connect to vCenter Connect-VIServer -Server $vCenter # Get All Connected Hosts $eVMHs = Get-View -ViewType HostSystem |?{$_.config.product.ProductLineId -eq "embeddedEsx"} |?{$_.Runtime.ConnectionState -eq "connected"} # For each Host in All Connected Hosts Foreach ($eVMH in $eVMHs) { # Backup Host config Set-VMHostFirmware -VMHost $eVMH.name -BackupConfiguration -DestinationPath $BackupPath } |
SCRIPT 2 – Restore ESX(i) Host
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | function LoadSnapin{ param($PSSnapinName) if (!(Get-PSSnapin | where {$_.Name -eq $PSSnapinName})){ Add-pssnapin -name $PSSnapinName } } # Load PowerCLI snapin LoadSnapin -PSSnapinName "VMware.VimAutomation.Core" # Variables [string] $vCenter = "vcenter.domain.local" # vCenter FQDN [string] $backupFile = "C:\Host_Backup\configBundle-esx01.domain.local.tgz" # ESX(i) host backup file [string] $esxHost = "esx01.domain.local" # FQDN of ESX(i) to be restored [string] $esxAccount = "root" # local ESX(i) account for the Host to be restored [string] $esxPasswd = "password" # password for ESX(i) account named above # Connect to ESX(i) Host connect-viserver -Server $esxHost -User $esxAccount -Password $esxPasswd # Put host in maintenance mode set-VMHost -vmhost $esxHost -state Maintenance # Get Host Firmware Get-VMHostFirmware -VMHost $esxHost # Restore Host Firmware Set-VMHostFirmware -VMHost $esxHost -Restore -SourcePath $backupFile -Force # Disconnect from Host Disconnect-VIServer -Server $esxHost -Confirm:$false |
Feedback
I hope someone finds these somewhere near as useful as I have.
If anyone has any suggestions about how I can improve the code or any better ways of writing the code in any of my scripts then please let me know – I’m always looking to level-up.