How To Back Up and Restore Esxi Host

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

How to: A Complete Guide to Backing Up

and Restoring VMware ESXi Host


Configuration

Aleks (NAKIVO)
Dec 23, 2020 5 Minute Read

 Spice(17)
 Reply (0)
 Subscribe
 Share

While the importance of backing up VMware vSphere VMs has been discussed at length,
little attention has been paid to ESXi host configuration backup. A failure to backup the ESXi
host configuration can trigger a number of consequences, ranging from minor to grave.

Even if you manage to mitigate the effects of ESXi host breakdown, reconfiguring it is neither
the most enjoyable nor the most practical use of your time. For this reason, you are advised to
keep your ESXi host configuration safe and recoverable at all times. Ahead is a guide that will
help you achieve that goal.

The guide covers backup and restoration of VMware ESXi hosts using 4 methods: ESXi
command line, vSphere CLI, PowerCLI, and manually.

57 Steps total
Step 1: Method 1: Using ESXi Command Line to Back Up
ESXi Hosts
The use of the ESXi command line to backup up ESXi hosts does not require the installation
of additional software. To run the commands, enable ESXi Shell and connect to a target ESXi
host via SSH.

Step 2: Back up an ESXi host manually


To ensure that the configuration of the target ESXi host is synchronized with persistent
storage, run the following command:

vim-cmd hostsvc/firmware/sync_config

Step 3: Run this command


Expand

To back up ESXi configuration, run this command:

vim-cmd hostsvc/firmware/backup_config

The command will produce a link for downloading the configBundle.tgz archive.

Note that you have to replace the asterisk in the provided link with your IP/FQDN.
Alternatively, access the backup file in the /scratch/downloads directory, where it is stored as
configBundle-HostFQDN.tgz.

Step 4: Automate the backup of ESXi configuration


Create a datastore directory for backup files:

mkdir /vmfs/volumes/datastore1/ESXi_backup

Step 5: Write a script


Write a script to back up ESXi configuration

vi /vmfs/volumes/datastore1/ESXi_backup/esxi_backup.sh

Step 6: Add lines to the script

Expand

Add these lines to the script:

vim-cmd hostsvc/firmware/sync_config vim-cmd hostsvc/firmware/backup_config find


/scratch/downloads/ -name *.tgz -exec cp {}
/vmfs/volumes/datastore1/ESXi_backup/ESXi_config_backup_$(date +’%Y%m%d_%H%M
%S’).tgz \;

Step 7: Save the changes and quit the vi editor


Save the changes and quit the vi editor:
:wq

Step 8: Mark the script as executable


Mark the script as executable:

chmod +x /vmfs/volumes/datastore1/ESXi_backup/esxi_backup.sh

Step 9: Navigate to the script’s directory


Navigate to the script’s directory:

cd /vmfs/volumes/datastore1/ESXi_backup/

Step 10: Run the script


Run the script:

./esxi_backup.sh

Step 11: Check the creation of a backup file

Expand

Check the creation of a backup file:

ls -al

Step 12: Edit the scheduler’s configuration


Edit the scheduler’s configuration:

vi /var/spool/cron/crontabs/root
Step 13: To perform a daily backup of ESXi configuration
at 3:15 AM
To perform a daily backup of ESXi configuration at 3:15 AM:

15 03 * * * /vmfs/volumes/datastore1/ESXi_backup/esxi_backup.sh

Step 14: Save the changes in the read-only file

Expand

Save the changes in the read-only file:

:wq!

Alternatively, change the read-only status of the file:

chmod +w /var/spool/cron/crontabs/roo

Step 15: Recover ESXi configuration in ESXi command


line
Before taking the first step, ensure that the ESXi version, build number, and UUID of the
target host match the version, build number, and UUID of the ESXi configuration that needs
to be recovered.

Then, connect to the target ESXi host via SSH and put the host into maintenance mode:

esxcli system maintenanceMode set –enable yes

or

vim-cmd hostsvc/maintenance_mode_enter

Step 16: Сopy the archive with the ESXi configuration to


the target ESXi host directory

Expand
Use an SCP client to copy the archive with the ESXi configuration (configBundle-xxxx.tgz)
to the target ESXi host directory.

Step 17: Rename the configBundle-xxxx.tgz file


Rename the configBundle-xxxx.tgz file to configBundle.tgz:

mv /tmp/configBundle-esxi6-7b.localdomain.tgz /tmp/configBundle.tgz

Step 18: Recover the ESXi configuration

Expand

Recover the ESXi configuration:

vim-cmd hostsvc/firmware/restore_config /tmp/configBundle.tgz

The ESXi host will restart automatically.

Step 19: Exit the maintenance mode


Exit the maintenance mode:

esxcli system maintenanceMode set –enable no

or

vim-cmd hostsvc/maintenance_mode_exit

Step 20: Method 2: Back Up ESXi Hosts with vSphere CLI


You can back up the target ESXi host remotely using vSphere CLI. The advantage of this
method is that you can run the administration commands from any server.

Step 21: Install and configure vCLI


Download vSphere CLI package: https://code.vmware.com/web/tool/6.7/vsphere-cli

Extract the downloaded file: tar -zxvf VMware-vSphere-CLI-6.5.0-4566394.x86_64.tar.gz


Step 22: Install the required set of packages
Install the required set of packages:

apt-get install -y perl-doc libmodule-build-perl libssl-dev libxml-libxml-perl libsoap-lite-perl


libuuid-perl build-essential

and

apt-get install ia32-libs build-essential gcc uuid uuid-dev perl libssl-dev perl-doc liburi-perl
libxml-libxml-perl libcrypt-ssleay-perl

Step 23: Run the installer from the directory where the
downloaded file is unpacked

Run the installer from the directory where the downloaded file is unpacked:

./vmware-install.pl

When prompted to accept the license agreement, type yes and press Enter. If prompted to
install additional modules, type yes and press Enter.

Step 24: Enter the interactive shell of Perl


To install the additional modules manually, enter the interactive shell of Perl:

perl -MCPAN -e shell

Step 25: Run the following command


Run the following command to install the required Perl module:

install Devel::StackTrace

Step 26: Save a thumbprint to connect to ESXi hosts


Navigate to the directory:

cd /usr/lib/vmware-vcli/apps/general/
Step 27: Save a thumbprint

Expand

Save a thumbprint:

/usr/lib/vmware-vcli/apps/general/credstore_admin.pl add -s server -t thumbprint

Step 28: Add login credentials to a credential store


A credential store is the vicredentials.xml file located in vmware subdirectory of user
directory.

Add login credentials:

/usr/lib/vmware-vcli/apps/general/credstore_admin.pl add –server 192.168.101.208 –


username root

Step 29: Check whether your credentials have been saved


Check whether your credentials have been saved:

/usr/lib/vmware-vcli/apps/general/credstore_admin.pl list

Step 30: Place the host into maintenance mode

Expand

Place the host into maintenance mode without entering login credentials:

esxcli –server 192.168.101.208 –credstore /root/.vmware/credstore/vicredentials.xml system


maintenanceMode get

The credentials of non-root users are located in

/home/user_name/.vmware/credstore/vicredentials.xml

Step 31: Back up ESXi host configuration using vSphere


CLI

Expand

Navigate to the /usr/bin/ directory and run the following command:

vicfg-cfgbackup –server 192.168.101.208 –credstore


/root/.vmware/credstore/vicredentials.xml -s /backup/192-168-101-208-esxi-cfg.tgz
Important: vicfg- commands are deprecated; therefore, use ESXCLI equivalent commands to
backup ESXi hosts.

Step 32: Automate ESXi host configuration


Create the esxi_backup.sh file:

vim /backup/esxi_backup.sh

Step 33: Add the following lines to the esxi_backup.sh file


Add the following lines to the esxi_backup.sh file:

!/bin/bash

ESXI101_208_BACKUP_NAME=”192-168-101-208-$(date +’%Y%m%d_%H%M
%S’)esxi-cfg.tgz” vicfg-cfgbackup –server 192.168.101.208 –config /backup/vcli-config -s
/backup/$ESXI101_208_BACKUP_NAME

Step 34: Save the file and quit vim editor


Save the file and quit vim editor:

:wq

Step 35: Mark the file as executable


Mark the file as executable:

chmod +x /backup/esxi_backup.sh

Step 36: Edit a crontab file


Edit a crontab file to run the ESXi configuration backup command automatically:

crontab -e

Note: if you are editing a crontab file for the first time, select the default text editor, e.g. vim.

Step 37: To perform a daily backup of ESXi configuration


at 6:50 AM

Expand

To perform a daily backup of ESXi configuration at 6:50 AM:

50 06 * * * /backup/esxi_backup.sh
Step 38: List directory contents

Expand

List directory contents to check whether ESXi configuration was backed up at 6:50 AM:

ls –al

Note that the ESXi session timeouts automatically after a reboot and every 30 minutes.

Step 39: Recover ESXi host configuration in vSphere CLI

Expand

Enable remote access via SSH and ensure that the ESXi server’s IP address is known. Then,
run a command of the following format:

vicfg-cfgbackup –server=ESXi_host_IP_address –username=root -l backup_file

For example:

vicfg-cfgbackup –server=192.168.101.208 –username=root -l /backup/192-168-101-208-esxi-


cfg.tgz
The UUID of the target ESXi host should match the UUID of the backed up ESXi host. Use
the –force key to skip the UUID check.

Step 40: Method 3: Back up ESXi hosts with PowerCLI


Windows users can backup configurations of ESXi hosts with PowerCLI, which is a set of
PowerShell cmdlets.

Step 41: Back up the ESXi host

Expand

Run PowerCLI as an administrator and connect to the target ESXi server with a command of
the following format:

Connect-VIServer ESXi_IP_address -user user_name -password your_password

For example:

Connect-VIServer 192.168.101.208 -user root -password TestPass_555

Get-VMHostFirmware -vmhost 192.168.101.208 -BackupConfiguration -DestinationPath


“C:\backup ESXi host”

Alternatively, connect to vCenter Server and enter your login credentials:

Connect-VIServer 10.10.10.16

Step 42: Back up ESXi configuration of your hosts


Expand

Back up ESXi configuration of your hosts:

Get-VMHostFirmware -vmhost 10.10.10.61 -BackupConfiguration -DestinationPath


“C:\backup ESXi host”

Get-VMHostFirmware -vmhost 10.10.10.46 -BackupConfiguration -DestinationPath


“C:\backup ESXi host”

Step 43: Restore ESXi configuration in PowerCLI


Run PowerCLI as an administrator and connect to the target ESXi host. Then put the host into
the maintenance mode:

Set-VMHost -VMHost 192.168.101.208 -State Maintenance

Step 44: Restore the ESXi configuration


Expand

Important: Before restoring the ESXi configuration, migrate or power off VMs on the target
ESXi host.

Restore the ESXi configuration:

Set-VMHostFirmware -vmhost 192.168.101.208 -Restore -SourcePath “C:\backup ESXi


host\configBundle-192.168.101.20 8.tgz” -HostUser root -HostPassword TestPass_555

Note that the UUID of the target ESXi host should match the UUID of the backed up ESXi
host. Use the –force key to skip the UUID check.

Step 45: Method 4: Restore ESXi Hosts Manually


Resort to manual backup when you cannot establish a connection with an ESXi host or an
ESXi host fails to boot.

Step 46: Copy the backup archive to a USB flash drive and
connect it to the target ESXi host
Copy the backup archive, previously created with ESXi shell, vSphere CLI or PowerCLI, to a
USB flash drive and connect it to the target ESXi host.

Step 47: Boot from the Ubuntu live CD/DVD


Boot from the Ubuntu live CD/DVD on the machine running on ESXi host.
Step 48: Open the terminal in Linux and list the partitions

Expand

Open the terminal in Linux and list the partitions:

ls -al /dev/sd* fdisk -l | grep /dev/sda

Step 49: Create a directory for mounting the required disk


partition
Create a directory for mounting the required disk partition:

mkdir /mnt/sda5

Step 50: Mount the partition to the newly-created


directory
Mount the partition to the newly-created directory:

mount /dev/sda5 /mnt/sda5

Step 51: Copy the ESXi configuration backup archive


from the USB flash drive to the /tmp/ directory
Copy the ESXi configuration backup archive (configBundle.tgz) from the USB flash drive to
the /tmp/ directory of Ubuntu loaded from the live CD/DVD:
cp /media/Ubuntu/Ubuntu\ 18.0/backup_ESXi_host/configBundle-192-168-101-208.tgz
/tmp/configBundle.tgz

Step 52: Extract data from the backup archive


Extract data from the backup archive:

tar zxvf /tmp/configBundle.tgz

Step 53: Check whether the state.tgz file is extracted


Check whether the state.tgz file is extracted.

ls -al /tmp/

Step 54: Locate the state.tgz file and rename it


In the /mnt/sda5 directory, access /dev/sda5 partition, locate the state.tgz file and rename it
with the following command:

mv /mnt/sda5/state.tgz /mnt/sda5/state-old.tgz

Step 55: Copy the state.tgz file extracted from the backup
archive to the /tmp/directory
Copy the state.tgz file extracted from the backup archive to the /tmp/directory:

cp /tmp/state.tgz /mnt/sda5/

Step 56: Unmount mounted partitions


Unmount mounted partitions:

umount /dev/sda5/

Step 57: Reboot the server


Reboot the server, remove the Ubuntu live CD/DVD and boot the disk on which ESXi is
installed:

init 6

To attain and maintain a smooth operational workflow and access to mission-critical data, use
a reliable backup solution – NAKIVO Backup & Replication:
https://www.nakivo.com/vmware-backup

The seamless integration with VMware vSphere allows NAKIVO Backup & Replication to
back up and recover VMs, applications and data within minutes:
https://www.nakivo.com/resources/download/trial-download

You might also like