How to copy VMs from one ESXi to another in free license

Using OVFTool from VMWARE you are able to copy VMs from one ESXi to another without vCenter. This script is used on our test environment to make a sort of disaster recovery.

It’s a bash shell script (I used a Linux machine as management host) but I think is not a big work to translate it in  powershell and run it on a windows machine.
The script need SSH service running on ESXi host because on “license free” is not possible to remotely manage ESXi hosts. Is also necessary to have installed “sshpass”

Main script (schedule this script on Linux machine):

#!/bin/bash
path_sshpass="/userhome/media/DISK_A63AB3C0-8E73/bin/"
path_ovf="/userhome/media/DISK_A63AB3C0-8E73/bin/ovf/"
path_script="/userhome/media/DISK_A63AB3C0-8E73/script/"

ESX_Server_Source="2001:DB8::10"
ESX_User_Source="your user"
ESX_Password_Source="your password"
ESX_Server_Target="2001:DB8::11"
ESX_User_Target="your user"
ESX_Password_Target="your password"
VMList="VM01TEST VM02TEST VM03TEST"

LOG="/userhome/media/DISK_A63AB3C0-8E73/script/esx_replica.log"

echo "$(date '+%Y-%m-%d %H:%M') # Starting Job" |tee $LOG

for VMNAME in $VMList
do
        echo "$(date '+%Y-%m-%d %H:%M') # Cloning $VMNAME ###################" |tee -a $LOG

        ESX_ssh=$ESX_User_Source"@"$ESX_Server_Source
        ovf_Source="vi://"$ESX_User_Source":"$ESX_Password_Source"@["$ESX_Server_Source"]/"$VMNAME
        ovf_Target="vi://"$ESX_User_Target":"$ESX_Password_Target"@["$ESX_Server_Target"]"
        sed_cmd="s/Patata/"${VMNAME}"/g"

        echo "$(date '+%Y-%m-%d %H:%M') # Running esx_vmStop" |tee -a $LOG
        STATE=$(sed ${sed_cmd} ${path_script}esx_vmStop.sh | ${path_sshpass}sshpass -p ${ESX_Password_Source} ssh ${ESX_ssh})
        echo "# VM state: $STATE"

        if [ "$STATE" = "Powered off" ]; then
                echo "$(date '+%Y-%m-%d %H:%M') # Running OVF..." |tee -a $LOG
                ${path_ovf}ovftool --noImageFiles --overwrite --powerOffTarget -ds="datastore1" ${ovf_Source} ${ovf_Target}
                OVF_Error=$?
                echo "$(date '+%Y-%m-%d %H:%M') # OVF exit: $OVF_Error" |tee -a $LOG

                echo "$(date '+%Y-%m-%d %H:%M') # Running esx_vmStart" |tee -a $LOG
                sed ${sed_cmd} ${path_script}esx_vmStart.sh | ${path_sshpass}sshpass -p ${ESX_Password_Source} ssh ${ESX_ssh}
        fi
done

esx_vmStop.sh script (script used by the main script under ssh for shutdown VMs):

#!/bin/ash
INTERVAL=60
VMNAME="Patata"

VMs=$(vim-cmd vmsvc/getallvms | grep -w ${VMNAME} | awk '{print $1}')
if [ -z "$VMs" ]; then
        echo "VM not found"
        exit
fi

STATE=$(vim-cmd vmsvc/power.getstate ${VMs} | grep -v "runtime")

if [ "$STATE" = "Powered off" ]; then
        echo $STATE
else
        vim-cmd vmsvc/power.shutdown $VMs
        sleep $INTERVAL
        STATE=$(vim-cmd vmsvc/power.getstate ${VMs} | grep -v "runtime")
        echo $STATE
fi

esx_vmStart.sh script (script used by the main script under ssh for start VMs):

#!/bin/ash
INTERVAL=60
VMNAME="Patata"

VMs=$(vim-cmd vmsvc/getallvms | grep -w ${VMNAME} | awk '{print $1}')
if [ -z "$VMs" ]; then
        echo "VM not found"
        exit
fi

STATE=$(vim-cmd vmsvc/power.getstate ${VMs} | grep -v "runtime")

if [ "$STATE" = "Powered off" ]; then
        vim-cmd vmsvc/power.on $VMs
else
        echo $STATE
fi

Copy this 3 script on management machine where is installed ovftool. Change variables according to your environment:
path_sshpass : path where is located sshpass
path_ovf :path where is located ovftool
ESX_Server_Source : IPv6 ipaddress of source ESXi Host
ESX_User_Source :user to use on ssh connection against source ESXi host
ESX_Password_Source :password of ESX_User_Source user
ESX_Server_Target : IPv6 ipaddress of target ESXi Host
ESX_User_Target :user to use on ssh connection against target ESXi host
ESX_Password_Target :password of ESX_User_Target user
VMList : list of VMs to copy from source ESXi to target ESXi (VM name separated with a space)
LOG : path where save the log

Notes:
If you are using ipv4, remove square bracket “[]” from the main script.
On windows, you can use “plink” instead of sshpass, or use public/private keys

I used a IGEL thin client (Linux-based) as “management machine”. Just copy from an UBUNTU 13.04 x32 machine the folder /usr/lib/VMware-ovftool/ and the /usr/bin/sshpass to IGEL machine (I put these stuffs and the script on a mounted usb key). It’s slow but it’s work!

Via:
http://www.virtuallyghetto.com/2012/06/how-to-copy-vms-directly-between-esxi.html
http://www.pjb.cc/?p=219

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s