This post will explain how to back up the VMs on an Ubuntu server. The goal is to make a cheap and straightforward system usable on a noncritical mission. Two old servers were dismissed at my working place, so I got the opportunity to prepare a testing environment for my colleague and a didactic exercise.
I’ll prepare three different scripts, one to turn off the VMs, one to run the backup, and one to start them before the next working day. The mission is not critical because the VMs are not used between 11 pm and 6 am of the next working day.
The VMs must be prepared to accept SSH connections from the host using a public/private key pair.
The first thing I need is to know the name of the VMs on the system. To do so, I use the command:
vboxmanage list vms
The name will be in quotes, as in “Ubuntu Server 20.04” or “VM01”
You want to back up VM01 to a .ova file and house it in an external drive mounted on /data. That command would be:
VBoxManage export "VM01" -o /data
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. Because using the VBoxManage power-off 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
The first script will power off the machine. Create the new script with the command:
nano vm-stop.sh
In that file, paste the following:
#!/bin/bash
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.
If you need to send a passphrase automatically, you can write in the script something like this:
shpass -P passphrase -p your_passphrase ssh -p port -t USER@HOST
sudo poweroff
Next, we’ll create the script to export the VM with the command:
nano vm-export.sh
In that file, paste the following
#!/bin/bash
today=$(date +"%Y-%m-%d")
VBoxManage export "VM01" -o /data/VM01-${today}.ova
The above command will append today’s date to the file name so that you won’t overwrite your previously exported OVAs.
Finally, we’ll create a script to start the virtual machine with the command:
nano vm-start.sh
In that file, paste the following contents (altering the contents to fit your needs):
!/bin/bash
VBoxManage startvm "VM01" --type headless
Give each of these files executable permissions with the commands:
chmod u+x vm-stop.sh
chmod u+x vm-export.sh
chmod u+x vm-start.sh
Should disaster strike, you can always import the last successful export of VM01 with a command like:
VBoxManage import /data/VM01-2023-4-6.ova
Now those three scripts can be run automatically with cron jobs. I’ll discuss automation in another post.