Monday, 27 September 2010

Backup of VMWare ESXi virtual machnes

Topic imageTask: To perform regular automated backup of virtual machines on freeware ESXi 4.x.

VM Host preparation

  • On console press ALT-F1
  • Enter unsupported and press Enter
  • login
  • Read warning message and skip it.
#vi /etc/inetd.conf
  • uncomment the line with SSH and TCP
  • Reboot the server
  • login to server by ssh remotely
Instead of VMS please insert your datastore name
#mkdir /vmfs/volumes/VMS/scripts
#cd vmfs/volumes/VMS/scripts
wget http://dakeung.com/wp-content/uploads/2009/09/GhettoVCB.sh(external link)
or just copy it to host using vSphere client
#chmod +x GhettoVCB.sh

Preparation of NFS server for backup datastorage

Install necessary packages:

#yum install portmap nfs-utils

Create export folder:

#mkdir /backup/esxi-backup

Modify /etc/exports:

/etc/exports
/backup/esxi-backup 192.168.1.0/255.255.255.0 (rw,sync,no_root_squash) 

Modify file to restrict access to NFS server:

#vi /etc/hosts.deny

Add the following line to the empty file:

ALL
Note: We are closing access for all hosts except listed in the /etc/hosts.allow
In the /etc/hosts.allow list all hosts/networks which will have access:
192.168.1.0/24

Start necessary services:

#/etc/init.d/portmap start
#/etc/init.d/nfs start

Check that all services started:

#/etc/init.d/portmap status
#/etc/init.d/nfs status

Add NFS server export folder as datastore:

---------Extract from ESXi Configuration Guide
Create an NFS-Based Datastore
You can use the Add Storage wizard to mount an NFS volume and use it as if it were a VMFS datastore.
Prerequisites
Because NFS requires network connectivity to access data stored on remote servers, before configuring NFS,
you must first configure VMkernel networking.
Procedure:
1 Log in to the vSphere Client and select the host from the Inventory panel.
2 Click the Configuration tab and click Storage in the Hardware panel.
3 Click Datastores and click Add Storage.
4 Select Network File System as the storage type and click Next.
5 Enter the server name, the mount point folder name, and the datastore name.
NOTE When you mount the same NFS volume on different hosts, make sure that the server and folder
names are identical across the hosts. If the names do not match exactly, for example, if you enter share as
the folder name on one host and /share on the other, the hosts see the same NFS volume as two different
datastores. This might result in a failure of such features as vMotion.
6 (Optional) Select Mount NFS read only if the volume is exported as read only by the NFS server.
7 Click Next.
8 In the Network File System Summary page, review the configuration options and click Finish.
---------------- End of extract
Assign our NFS datastore name backup

Modify file GhettoVCB.sh:

Connect to ESXi server and execute the following commands:
#ssh 192.168.1.1
#cd /vmfs/volumes/VMS/scripts
#ls /vmfs/volumes/
Check backup path.

According to above information modify backup script:

# vi GhettoVCB.sh
Change appropriate lines to:
VM_BACKUP_VOLUME=/vmfs/volumes/backup
NFS_SERVER=our_nfs_server_fqdn
NFS_MOUNT=/backup/esxi-backup
ENABLE_COMPRESSION=1 # In my case it doesn't work!!!!!

If you want to maintain your files without compression. Change this to =0
Save file.

Prepare vm list to backup:

Make file with virtual machines to backup:
/backup # touch vmserver.list
Add virtual machines to list:
/backup # vi vmserver.list

All virtual machines have to have VMware Tools installed and started.

Start test backup manually:
/backup # ./GhettoVCB.sh vmserver.list

Automatically start backup of virtual machines by schedule:
Change mode for temporary crontab file:
#chmod u+w /var/spool/cron/crontabs/root
Add task to perform backup every day at 19 (In my case ESXi local time is GMT time instead of my local time.):
#vi /var/spool/cron/crontabs/root
To perform backup every Friday at 00:00 add to the end of file the following line:
0 0 * * 5 /backup/ghettoVCB.sh /backup/vmserver.list > /backup/ghettoVCB-backup-$(date +\%s).log
Restart crontab:
# kill $(cat /var/run/crond.pid)
# busybox crond

Make changes permanent (Temporary crontab file changes after every reboot):
#vi /etc/rc.local
Add the following lines:
/bin/kill $(cat /var/run/crond.pid)
/bin/echo "0 19 * * 5 /vmfs/volumes/VMS/scripts/ghettoVCB.sh -f /vmfs/volumes/VMS/scripts/vmserver.list > /vmfs/volumes/VMS/scripts/ghettoVCB-backup-\$(date +\\%s).log" >>
/bin/busybox crond


Useful links:
http://dakeung.com/2009/09/28/how-to-backup-esxi-4-0-virtual-machines/
http://communities.vmware.com/docs/DOC-8760

2 comments:

smithj10 said...

This is very cool stuff, and everything worked like a charm till I got the the yum step. It would appear that my ESXi doesn't have yum installed.

Can you offer any suggestions?

Andrey Ivanov said...

Hi, I mentioned yum only for NFS server, which I assumed is located on other machine. ESXi itself does not have you installed.