Skip to content

Instantly share code, notes, and snippets.

@f1sherman
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f1sherman/c3bfd69600ce81e3d982 to your computer and use it in GitHub Desktop.
Save f1sherman/c3bfd69600ce81e3d982 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Fail this script if any commands fail (-e) or if any uninitialized variables are referenced (-u)
set -e -u
# Set the following variables based on your environment
VMX='/path/to/file.vmx'
BACKUP_DIR='/path/to/backup/directory'
VMRUN='/Applications/VMware Fusion.app/Contents/Library/vmrun'
NUM_BACKUPS_TO_KEEP=3
# Make sure the VM gets started even if the backup fails
trap startvm EXIT
START_VM=false
function startvm {
if $START_VM; then # only start the VM if it was running when we began the backup
"$VMRUN" start "$VMX"
fi
}
# *** Shut down the VM if it is running ***
if "$VMRUN" list | grep --quiet "$VMX"; then
START_VM=true
"$VMRUN" stop "$VMX" soft
fi
# *** Create the backup file ***
VMWAREVM="${VMX%/*}" # Remove the .vmx filename to get the full path to the .vmwarevm bundle
cd "$VMWAREVM"
tar -czpf "$BACKUP_DIR"/fusion-backup-`date +%Y%m%d-%H%M%S`.tar.gz --exclude '*.vmem' *
# *** Delete all but the most recent backup files ***
cd "$BACKUP_DIR"
(ls -t | head -n $NUM_BACKUPS_TO_KEEP; ls) | sort | uniq -u | xargs rm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment