Exporting a Backup of a Virtualbox VM

This script allows you to backup a Virtualbox VM via the export function.

Text Box

#!/bin/bash

#

#

# This script will export running vms to ova files

# Script runs daily

VBOXUSER="vbox"

RUNNINGVMS=$(sudo -H -u $VBOXUSER vboxmanage list runningvms | cut -d " " -f1 | sed -e 's/^.//' -e 's/.$/$

DIR="/home/vbox/exports/"

DATE=$(date +'%Y%m%d')

2DAYSAGO=$(date --date="2 days ago" +'%Y%m%d')

for i in $RUNNINGVMS

do

echo "Exporting $i... Please wait..."

sudo -H -u $VBOXUSER vboxmanage controlvm $i savestate

sudo -H -u $VBOXUSER vboxmanage export $i -o $DIR$i"-"$DATE.ova

sudo -H -u $VBOXUSER vboxmanage startvm $i --type headless

sleep 10

done

rm $DIR$i"-"$2DAYSAGO.ova

exit 0