根据 VMware KB 2042141 知识库文章,SSH 登录到 ESXi 主机,可以使用下面的命令用于备份和还原 ESXi 主机的配置文件。但是,这个方法需要手动登录到每个 ESXi 主机,并通过浏览器链接下载配置文件,显然不是很方便。
- 备份
vim-cmd hostsvc/firmware/sync_config
vim-cmd hostsvc/firmware/backup_config
- 还原
vim-cmd hostsvc/maintenance_mode_enter
vim-cmd hostsvc/firmware/restore_config 0
当然还可以使用 PowerCLI 命令方法备份 ESXi 主机配置文件,这种方法相对于上面那种来说要方便一些。
- 备份
Get-VMHostFirmware -VMHost ESXi_host_IP_address -BackupConfiguration -DestinationPath output_directory
- 还原
Set-VMHost -VMHost ESXi_host_IP_address -State 'Maintenance'
Restart-VMHost -VMHost ESXi_host_IP_address -Confirm:$false
Set-VMHostFirmware -VMHost ESXi_host_IP_address -Restore -SourcePath backup_file -HostUser username -HostPassword password
通过 PowerCLI 方法,可以借助 PowerCLI 脚本批量备份 ESXi 主机的配置文件。这里使用了 THEDXT 制作的 vCenter_ESXi_Config_Backup.ps1 备份脚本,让我们来看看如何使用它。
# vCenter ESXi Config Backup
# Version 1.0.0
#
# Author: Daniel Keer
# Author URI: https://thedxt.ca
# Script URI: https://github.com/thedxt/VMware
#
# DESCRIPTION
# Backs up each ESXi host in vCenter
#
# EXAMPLE
# esxi-conf-backup -vcenter "vcenter.contoso.com" -folder "C:\ESXi-Backups"
# Main function
function esxi-conf-backup{
param(
[Parameter (Mandatory = $true)] [String]$vcenter,
[Parameter (Mandatory = $true)] [String]$folder,
[Parameter(Mandatory=$false)] [ValidateSet('Yes','No')] [String]$connected
)
# function to check if folder exists if not make it
function folder-check{
if (-not (Test-Path $folder))
{
write-host $folder "Does NOT exist creating it"
New-Item -ItemType Directory $folder | out-null
write-host $folder "has been created"
}else{
write-host $folder "Exists no action needed"
}
}
# Function to connect to vCenter
function connect-vcenter{
if($connected -eq "Yes"){
Write-host "Connected value is Yes."
write-host "no prompt to connect to vCenter will appear"
}else{
write-host "Connected value not set assuming vCenter is not connected."
write-host "prompting to connect to vCenter"
Connect-VIServer -Server $vcenter
}
}
# function to backup the ESXi hosts in vCenter.
function esxi-backup{
Write-host "Starting the backup process"
Write-host "Getting all the ESXi hostnames"
#get all the ESXi hostnames
$hosts = Get-VMHost | select name
#set the date format
$date = get-date -f yyyy-MMM-dd_HHmmss
#loop for each of the ESXi hosts
Foreach ($singlehost in $hosts)
{
#backup to C:\$folder
$backup = Get-VMHostFirmware -VMHost $singlehost.Name -BackupConfiguration -DestinationPath $folder
Write-host "Running backup for" $singlehost.Name
#run the backup but dont output anything to the screen
$backup | out-null
Write-host "Backup completed for" $singlehost.Name
# get the version and build number for the host
Write-host "Getting Version and Build info for" $singlehost.Name
$verinfo = Get-VMHost -Name $singlehost.Name | select Version, Build
# rename the files to have the hostname and version and build number and the date info
Write-host "Fixing the backup file name for" $singlehost.Name
Rename-Item -path $backup.Data.FullName -newname "$($backup.Host.name)_v$($verinfo.Version)_b$($verinfo.Build)_$($date).tgz"
Write-host "Everything is completed for" $singlehost.Name
Write-host "---------------------------"
}
Write-host "The backup process is completed. Backups are located in" $folder
}
#check if connect is defined if not defined assume not connected and connect
connect-vcenter
#check if the folder exists
folder-check
#run the backup
esxi-backup
#end of main function
}
注意,使用这个脚本的前提是需要安装 PowerCLI 环境以及安装 vCenter Server 的证书。
1.打开 PowerShell 并进入到脚本所在的目录。
ls .\vCenter_ESXi_Config_Backup.ps1
2.运行 PowerCLI 命令连接到vCenter Server。
Connect-VIServer vcsa7-01.lab.com -user [email protected] -password VMware1@#
3.运行 PowerCLI 脚本,准备使用如下备份命令。
. .\vCenter_ESXi_Config_Backup.ps1
4.如果已经连接到 vCenter Server,运行下面命令执行配置备份。
esxi-conf-backup -vcenter "vcsa7-01.lab.com" -folder "C:\ESXi-Backup" -connected Yes
5.查看备份文件,如果上面导出的目录没有提前创建则自动创建。文件名被重命名为ESXi主机名+安装版本+内部版本号+时间戳。
ls "C:\ESXi-Backup"
6.如果没有连接到 vCenter Server,运行下面命令后输入用户名和密码执行配置备份。
esxi-conf-backup -vcenter "vcsa7-01.lab.com" -folder "C:\ESXi-Backup"
注意,执行还原过程请参阅官方 KB 文章中的方法,并按要求使用与源 ESXi 主机相同的内部版本。
标签:配置文件,ESXi,Write,host,PowerCLI,folder,backup,vCenter From: https://www.cnblogs.com/juniormu/p/18299139