Skip to content

Instantly share code, notes, and snippets.

@davidegironi
Last active June 5, 2017 19:58
Show Gist options
  • Save davidegironi/73f7dc8c65625d51b9a45e9fbe2f73c2 to your computer and use it in GitHub Desktop.
Save davidegironi/73f7dc8c65625d51b9a45e9fbe2f73c2 to your computer and use it in GitHub Desktop.
vSphere ESXi backuphelper.sh
#!/bin/sh
#run this script at machine startup with option S
#if the machine is startup at selected day of week, hour and between minute interval then a shutdown is triggered
#the shutdown trigger can be disabled by deleting the shutdown trigger file
RUN_DAYOFWEEK=6
RUN_HOUR=2
RUN_MINUTEMIN=0
RUN_MINUTEMAX=20
BACKUP_CRONTABCMD="30 2 * * 6 /opt/ghettovcb/bin/ghettoVCB.sh -a -g /vmfs/volumes/backups01/backuphelper/ghettoVCB.conf > /vmfs/volumes/backups01/backuphelper/ghettoVCB.log"
SHUTDOWN_TRIGGERFILE=/vmfs/volumes/backups01/backuphelper/shutdowntriggerenabled.txt
SHUTDOWN_CRONTABCMD="0 2 * * 7 /vmfs/volumes/backups01/backuphelper/backuphelper.sh C"
SHUTDOWN_CMD=/vmfs/volumes/backups01/backuphelper/esxidown.sh
if [ "$1" == "S" ]; then
echo backuphelper.sh Startup mode
echo Scheduling the next backup
/bin/kill $(pidof crond)
/bin/echo "$BACKUP_CRONTABCMD" >> /var/spool/cron/crontabs/root
/bin/crond
DAYOFWEEK=$(date +"%u")
HOUR=$(date +"%H")
MINUTE=$(date +"%M")
if [ -f "$SHUTDOWN_TRIGGERFILE" ]; then
echo Removing previous shutdown trigger file
rm "$SHUTDOWN_TRIGGERFILE"
fi
if [ "$DAYOFWEEK" -eq "$RUN_DAYOFWEEK" ]; then
if [ "$HOUR" -eq "$RUN_HOUR" ]; then
if [ "$MINUTE" -ge "$RUN_MINUTEMIN" ]; then
if [ "$MINUTE" -le "$RUN_MINUTEMAX" ]; then
echo Scheduling shutdown
touch "$SHUTDOWN_TRIGGERFILE"
/bin/kill $(pidof crond)
/bin/echo "$SHUTDOWN_CRONTABCMD" >> /var/spool/cron/crontabs/root
/bin/crond
fi
fi
fi
fi
elif [ "$1" == "C" ]; then
echo backuphelper.sh Crontab mode
if [ -f "$SHUTDOWN_TRIGGERFILE" ]; then
echo Removing previous shutdown trigger file
rm "$SHUTDOWN_TRIGGERFILE"
echo Shutting down
$SHUTDOWN_CMD
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment