How to create a VirtualBox virtual machine backup on a Linux host

Jack Wallen walks you via the system of developing easy bash scripts to automate backing up your VirtualBox VMs.

Image: Jack Wallen

Your facts centre could rely on virtual devices (VMs), and you may possibly use VirtualBox for some of individuals VMs. If which is the scenario, what do you do when catastrophe strikes? Do you presently have a system set in movement for such an eventuality? You need to. In simple fact, you ought to.

When one thing goes awry, you want to have a stable backup offered. Of program, VirtualBox has a excellent snapshot instrument and you could normally make use of that. What if you want to generate a totally automatic backup procedure that would export your VMs (to .ova information) routinely? 

Let us make that occur.

Everyone’s problem is various, so your mileage may well fluctuate with this. Even so, what I am about to present you will give you a good commence on developing your pretty possess VirtualBox VM backup system.

SEE: MSP most effective practices: Server deployment checklist (TechRepublic Premium)

What you may need to have

To do the job via this, you can expect to require a managing occasion of VirtualBox with at the very least a person VM offered to again up. You will also have to have a travel with lots of house to back that VM up to.

How to create the backup

This backup will arrive by way of a bash script. It is really considerably less difficult than you believe. The very first point we have to have to know is the name of the VM to be backed up. To discover out the names of all your current VMs, concern the command:

vboxmanage record vms

In that listing, you will discover the VM you want to back again up. The title will be in offers, as in “Ubuntu Server 20.04,” or “Net01.” Let us say you want to again up Website01 to an .ova file housed in an external drive mounted on /details. That command would be:

VBoxManage export "Internet01" -o /info

Dependent on how huge the VM is, the command could just take some time to run. When it completes, you ought to see the /information/World wide web01.ova file all set. 

In advance of you difficulty the command, the device will have to be powered off. The ideal way to do that is to energy off the VM from within the guest. Making use of the VBoxManage poweroff command could result in facts loss, which we want to steer clear of at all expenses. 

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

ssh -t Person@HOST sudo poweroff

Exactly where User is the identify of the remote admin consumer and HOST is the IP deal with of the VirtualBox visitor VM. Of class, to make this work by using a script, you will want to set up SSH critical authentication. Make sure to copy your SSH important from the host to the visitor with the command:

ssh-duplicate-id Person@HOST

Once you’ve got taken treatment of that, you can then situation the poweroff command as listed earlier mentioned and then export the VM to the .ova file.

How to automate this

This is the place it will get a little bit difficult simply because in the automation you have to produce 3 bash scripts:

The initial script will energy off the equipment. Make the new script with the command:

nano vm-stop.sh

In that file, paste the adhering to:

#!/bin/bash

ssh -t Person@HOST sudo poweroff

Where Person is the name of the distant admin consumer and HOST is the IP address of the VirtualBox visitor VM.

Help you save and close the file.

Following, we will create the script to export the VM with the command:

nano vm-export.sh

In that file, paste the adhering to

#!/bin/bash

nowadays=$(day +"%Y-%m-%d")
VBoxManage export "World wide web01" -o /info/Net01-$now.ova

Bear in mind to adjust the particulars to match your demands. Save and near the file. The higher than file will append modern date in the file, so you won’t overwrite your formerly exported OVAs.

At last, we’ll create a script to start the digital equipment with the command:

nano vm-begin.sh

In that file, paste the adhering to contents (altering the contents to in shape your requirements):

#!/bin/bash

VBoxManage startvm "Internet01" --type headless

Preserve and near the file.

Give just about every of these files executable permissions with the commands:

chmod u+x vm-quit.sh
chmod u+x vm-export.sh
chmod u+x vm-start off.sh

Move your scripts to a listing of your option and then develop cron work for every just one. You’ll want to make guaranteed to different them with plenty of time for each and every to correctly run. For instance, you could operate the halt script at 10 p.m., the export script at midnight, and the start script at 2 a.m. Those cron work would anything appear like:

 22 * * * /route/to/vm-halt.sh >/dev/null 2>&1
  * * * /route/to/vm-export.sh >/dev/null 2>&1
 2 * * * /path/to/vm-end.sh >/dev/null 2>&1

With those people cron work in position, the backup will run each evening. The just one issue you may want to make guaranteed of is that your external push would not fill up with OVA information. At the time a month (or so), make certain to clear out outdated files from that travel.

Should really disaster strike, you can always import the previous profitable export of World wide web01 with a command like:

VBoxManage import /information/Internet01-2021-4-6.ova

And which is all there is to building a handy, automatic backup tactic for your VirtualBox VMs.

Subscribe to TechRepublic’s How To Make Tech Operate on YouTube for all the newest tech advice for company execs from Jack Wallen.

Also see

Fibo Quantum