Data Centers

How to automate VirtualBox snapshots with the VBoxManage command

If you're looking for a way to automate VirtualBox virtual machine snapshots, learn how to use a bash script and cron to make this happen.

Image: Jack Wallen

Your data center might be powered by a mixture of technologies. Chances are, virtual machines (VMs) will be in that mix.

If you happen to be taking advantage of VirtualBox, you know there are plenty of tools to make that experience more flexible and powerful. Some of those tools require a bit of command line and/or scripting prowess on your part. One such instance is the automation of snapshots.

Snapshots are an incredibly important aspect of your VirtualBox landscape. With snapshots, you can roll back issues such as a security issue, data loss, or a broken update.

If you're running VMs, you don't want to have to remember to go back and take a snapshot on a regular basis. With the help of the VBoxManage command, you can use scripting magic to make taking snapshots a no-brainer.

Before I walk through this process, know that:

  • I'm using Elementary OS Loki as my host;
  • I'm going to automate the snapshotting of a single VM called UBUNTUSERVER_WEB; and
  • This should not be considered a backup solution.

Let's get to work.

SEE: Virtualization Policy (Tech Pro Research)

The script

The first thing we must do is cobble together a script that will be used by cron to work the backup magic. This will be a very simple script to back up a single VM, though you can modify this script to suit your needs. You can also create a backup script for each of your VMs that require snapshotting.

The contents of my script (to back up a VM called UBUNTUSERVER_WEB) look like this:

​NOW=`date +"%m-%d-%Y%T"`
​SNAPSHOT_DESCRIPTION="Snapshot taken on $NOW"


The above script:

  • creates a timestamp;
  • names the snapshot with the VM name with an appended timestamp;
  • gives the snapshot a description; and
  • runs the VBoxManage snapshot command using the above defined variables.

Once you have the contents of the script massaged to perfectly fit your needs, save and close the file (we'll name it vboxscript, for our example) and then give it executable permission with the command chmod u+x vboxscript. You're ready to move on to the schedule part of the program.

Cron that script

We're going to set this up to run daily, but you can set it up to run as often as you like. This builds up a LOT of snapshots, so you might want to alter that to take weekly snapshots, or go ahead with the daily option and know that you'll need to go into the VirtualBox GUI and merge the snapshots by creating a clone (we'll cover this at a later date).

From a terminal window, let's move your script (vboxscript) to a location the VirtualBox user will have permission to access. If this is a standard user, you might place that script in a new directory, such as ~/.scripts. We'll assume you run VirtualBox as a standard user and create that folder with the command mkdir ~/.scripts. Now move vboxscript into that folder with the command mv vboxscript ~/.scripts. We'll create the cron job to run the script every day at 1:00 AM. Issue the command (as the user that runs VirtualBox) crontab -e and add the following to the crontab file:

0 1 * * * ~/.scripts/vboxscript

Once you save that file, the scheduling will be set, and a new snapshot will be created at 1:00 AM. That's it—the automation of VirtualBox snapshots is now working for that particular VM.

You can easily create scripts for each VM you run. Just remember that it will be important to regularly clone the VM; otherwise, you'll wind up with an inordinate number of snapshots.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website

Editor's Picks