Last active
July 4, 2022 23:25
-
-
Save tuklusan/15649a2de122a4002028986700d98ad9 to your computer and use it in GitHub Desktop.
SANYALnet Labs | Save State of VirtualBox Virtual Machines on APC UPS power loss shutdown by apcupsd Linux Daemon / save-vms-on-ups-power.sh: see https://supratim-sanyal.blogspot.com/2021/09/save-state-of-virtualbox-virtual.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# /root/save-vms-on-ups-power.sh | |
# | |
# SEE https://supratim-sanyal.blogspot.com/2021/09/save-state-of-virtualbox-virtual.html | |
# | |
# SANYALnet Labs | Supratim Sanyal's Hobbyist Lab | |
# | |
# Based on https://ubuntuforums.org/archive/index.php/t-1890848.html | |
# | |
VBOXUSER="localuser" | |
# --- | |
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin" | |
SU="sudo -H -u ${VBOXUSER}" | |
###RUNNINGVMS=$(${SU} vboxmanage --nologo list vms | sed -e 's/^".*".*{\(.*\)}/\1/') | |
RUNNINGVMS=$(${SU} vboxmanage --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/') | |
# Exit if no VMs are running | |
if [[ ${RUNNINGVMS} = "" ]]; then | |
echo -e "$0 $(date): No VirtualBox VMs are running.\a" | |
exit 0 | |
fi | |
function getVMName() | |
{ echo $(${SU} vboxmanage --nologo list vms | grep "$1" | awk -F\" '{print $(NF -1)}'); } | |
echo "$0 $(date): List of running VMs:" | |
echo ${RUNNINGVMS} | |
if [[ -n ${RUNNINGVMS} ]]; then | |
for VM in ${RUNNINGVMS}; do | |
VMNAME=$(getVMName ${VM}) | |
CURTIME=$(date +"%d%b%Y-%H%M%S.%N") | |
echo -e "$0 $(date): Taking snapshot of \"${VMNAME}\"" | |
wall "$0 $(date): Taking snapshot of \"${VMNAME}\"" | |
echo "$0 $(date): Doing ${SU} vboxmanage --nologo snapshot ${VM} take UPS-ON-BATTERY-${CURTIME} --description=BEFORE\ UPS\ TRIGGERED\ SAVING\ STATE\ ON\ BATTERY\ AT\ ${CURTIME}" | |
${SU} vboxmanage --nologo snapshot ${VM} take UPS-ON-BATTERY-${CURTIME} --description=BEFORE\ UPS\ TRIGGERED\ SAVING\ STATE\ ON\ BATTERY\ AT\ ${CURTIME} | |
sync;sync;sync | |
echo -e "$0 $(date): Saving state of \"$(getVMName ${VM})\"" | |
wall "$0 $(date): Saving state of \"$(getVMName ${VM})\"" | |
echo "$0 $(date): Doing ${SU} vboxmanage --nologo controlvm ${VM} savestate" | |
${SU} vboxmanage --nologo controlvm ${VM} savestate | |
sync;sync;sync | |
echo -e "$0 $(date): UPS STATUS" | |
apcaccess status | |
echo -e "$0 $(date): VIRTUAL MEMORY STATUS" | |
vmstat | |
sync;sync;sync | |
done | |
else | |
echo -e "$0 $(date): No VirtualBox VMs to save state\a" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment