Skip to content

Instantly share code, notes, and snippets.

@tpowellcio
Created May 24, 2016 12:45
Show Gist options
  • Save tpowellcio/00cc5e6fddd473b18e2d9191d96c246d to your computer and use it in GitHub Desktop.
Save tpowellcio/00cc5e6fddd473b18e2d9191d96c246d to your computer and use it in GitHub Desktop.
Backup VMs using OVFTool
#!/bin/bash
SOURCE=$1/*
TARGET=/Volumes/VMBackup/$2
echo "*** source=$SOURCE"
echo "*** target=$TARGET"
OVFTOOL="/Applications/VMware\ Fusion.app/Contents/Library/VMware\ OVF\ Tool/ovftool"
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 folder in $SOURCE; do
folder=$folder
#if [ ${#file} < 17] && [ $file != "filerbkps" ]; then
echo "folder=$folder"
folder=$folder
FILES=$folder/*
echo "files=$FILES"
for file in $FILES; do
if [[ "$file" == *"vmx" ]]; then
#file=$(basename $file)
echo "file = $file"
#name=$(grep displayName $file | tr -d '"')
#echo "name=$name"
# 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 $file"
filename=$(basename $file .vmx)
destpath="$TARGET"
echo "Source path: $file"
echo "Destination path: $destpath"
if [ ! -d "$destpath" ]; then
echo "Creating destination path $destpath"
/bin/mkdir -p $destpath
fi
# 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 "Converting Virtual Machine to ova:"
echo ovftool "$file" "$destpath/$filename.ova"
eval $OVFTOOL "$file" "$destpath/$filename.ova"
echo "Completed copying VM $vmxs"
echo ""
echo ""
fi
done
#fi
done
@InsaneBeans
Copy link

thx for your share, but if i wanna backup the vm to remote server, how can i do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment