warm_vm_migration.sh
#!/bin/bash
 
if [[ -n "$1" ]]; then vm="$1"; else echo "Usage: $(basename "$0") vm_name"; exit 1; fi
 
src="server1"; dst="server2"
 
############# Source server
srv="$src"
vmline="$(ssh "root@$srv" vim-cmd vmsvc/getallvms|grep "^[0-9]\+ \+$vm \+")"
echo "$vmline"
if [[ -z "$vmline" ]]; then echo "VM $vm not found. Exiting"; exit 1; fi
vmid="$(awk '{print $1}' <<<"$vmline")"
vmds="$(awk '{print $3}' <<<"$vmline"|tr -d '[]')"
vmfile="$(awk '{print $4}' <<<"$vmline")"
 
vmstate="$(ssh "root@$srv" vim-cmd vmsvc/power.getstate $vmid| tail -n1)"
if [[ "$vmstate" == "Powered on" ]]
 then
  ssh "root@$srv" vim-cmd vmsvc/power.suspend $vmid
  ssh "root@$srv" vim-cmd vmsvc/power.getstate $vmid| tail -n1 #Suspended
 else
  echo "VM is not powered on! Exiting..."; exit 0
fi
ssh "root@$srv" vim-cmd vmsvc/unregister $vmid
 
############# Destination server
srv="$dst"
vmid2="$(ssh "root@$srv" vim-cmd solo/registervm /vmfs/volumes/$vmds/$vmfile)"
ssh "root@$srv" vim-cmd vmsvc/getallvms|grep "^[0-9]\+ \+$vm \+"
ssh "root@$srv" vim-cmd vmsvc/power.on "$vmid2" &
sleep 5
ssh "root@$srv" vim-cmd vmsvc/message "$vmid2"
#Virtual machine message 735221:
#This virtual machine might have been moved or copied. In order to configure certain management and networking features, VMware ESX needs to know if this virtual machine was moved or copied. If you don't know, answer "I Copied It".
#   0. button.uuid.cancel (Cancel)
#   1. button.uuid.movedTheVM (I Moved It)
#   2. button.uuid.copiedTheVM (I Copied It) [default]
msgid="$(ssh "root@$srv" vim-cmd vmsvc/message "$vmid2"|sed -rne 's/(^Virtual machine message )([0-9]+)(:$)/\2/p')"
echo "Virtual machine message $msgid answer 1"
ssh "root@$srv" vim-cmd vmsvc/message "$vmid2" $msgid 1
ssh "root@$srv" vim-cmd vmsvc/power.getstate $vmid2| tail -n1