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.
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 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:
#!/bin/bash NOW=`date +"%m-%d-%Y%T"` SNAPSHOT_NAME="UBUNTUSERVER_WEB_$NOW" SNAPSHOT_DESCRIPTION="Snapshot taken on $NOW" VBoxManage snapshot UBUNTUSERVER_WEB take "$SNAPSHOT_NAME" --description "$SNAPSHOT_DESCRIPTION"
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.
- How to encrypt VirtualBox VMs (TechRepublic)
- How to improve VirtualBox guest performance in five steps (TechRepublic)
- How to share folders between guest and host in VirtualBox (TechRepublic)
- How to use snapshots in VirtualBox (TechRepublic)
- Microsoft Azure: Price cuts for virtual machines and storage services (ZDNet)