Skip to content

Instantly share code, notes, and snippets.

@tpowellcio
Created May 24, 2016 12:42
Show Gist options
  • Save tpowellcio/4f406876c1948ea9447a905af8c4e6cc to your computer and use it in GitHub Desktop.
Save tpowellcio/4f406876c1948ea9447a905af8c4e6cc to your computer and use it in GitHub Desktop.
ESXi Backup VMs
#!/bin/sh
SOURCE=$1/*
TARGET=$2
if [ $# != 2 ]; then
echo "Usage: $(basename $0) <SOURCE VM PATH> <DESTINATION PATH>"
echo "Example: $(basename $0) /vmfs/volumes/datastore1/VM1 /vmfs/volumes/datastore2"
exit
fi
for file in $SOURCE; do
vmx=$(basename $file/*.vmx)
name=$(grep displayName $file/$vmx | /bin/awk -F\" '{print $(NF-1)}')
vmxf=$(grep vmxf $file/$vmx | /bin/awk -F\" '{print $(NF-1)}')
nvram=$(grep nvram $file/$vmx | /bin/awk -F\" '{print $(NF-1)}')
vmdks=$(grep vmdk $file/$vmx | /bin/awk -F\" '{print $(NF-1)}')
echo "Started copying VM $name"
vmdir=$(basename $file)
destpath="$TARGET/$vmdir"
echo "Source path: $1"
echo "Destination path: $destpath"
echo "Creating destination path $destpath"
/bin/mkdir -p $destpath
echo "Copying configuration files:"
echo $vmx
/bin/cp $file/$vmx $destpath
echo $vmxf
/bin/cp $file/$vmxf $destpath
echo $nvram
/bin/cp $file/$nvram $destpath
echo "Copying virtual disks:"
for vmdk in $vmdks;
do
echo $vmdk
/sbin/vmkfstools -d thin -i $file/$vmdk $destpath/$vmdk
done
echo "Completed copying VM $name"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment