2

I am working with a server running Ubuntu 18.01 LTS and I'm trying to automate the backup of multiple virtual machines.

I have the VM names in an array and then a for loop to shut down, backup and then restart each VM. I ran this over the weekend, came in today and all the commands seem to have run, but only for the first index of the array and the script doesn't exit.

Here is my script.

#!/bin/bash
######################
#
# Shut down and back up select VMs
#
#####################

#make new date formatted directory
sudo mkdir /mnt/md1/VirtualMachines/bak/$(date +%Y_%m_%d) |& tee -a /mnt/md1/Scripts/log_vboxBak_$(date +%Y_%m_%d).txt;
sudo chown bvserv /mnt/md1/VirtualMachines/bak/$(date +%Y_%m_%d) |& tee -a /mnt/md1/Scripts/log_vboxBak_$(date +%Y_%m_%d).txt;

#Array of VMs
declare -a VM=("Win-10-POS-1" "Win-10-POS-2" "Desktop_Neil")

#loop through array of VMs
for i in "${VM[@]}"
do
# Shut down virtual machine
sudo -u bvserv VBoxManage controlvm "$i" poweroff |& tee -a /mnt/md1/Scripts/log_vboxBak_$(date +%Y_%m_%d).txt;
# Export virtual machine to dated file
sudo -u bvserv VBoxManage export "$i" -o /mnt/md1/VirtualMachines/bak/$(date +%Y_%m_%d)/"$i".ova |& tee -a /mnt/md1/Scripts/log_vboxBak_$(date +%Y_%m_%d).txt;
# Restart virtual machine
sudo -u bvserv VBoxHeadless --startvm "$i" |& tee -a /mnt/md1/Scripts/log_vboxBak_$(date +%Y_%m_%d).txt
done
  • What do you mean by "the script doesn't exit". Was it just hanging blocked on a particular VBoxManage command? – ddnomad Mar 9 at 16:54
  • @ddnomad I left an open ssh session on local network from when I executed the script. When I returned all output looked good, all scripts for index 0 ran properly without error. However the terminal did not return to where I could enter a new command. Looks like it hit the done line and just sat there. – Jaden Mar 9 at 18:54
  • @steeldriver Do you think it would run fine if I commented out the restart line it would work fine? I have a separate script to start these VMs at boot and could call that when done instead of looping restart. – Jaden Mar 9 at 19:00
  • @steeldriver I will try that after hours tonight. Thanks for the suggestion. – Jaden Mar 9 at 19:17
  • @steeldriver Thanks that solution works. I have posted an answer crediting you. – Jaden Mar 10 at 16:11
3

The issue turns out to be that the VBoxHeadless command starts each VM as a foreground process, so execution of the loop does not continue to the next VM until the previous one exits.

For the restart portion of the script I had to use VBoxManage instead of VBoxHeadless to start the machines. After making that change everything is working. Here is the updated script now loading an external array for reference.

#!/bin/bash
######################
#
# Shut down and back up select VMs
#
#####################

#make new date formatted directory
sudo mkdir /mnt/md1/VirtualMachines/bak/$(date +%Y_%m_%d) |& tee -a /mnt/md1/Scripts/log_vboxBak_$(date +%Y_%m_%d).txt;
sudo chown bvserv /mnt/md1/VirtualMachines/bak/$(date +%Y_%m_%d) |& tee -a /mnt/md1/Scripts/log_vboxBak_$(date +%Y_%m_%d).txt;

#Read array of virtual machines from file
readarray -t VM < /mnt/md1/VirtualMachines/auto-start_list.txt

#loop through array of VMs
for i in "${VM[@]}"
do
# Shut down virtual machine
sudo -u bvserv VBoxManage controlvm "$i" poweroff |& tee -a /mnt/md1/Scripts/log_vboxBak_$(date +%Y_%m_%d).txt;
# Export virtual machine to dated file
sudo -u bvserv VBoxManage export "$i" -o /mnt/md1/VirtualMachines/bak/$(date +%Y_%m_%d)/"$i".ova |& tee -a /mnt/md1/Scripts/log_vboxBak_$(date +%Y_%m_%d).txt;
# Restart virtual machine
sudo -u bvserv VBoxManage startvm "$i" --type headless |& tee -a /mnt/md1/Scripts/log_vboxBak_$(date +%Y_%m_%d).txt
#echo "$i"
done

Reference:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.