1

I am looking for a way to automate nightly backups of my VMWare ESXi Configs for disaster recovery.

Ideally the script would connect to my vCenter server, poll it for hosts, and then backup the config in a logical directory structure, including the current ESXi version running, since config backups can only be restored to a machine running the exact same build.

Is there such a script available somewhere?

Glenn Sullivan
  • 1,368
  • 9
  • 17

1 Answers1

1

There sure is. Here is an example, using Power-CLI:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

$backupbasedir = "<Base directory to store the backups>"
$username = "<Username with the correct right in vCenter>"
$password = "<Password for that user>"
$VCenterServer = "<FQDN of the vCenter server>"

if ($backupbasedir.Substring($backupbasedir.Length - 1, 1) -ne "\") {
    $path = $backupbasedir + "\"
}
else {
    $path = $backupbasedir
}

Connect-VIServer $VCenterServer -User $username -Password $password

Get-VMHost | ForEach-Object {
    $path = $path + $_ + "\" + $_.ExtensionData.Config.Product.Version + "\" + $_.ExtensionData.Config.Product.Build
    if (!(Test-Path $path)) { New-Item -ItemType directory -Path $path }
    Get-VMHostFirmware -VMHost $_ -BackupConfiguration -DestinationPath $path
}

This script first disables the error message when certificate errors happen, and then in goes through all of the hosts on a particular vCenter and backs up their config to a directory structure of "\ServerName\ESXiVersion\BuildNumber"

This makes rebuilding a particular host very easy...

  1. Reinstall the correct major version of ESXi.
  2. Patch it to the correct build number of your latest backup. The easiest way I have found to do that is to use the esxcli software profile update command and point it to the proper download location for the build number you need. A list of the correct locations was found at https://tinkertry.com/easy-update-to-latest-esxi at the time of this post.
  3. Restore the latest backup with the "Set-VMHostFirmware" command: Set-VMHostFirmware -VMHost ESXi_host_IP_address -Restore -SourcePath <Backup Location>
  4. Reboot and reconnect.
Glenn Sullivan
  • 1,368
  • 9
  • 17