Skip to content

Instantly share code, notes, and snippets.

@DonRichards
Last active February 27, 2020 16:57
Show Gist options
  • Save DonRichards/ad84282f44fc09e7b4ebf4bd45687db8 to your computer and use it in GitHub Desktop.
Save DonRichards/ad84282f44fc09e7b4ebf4bd45687db8 to your computer and use it in GitHub Desktop.
Snapshot & Restore from snapshot Virtualbox Quickly
#!/bin/bash
#title : oh_snap.sh
#description : This script will create and restore virtualbox snapshots for Islandora 8.
#author : Don Richards
#date : 20200227
#version : 1.0.0
#usage : ./oh_snap.sh
#notes : Tested on Islandora Playbooks 1.0.0
#==============================================================================
clear
VMNAME="Islandora 8 Ansible"
echo -e "\n\n -------- Snapshot names --------"
vboxmanage snapshot "$VMNAME" list --machinereadable | grep "SnapshotName" | grep "Islandora_8_Ansible" | sed 's/^[^=]*=//' | sed 's/"//g'
echo -e " -------- | --------"
VMS_RUNNING=$(VBoxManage list runningvms | grep "$VMNAME" | wc -l| sed 's/[^0-9]*//g')
if [ $VMS_RUNNING -ne 0 ]; then
echo -e "\n$VMNAME is current running..."
fi
NOW=$(date +"%m-%d-%Y-%I-%M-%S")
SNAPSHOT_NAME="Islandora_8_Ansible_${NOW}"
SNAPSHOT_DESCRIPTION="Snapshot taken on ${NOW}"
HELP="\n\nOPTIONS: $ ./oh_snap.sh -h|--help -l|--last|--mcfly -r|--restore -f|--flux .... \n\nfor help: \n $ ./oh_snap.sh -h\n\nTo restore from last snapshot: \n $ ./oh_snap.sh -last\n\nTo restore from snapshot name: \n $ ./oh_snap.sh -r SNAPSHOT-NAME \n\n $ ./oh_snap.sh -r Islandora_8_Ansible_02-26-2020-15-44-01\n place the name from 'Snapshot names' list here ⤴ \n\nTake a snapshot: \n $ ./oh_snap.sh --flux\n\n\n"
key=$1
case $key in
-h|--help)
echo -e "$HELP"
;;
-l|--last|--mcfly)
if [ $VMS_RUNNING -ne 0 ]; then
echo -e "\n$VMNAME is current running..."
echo "Powering off VM"
VBoxManage controlvm "$VMNAME" poweroff
fi
echo "Restoring the VM to the last snapshot occurrence"
VBoxManage snapshot "$VMNAME" restorecurrent
echo -e "\nYou’ve got to come back with me! Back to the .... directory where the Vagrantfile is and run:\n\t$ vagrant up\n\n"
;;
-r|--restore)
if [ -z "$2" ]; then
echo -e "\n Roads? Where we're going, we don't need roads. But you do need to specify a snapshot name with this option to restore from.\n\n"
exit 0
fi
GOINGBACK=$(vboxmanage snapshot "Islandora 8 Ansible" list --machinereadable | grep "SnapshotName-" | grep "$2" | cut -f1 -d"=")
GOINGGOING=$(echo $GOINGBACK | sed "s/SnapshotName/SnapshotDescription/g")
GONEBACK=$(vboxmanage snapshot "Islandora 8 Ansible" list --machinereadable | grep "${GOINGGOING}=" | sed 's/^[^=]*=//' | sed 's/"//g' | sed 's/Snapshot taken on //g' | sed 's/-/ /3' | sed 's/-/:/3' | sed 's/-/:/3')
[ $VMS_RUNNING -ne 0 ] && VBoxManage controlvm "$VMNAME" poweroff
echo "Sending the VM back to $GONEBACK using the $2 snapshot"
VBoxManage snapshot "$VMNAME" restore "${2}"
echo -e "\nYou’ve got to come back with me! Back to the .... directory where the Vagrantfile is and run:\n\t$ vagrant up\n\n"
;;
-f|--flux)
echo -e "\n\tWait a minute, Doc. Ah... Are you telling me that you built a time machine... Yeah kinda. It creates snapshots, creating one now...\n"
VBoxManage snapshot "$VMNAME" take "$SNAPSHOT_NAME" --description "$SNAPSHOT_DESCRIPTION" --pause
echo -e "Alright then, Future Boy, the snapshot done.\n\n"
;;
*)
echo -e "$HELP"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment