Backing Up VirtualBox

I've been playing with VirtualBox, a great open-source virtual machine. Mostly I've been deploying Windows guests on a Linux host. Right now I have two VMs; one for IE8 testing, and one to run MS SQL Server.

I have noticed that VirtualBox can corrupt a VM image if it gets shut down unexpectedly. Given that it takes a significant amount of time to install an OS and get the tools you need running, I wanted to backup my VMs.

I also wanted the backup to succeed even if the VM was running at the time. This is especially important for the SQL Server, as I'm running it as a service all the time. More on that in a future post...

Here is the script that I whipped up, aided by the excellent command-line tools that come with VirtualBox.


# start by discarding all snapshots

#from `VBoxManage list vms |grep "^\(Name\|UUID\)"

#from `VBoxManage list hdds |grep "^\(Location\|UUID\)"

# stop
VBoxManage controlvm $VM savestate

# backup (shrink happens automatically)
rm -f /home/chase/vms/backup/$VDI
VBoxManage clonehd $HD /home/chase/vms/backup/$VDI -remember

Note: You must discard all snapshots before running the backup. Otherwise, the backup will only get the pre-snapshot state.

I'm currently working at NerdWallet, a startup in San Francisco trying to bring clarity to all of life's financial decisions. We're hiring like crazy. Hit me up on Twitter, I would love to talk.

Follow @chase_seibert on Twitter