Monday, November 25, 2019

clean up openbox failed purged vms

#!/bin/bash
work=/home/uid/tmp
validvms=/home/uid/tmp/validvms
workclean=/home/uid/tmp/cleanvms
virtualboxvmsdir=/home/uid/VirtualBox\ VMs
box=$(hostname)
purgedate=$(date +"%m-%d-%Y")

# clean  up old work directories
rm -rf $work
mkdir $work

# find all vbox vms - not just running
# make the vbox vms into a list and remove
# extraneous information

vboxmanage list vms >> $validvms
cut -d '"' -f2 < $validvms >> $workclean

# change directory into where vbox vms reside

cd /home/uid/VirtualBox\ VMs
echo $purgeate >> $work/purgedvms-use
echo ".........................." >> $work/purgedvms-use
echo "start" >> $work/purgedvms-use
du -hsc >> $work/purgedvms-use

# exit if vbox vms directory is not found
if (($?>0)); then
    echo "cannot find virtualbox dir exiting"
    exit
fi

# grep is going through the validvm list
# if the line item is not found then it is deleted
# as each item is being deleted it is being captured
# in a file

for i in *; do
    if ! grep -qxFe "$i" $workclean; then
        echo "Deleting: $i"
        echo $i >> $work/purgedvms
        # the next line is commented out.  Test it.  Then uncomment to remove the files
        rm -rf "$i"
    fi
done

echo "finish" >> $work/purgedvms-use
du -hsc >> $work/purgedvms-use
echo ".........................." >> $work/purgedvms-use
sed -i '/total/d' $work/purgedvms-use


# here we email the results

cat $work/purgedvms-use $work/purgedvms > $work/purgedvms-union
mail -s "$box purged $purgedate" me@inhell < $work/purgedvms-union

exit

No comments: