Skip to content

Instantly share code, notes, and snippets.

@cdwilson
Created November 20, 2012 20:27
Show Gist options
  • Save cdwilson/4120841 to your computer and use it in GitHub Desktop.
Save cdwilson/4120841 to your computer and use it in GitHub Desktop.
Clone a VM on VMware ESXi with a thin vmdk
#!/bin/sh
# modified from script found at http://serverfault.com/questions/311459/how-to-backup-virtual-machines-on-a-standalone-esxi-host to allow VMs with spaces in the file name
if [ $# != 2 ]; then
echo "Usage: $(basename $0) <SOURCE VM PATH> <DESTINATION PATH>"
echo
echo " 1) Only works with VMs with a single .vmdk"
echo " 2) VM must be powered off"
echo " 3) VM must have no active snapshots"
echo
echo "Example: $(basename $0) /vmfs/volumes/datastore1/VM1 /vmfs/volumes/datastore2"
exit
fi
vmx_path=$(/bin/ls "$1"/*.vmx)
vmx=$(basename "$vmx_path")
name=$(grep displayName "$1/$vmx" | /bin/awk -F\" '{print $(NF-1)}')
vmxf=$(grep vmxf "$1/$vmx" | /bin/awk -F\" '{print $(NF-1)}')
nvram=$(grep nvram "$1/$vmx" | /bin/awk -F\" '{print $(NF-1)}')
vmdk=$(grep vmdk "$1/$vmx" | /bin/awk -F\" '{print $(NF-1)}')
echo "Started copying VM '$name'"
vmdir=$(basename "$1")
destpath="$2/$vmdir"
echo "Source path: $1"
echo "Destination path: $destpath"
echo "Creating destination path '$destpath'"
/bin/mkdir -p "$destpath"
echo "Copying configuration files:"
echo "vmx: $vmx"
/bin/cp "$1/$vmx" "$destpath"
echo "vmxf: $vmxf"
/bin/cp "$1/$vmxf" "$destpath"
echo "nvram: $nvram"
/bin/cp "$1/$nvram" "$destpath"
echo "Copying virtual disk:"
echo "vmdk: $vmdk"
/sbin/vmkfstools -d thin -i "$1/$vmdk" "$destpath/$vmdk"
echo "Completed copying VM '$name'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment