VMWare ESXi – Automatic Backups to another ESXi host


So if you have VMWare´s free hypervisor ESXi ( 5.1 ) running virtual machines, you´re probably already using the “ghettoVCB” script from the VMWare community ( http://communities.vmware.com/docs/DOC-8760 ) to backup your VM´s.

Now let´s assume you have two (or more) virtual machines and want to extend availability and / or performance to a new level by using 2 ESXi Servers running each VM on a separate host , automatically backing up VM´s from one host to the other and vice versa.

The following howto gives you step-by-step instructions on how to set this up. If not described different, enter each step on each ESXi host before going to the next step!

ghettoVCB backups running

Local backups of VM´s must be successfully running, e. g. setup with the great ghettoVCB script ( Link ). There should at least be a manually run backup to be able to test the automatic copying.

Enable SSH access

If you have running the ghetto backup script the following steps to enable SSH access should sound familiar. We need to enable it to login with an SSH client (e. g. putty on Windows).

  • Login via vSphere client
  • open configuration tab
  • under security profile / services enter properties
  • start SSH and enable starting / stopping with the host
  • under firewall profile allow SSH server and client

File persistence after reboots

The free version license of ESXi wipes away almost every setting after rebooting, so we need ways to persist certain settings. Since ESXi 5.1 there is a file called/etc/rc.local.d/local.sh which persists through reboots an is executed on each system start, so we can use this file to call other scripts and make settings on each boot. We will also create a directory inside a datastore where we can for example place scripts that are called from the /etc/rc.local.d/local.sh file (replace DATASTORE to match the name of your local datastory directory for the rest of this article).

mkdir /vmfs/volumes/DATASTORE/bootup/

Modify bash prompt to show hostname

For better orientation we´ll modify bash promt to show the corresponding hostname.

vi  /vmfs/volumes/DATASTORE/bootup/.profile


PS1="`hostname | sed 's/\..*//'` # "

Add the following line to the file /etc/rc.local.d/local.sh and execute it once in the shell:

cp /vmfs/volumes/DATASTORE/bootup/.profile /

Now if you relogin bash prompt should show the hostname.

SSH login without password (key-based)

We need to be able to login without a password from one ESXi to another in order to perform automatic copying of VM´s to the other server. As before, perform each step on both servers before moving on.


Confirm 3 times, then copy public SSH key to other server

scp .ssh/id_rsa.pub IPFROMOTHERESXSERVER:/etc/ssh/keys-root/authorized_keys

Now login from both servers to both servers via SSH once and confirm warning with “yes”. This generates the file /.ssh/known_hosts. Next:

cp -r /.ssh/ /vmfs/volumes/DATASTORE/bootup/

Now copy file to persist through reboot:

cp /etc/ssh/keys-root/authorized_keys /vmfs/volumes/DATASTORE/bootup/

Modify /etc/rc.local.d/local.sh and add these two lines before exit 0:

cp -r /vmfs/volumes/DATASTORE/bootup/.ssh/ /
cp /vmfs/volumes/DATASTORE/bootup/authorized_keys /etc/ssh/keys-root/authorized_keys

Now you can login from one ESXi server to another without being asked for a password, persisting through reboots.

Shell script to copy VM´s and cron entry

On the source server (running the VM´s to backup) create a directory and a shell script. Example:

mkdir /vmfs/volumes/DATASTORE/scripts/
touch /vmfs/volumes/DATASTORE/scripts/sshcopy.sh
chmod +x /vmfs/volumes/DATASTORE/scripts/sshcopy.sh

Content of /vmfs/volumes/DATASTORE/scripts/sshcopy.sh. Source directory must exist, we assume the backups from ghettoVCB are there.

scp -r /vmfs/volumes/DATASTORE/backup/ IPFROMTARGETSERVER:/vmfs/volumes/DATASTORE/

Modify /etc/rc.local.d/local.sh again and enter (before exit 0):

/bin/kill $(cat /var/run/crond.pid)
/bin/echo "0 23 * * 1-5 /vmfs/volumes/DATASTORE/scripts/sshcopy.sh" >> /var/spool/cron/crontabs/root

Deleting old backups

To prevent the target datastore from running out of disk space we have to create a job ontarget server that deletes old backups. Create the file/vmfs/volumes/DATASTORE/scripts/del_vms.sh and enter the line (adjusted to your directory):


Make it executeable

chmod +x /vmfs/volumes/DATASTORE/scripts/del_vms.sh

Now we create a cron job. Adjust the time to ensure the job is running right before the target server starts copying the files. File /var/spool/cron/crontabs/root:

55 0 * * 2-6 /vmfs/volumes/datastore1/scripts/del_vms.sh

Restart cron service:

/bin/kill $(cat /var/run/crond.pid)

Additionally modify /etc/rc.local.d/local.sh to persist through reboots (adjust time, enter line before crond):

/bin/echo "55 0 * * 2-6 /vmfs/volumes/datastore1/scripts/del_vms.sh" >> /var/spool/cron/crontabs/root


Now restart both servers and check if everything works as intended as many errors only show up after rebooting. You should now have a free ESXi environment with automatic backups between hosts!