Home Notes Other VirtualBox script: automatic snapshot

VirtualBox script: automatic snapshot E-mail
Thursday, 16 June 2011 20:52


The original code (below) fails if the current hour is less than 2 digits long, i.e., earlier than 10 AM.  This is the fix:

cd "C:\Program Files\Oracle\VirtualBox
set current_hour=%time:~0,2%
if %current_hour% lss 10 (set current_hour=0%time:~1,1%)
set backup_detailed=%date:~10,4%_%date:~7,2%_%date:~4,2%__%current_hour%_%time:~3,2%_%time:~6,2%
VBoxManage snapshot "<virtual machine name>" take %backup_detailed%
VboxManage startvm "<virtual machine name>"



VirtualBox is great for experimenting, specifically with its snapshot feature that lets you easily revert to prior states.  I like to experiment frequently, and often my experiments break the virtual machine.  For moments like that I wish I had a script that automatically made a snapshot of a VM before it launches.


For my Windows 7 host machine, placing the following code in a bat (batch) file does the trick:

cd "C:\Program Files\Oracle\VirtualBox
set backup_detailed=%date:~10,4%_%date:~7,2%_%date:~4,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%
VBoxManage snapshot "<virtual machine name>" take %backup_detailed%
VboxManage startvm "<virtual machine name>"

This will create a virtual machine titled with a timestamp, such as 2011_16_06__18_46_50, and then launch the VM.



Adding timestamps in a DOS batch file

Using timestamps in batch files

Automatically create snapshot before launching Virtualbox VM?