Automating VirtualBox Snapshots

biblical_apologetics_degree_wideI depend a lot upon VirtualBox for my security-related research and testing. That being the case I make a lot of changes to my VirtualBox VM’s and losting a given state and not being able to rollback to last known good state would be very bad. Yes, you can take snapshots manually via the GUI or even by the means of the CLI. When you have over 20 VM’s that you manage this can be pain in the butt.

This is where scripting comes in, so I built some simple Bash scripts to automate this process and have it run hourly via Cron.



The first script simply outputs to STDOUT a list of all the VirtualBox VM’s in the system:

vboxmanage list vms 

This will simply produce the name and registration number of each VM you have defined on the system.

Now to automate the snapshot process we simply craft something like:

for i in `vmlist | awk '{print $1}' | perl -pi.orig -e 's/\"//g'`
      echo "Creating snapshot for $i"
      vboxmanage snapshot $i take $i-`date +%Y%m%d%H%M%S`

This will create a snapshot for each VM with the snapshot name of each VM followed by a date/time stamp. Put this script in your crontab and your good to go.