Regularly throughout the year, I like to take PIT (Point in Time) backups of my ESXi host configurations. These form part of my Critical Config backups that I hope I’ll never have to use.
But like any backup, these are merely another safety net as we strive to have more resilient systems.
With the calendar year now drawing to a close, it’s a good time to do this procedure.
PowerCLI makes this task simple and a non-event. This will utilize the “Get-VMHostFirmware” commandlet.
From a PowerCLI session, establish a connection to your VC server (or single ESXi host) using Connect-ViServer
Note: if you have the original self-signed certificate and receive yellow warning message regarding an invalid cert, you can run Set-PowerCLIConfiguration – InvalidCertifcateAction Ignore to ignore the warning.
The following one liner will backup the host with the esxihostname value and place the config bundle into C:\CriticalConfigs\ESXiHosts
1 |
Get-VMHostFirmware -VMHost esxihostname -BackupConfiguration -DestinationPath C:\CriticalConfigs\ESXiHosts |
Now let’s put this to work in a more useful way.
The following will backup all hosts connected to the Virtual Center server you specify, and save the config bundles to C:\CriticalConfigs\ESXiHosts. The backup files will have the current date (in the format specified in the $datetime variable appended to the default filename, before the extension.
After the files are renamed, an email is sent showing the filenames and locations for reference and verification.
Files already in the Output Directory will be moved to the Archive directory specified.
Configure the variables;
-
$savedir [Location for the config backup bundles]
-
$archivedir [Location for backup archives]
-
$datetime [Change the format as required, I have it set to ddMMYY]
-
$VCServer [Change to your Virtual Center Server hostname]
-
Email details in the ‘EmailResults‘ Function will need to be customised for your own environment also.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$savedir = "c:\CriticalConfigs\ESXiHosts" $archivedir = "c:\CriticalConfigs\ESXiHosts\Archive" $datetime = (Get-Date -f dd_MM_yy) $VCServer = "Virtual_Center_Hostname" Function ArchiveExisting { Move-item -path $savedir\*tgz -destination $archivedir } Function DoBackup { Connect-VIServer $VCServer Get-VMHost | Get-VMHostFirmware -BackupConfiguration -DestinationPath $savedir } Function RenameBackup { Get-ChildItem $savedir\*.tgz |Rename-Item -NewName {$_.BaseName+"_"+($datetime)+$_.Extension}} Function EmailResults { $MsgBody = (gci c:\CriticalConfigs\ESXiHosts | select-object Name, LastWriteTime |ft |out-string) send-mailmessage -from "ESXiBackups@domain.com.au" -to "recipient@domain.com.au" -subject "ESXi Host Backups complete" -body "$MsgBody" -SmtpServer mailserver@domain.com.au } ArchiveExisting DoBackup RenameBackup EmailResults |
Script execution;
Files after being processed;
Email Message body;
This is more of a modularised script, so more function blocks can be added in as required, or to form a larger base to do more tasks.
It’s easily customisable, for example if you wanted to restrict it to certain hosts, cluster(s) multiple VC servers, filter the “Get-VMHost” using ‘where’ options.
Add it into a scheduled task according to your preference or run it on demand.
Happy backups..
Hey Guys,
Great Script, Thanks, but i have a few questions ?
when i launch the script i have a few errors –
Get-VMHostFirmware : 25/06/2018 15:55:46 Get-VMHostFirmware A general system error occurred: Internal error
At Y:\Script Backup\Vmware-AutoConfigBackup.ps1:27 char:14
+ … et-VMHost | Get-VMHostFirmware -BackupConfiguration -DestinationPath …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-VMHostFirmware], SystemError
+ FullyQualifiedErrorId : Client20_SystemManagementServiceImpl_BackupVmHostFirmware_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetVMHostFir
mware
any Ideas what is going wrong ?