Skip to content

Instantly share code, notes, and snippets.

@f1sherman
Last active August 29, 2015 14:13
Show Gist options
  • Save f1sherman/c3bfd69600ce81e3d982 to your computer and use it in GitHub Desktop.
Save f1sherman/c3bfd69600ce81e3d982 to your computer and use it in GitHub Desktop.

Revisions

  1. f1sherman revised this gist Feb 9, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup-fusion.sh
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ 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 *
    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"
  2. f1sherman created this gist Jan 11, 2015.
    35 changes: 35 additions & 0 deletions backup-fusion.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/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 *

    # *** 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