Your data center might depend on virtual machines (VMs), and you might use VirtualBox for some of those VMs. If that’s the case, what do you do when disaster strikes? Do you already have a plan set in motion for such an eventuality? You should. In fact, you must.

When something goes awry, you want to have a solid backup available. Of course, VirtualBox has a great snapshot tool and you could always make use of that. What if you want to create a completely automated backup system that would export your VMs (to .ova files) regularly?

Let’s make that happen.

Everyone’s situation is different, so your mileage may vary with this. Even so, what I’m about to show you will give you a good start on creating your very own VirtualBox VM backup plan.

SEE: MSP best practices: Server deployment checklist (TechRepublic Premium)

What you’ll need

To work through this, you’ll need a running instance of VirtualBox with at least one VM available to back up. You’ll also need a drive with plenty of space to back that VM up to.

How to create the backup

This backup will come by way of a bash script. It’s much easier than you think. The first thing we need to know is the name of the VM to be backed up. To find out the names of all your current VMs, issue the command:

vboxmanage list vms

In that list, you’ll find the VM you want to back up. The name will be in quotes, as in “Ubuntu Server 20.04,” or “Web01.” Let’s say you want to back up Web01 to an .ova file housed in an external drive mounted on /data. That command would be:

VBoxManage export "Web01" -o /data

Depending on how large the VM is, the command could take some time to run. When it completes, you should see the /data/Web01.ova file ready.

Before you issue the command, the machine must be powered off. The best way to do that is to power off the VM from within the guest. Using the VBoxManage poweroff command could cause data loss, which we want to avoid at all costs.

How do you pull this off? You have to do so via an SSH session like so:

ssh -t USER@HOST sudo poweroff

Where USER is the name of the remote admin user and HOST is the IP address of the VirtualBox guest VM. Of course, to make this work via a script, you’ll need to set up SSH key authentication. Make sure to copy your SSH key from the host to the guest with the command:

ssh-copy-id USER@HOST

Once you’ve taken care of that, you can then issue the poweroff command as listed above and then export the VM to the .ova file.

How to automate this

This is where it gets a bit tricky because in the automation you have to create three bash scripts:

  • One to power off the guest

  • One to export the VM

  • One to start the guest

The first script will power off the machine. Create the new script with the command:


In that file, paste the following:


ssh -t USER@HOST sudo poweroff

Where USER is the name of the remote admin user and HOST is the IP address of the VirtualBox guest VM.

Save and close the file.

Next, we’ll create the script to export the VM with the command:


In that file, paste the following


today=$(date +"%Y-%m-%d")
VBoxManage export "Web01" -o /data/Web01-${today}.ova

Remember to change the details to match your needs. Save and close the file. The above file will append today’s date in the file, so you won’t overwrite your previously exported OVAs.

Finally, we’ll create a script to start the virtual machine with the command:


In that file, paste the following contents (altering the contents to fit your needs):


VBoxManage startvm "Web01" --type headless

Save and close the file.

Give each of these files executable permissions with the commands:

chmod u+x
chmod u+x
chmod u+x

Move your scripts to a directory of your choice and then create cron jobs for each one. You’ll want to make sure to separate them with plenty of time for each to successfully run. For example, you could run the stop script at 10 p.m., the export script at midnight, and the start script at 2 a.m. Those cron jobs would something look like:

0 22 * * * /path/to/ >/dev/null 2>&1
0 0 * * * /path/to/ >/dev/null 2>&1
0 2 * * * /path/to/ >/dev/null 2>&1

With those cron jobs in place, the backup will run every night. The one thing you’ll want to make sure of is that your external drive doesn’t fill up with OVA files. Once a month (or so), make sure to clear out old files from that drive.

Should disaster strike, you can always import the last successful export of Web01 with a command like:

VBoxManage import /data/Web01-2021-4-6.ova

And that’s all there is to creating a handy, automated backup strategy for your VirtualBox VMs.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Image: Jack Wallen