Skip to content

Instantly share code, notes, and snippets.

@redhoodie
Last active August 29, 2015 14:11
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 redhoodie/54989a4f2af22b94e8e2 to your computer and use it in GitHub Desktop.
Save redhoodie/54989a4f2af22b94e8e2 to your computer and use it in GitHub Desktop.
#! /bin/sh
# /etc/init.d/vbox
#Edit these variables!
VMUSER=VMAdmin
VMNAME="cdb62186-7c30-4c25-a0b0-e4a32cfb0504"
BASEFOLDER=/home/VMAdmin/path/to/backups/
case "$1" in
start)
echo "Starting VirtualBox VM..."
sudo -H -u $VMUSER VBoxManage startvm "$VMNAME" --type headless
;;
reset)
echo "Resetting VirtualBox VM..."
sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" reset
;;
stop)
echo "Saving state of Virtualbox VM..."
sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" savestate
;;
shutdown)
echo "Shutting down Virtualbox VM..."
sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" acpipowerbutton
;;
status)
sudo -H -u $VMUSER VBoxManage list vms -l | grep -e ^"$VMNAME": -e ^State | sed s/\ \ //g | cut -d: -f2-
;;
backup)
echo ""
sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" acpipowerbutton
echo "Waiting for VM "$VMNAME" to poweroff..."
until $(sudo -H -u $VMUSER VBoxManage showvminfo --machinereadable "$VMNAME" | grep -q ^VMState=.poweroff.)
do
sleep 1
done
FILENAME=$(date +"%Y_%m_%d-%T")
echo "Backing up Virtualbox VM to '$BASEFOLDER$FILENAME'..."
sudo -H -u $VMUSER VBoxManage clonevm "$VMNAME" --options keepallmacs --name $FILENAME --basefolder $BASEFOLDER
echo "Restarting VirtualBox VM..."
sudo -H -u $VMUSER VBoxManage startvm "$VMNAME" --type headless
echo ""
;;
*)
echo "Usage: sudo service vbox {start|stop|status|shutdown|reset|backup}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment