Skip to content

Instantly share code, notes, and snippets.

@dv336699
Last active March 30, 2021 20:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save dv336699/164b621210ab0d61f62392a63477531c to your computer and use it in GitHub Desktop.
Save dv336699/164b621210ab0d61f62392a63477531c to your computer and use it in GitHub Desktop.
Backup Running VirtualBox VMs
#!/bin/sh
BASEFOLDER=/home/vbox/backups
for VMNAME in $(VBoxManage list runningvms | cut -d ' ' -f1 | sed 's/"//g;')
do
echo ""
VBoxManage controlvm "$VMNAME" acpipowerbutton
echo "Waiting for VM "$VMNAME" to poweroff..."
until $(VBoxManage showvminfo --machinereadable "$VMNAME" | grep -q ^VMState=.poweroff.)
do
sleep 1
done
echo "Exporting VM to $BASEFOLDER/$VMNAME-temp.ova..."
VBoxManage export "$VMNAME" -o "$BASEFOLDER/$VMNAME-temp.ova" --ovf20;
rm -rf "$BASEFOLDER/$VMNAME.ova"
mv "$BASEFOLDER/$VMNAME-temp.ova" "$BASEFOLDER/$VMNAME.ova"
echo "Restarting VirtualBox VM..."
VBoxManage startvm "$VMNAME" --type headless
echo ""
done
exit 0
@ay747
Copy link

ay747 commented Feb 7, 2021

Hi there,

The command $(VBoxManage list runningvms | cut -d ' ' -f1 | sed 's/"//g;') will show incorrect output if virtual machine name with space character(s), e.g. "Ubuntu Server 20.04". However, if it is changed to $(VBoxManage list runningvms | cut -d '{' -f1 | sed 's/"//g;'), the issue will be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment