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
VBoxManagecommand?