Virtual Box – Automating OVA’s backups

#!/bin/bash
#jjest@donkey:~$ VBoxManage list vms
#”house-plex-new” {14c956f3-79a5-447d-a1a8-4bfe091d6bed}
#”house-nagios_1″ {6b219ce4-cdbc-49c8-b899-e1e61e64b096}
#”house-ftp” {d126abb8-927d-4066-b66a-c0d52abec7c2}

date=$(date “+%Y%m%d”)
backup_path=”/data/vms/vm-backups”
for vm in $(VBoxManage list vms | cut -d ‘”‘ -f 2); do
#CREATE DIR IF NOT EXISTS
if [[ ! -d “$backup_path/$vm” ]]; then
echo “Creating $backup_path/$vm”
mkdir “$backup_path/$vm”
fi

#SHUTDOWN IF RUNNING
running_vm=$(VBoxManage list runningvms | cut -d ‘”‘ -f 2 | grep “$vm”)
if [[ “$running_vm” == “$vm” ]]; then
echo “Powering off $vm…”
VBoxManage controlvm “$vm” poweroff
else
echo “$vm already powered off…”
fi

#EXPORT OVA
if [[ ! -e “$backup_path/$vm/$vm-$date.ova” ]]; then
echo “Exporting $vm…”
VBoxManage export “$vm” -o “$backup_path/$vm/$vm-$date.ova”
else
echo “$vm-$date.ova already exists!”
fi

#POWER ON VM
echo “Powering on $vm…”
VBoxManage startvm “$vm” –type headless
done

Leave a Reply