1

What I'm Trying To Accomplish:

  • Get list of Virtual Machines ".vmx" file and place in variables
  • Give the user and choice of which VM to backup
  • Use "ovftool" to produce an export of the chosen VM to a hardcoded location

What I Have So Far:

#!/bin/bash

# MyVariables
#vmFolder=
#vmList=
#vmDestination="~/Desktop/"
vmTool="/Applications/VMware OVF Tool/ovftool"

# DatScript
for i in "/Users/$USER/Documents/Virtual Machines.localized/"* ; do
   if [ -d "$i" ]; then
     echo $(basename "$i")
   fi
done

Still getting used to Bash and FWIW I'm writing this for OS X 10.8 but thought the Unix SE would be best.

3
  • @Anthon it says you edited my post but I don't see anything different? And is this post in the correct spot? Commented Nov 30, 2013 at 16:17
  • 1
    You can click on the link 'edited X hours ago' above the last editors (my) image to the left of yours. You will see the edit history. There should be people here who can help you out with bash scripts, although I would do such a thing in a Python program. So yes it should be ok here. Commented Nov 30, 2013 at 17:02
  • this decade old question was at the top of 'related' because bash I guess. Good job SO, keep cookin' 💩 Commented Sep 7, 2024 at 12:07

1 Answer 1

-1

You can use this script I just wrote https://github.com/Yafet-Getachew/vmbackuputil.git. It loops through multiple virtual machines in Virtualbox (I'm sure you can do some modifications for vmware) and backs them up to a remote server. You can delete the bottom half of the script and just get the OVA files.

#!/bin/bash
#
#
#   Backup Virtual Machines to SERVERNAME 
#
#
#

# create array with names, ips, usernames and passwords of virtual machines
echo
echo "Backup utility for VirtualBox virtual machines"
echo

ssh-add

declare -A virtualmachines

virtualmachines=(
    [VIRTUAL_MACHINE_NAME]="VIRTUAL_MACHINE_IPADDRESS"
    )

users=(
    [VIRTUAL_MACHINE_NAME]="VIRTUAL_MACHINE_USERNAME"
    )

passwords=(
    [VIRTUAL_MACHINE_NAME]="VIRTUAL_MACHINE_USER_PASSWORD"
    )

#make a directory 
mkdir -p backup-vms

#check if sshpass is installed and install it if not
if ! (dpkg-query -l sshpass) > /dev/null; then
   echo -e "sshpass not installed, installing (sudo might be required)... "
   sudo apt install sshpass
fi



# loop through virtual machines and shut them down
for i in "${!virtualmachines[@]}"
do

    echo "shutting down $i through ip ${virtualmachines[$i]}"
    sshpass -p ${passwords[$i]} ssh -o StrictHostKeyChecking=no ${users[$i]}@${virtualmachines[$i]} sudo poweroff

    echo "Waiting for machine $i to poweroff..."

    until $(VBoxManage showvminfo --machinereadable $i | grep -q ^VMState=.poweroff.)

    do
      sleep 1
      echo "poweroff state not reached for $i"
    done

    echo "poweroff of $i successful, building ova"
    echo

    # build .ova in home directory to then transport to medemer
    VBoxManage export $i -o backup-vms/$i-$(date +"%m-%d-%y").ova

    # Copy backed up file to remote backup folder
    rsync -e 'ssh -p 22' -avzpi --progress backup-vms BACKUP_SERVER_USER@BACKUP_SERVER_IP:~/backup/backup-vms
    #rm -rf backup-vms/*

    echo "SUCCESSFUL BACKUP OPERATION"
done

echo
4
  • Hello thank you for your contribution, but answer on this site should not require clarification from the asker. Could you elaborate a bit ? Commented Aug 28, 2018 at 12:05
  • Thanks @Morpheus.47. I'll take a look at this and test it out. I didn't get a notification saying this was answered so my apologies for the time it's taken to respond. Commented Aug 31, 2018 at 3:23
  • @Kiwy more reading less mini-modding and maybe you'd have understood how that loop runs over the array this guy thinks is associative while exposing passwords and requiring sudo to ssh somewhere because keys are hard and comments were all he learned in school. probs not though. Commented Sep 7, 2024 at 12:28
  • @EdSwangren do not infer something on an 6 years old comment. the edit makes it simpler to understand and it was not like that initialy. Commented Sep 9, 2024 at 10:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.