What do you do when you are managing a remote server and you need to make some critical changes (like to the networking configs) and you feel uncomfortable about the possibility of losing access to the server and never getting it back? This was the situation we were in today. The server is a little esoteric – its a headless box and even in the server center the engineers don’t have any way to log in interactively at the server itself. Luckily the server is running Debian linux and has a usb port so help is at hand via bash!
I wrote this little script which is designed to be run from a cron job, for example every minute.
#!/bin/bash
# This script is to rescue the system from usb while
# testing migration to the new vpn.
# It will mount the last partition of any inserted usb,
# cd to the mount point and try to run a script
# called 'rescue.sh'
# After the script is run it will be renamed to
# rescue.ok
#
# You should set this script to run as a cron job
# at minute intervals.
#
# e.g. # m h dom mon dow command
# * * * * * /root/usbrescue.sh
#
RESCUEFILE=rescue.sh
OKFILE=rescue.ok
LOGFILE=rescue.log
MOUNTPOINT=/mnt/rescue
SCRIPTPATH=${MOUNTPOINT}/${RESCUEFILE}
OKPATH=${MOUNTPOINT}/${OKFILE}
LOGPATH=${MOUNTPOINT}/${LOGFILE}
# Note we ignore partitions on devices sda - sdd as they are internal disks
LASTPARTITION=$(cat /proc/partitions | awk '{print $4}' | grep -v 'sd[a-d]' \
| grep -v name | grep -v '^$' |sort | tail -1)
if [ $LASTPARTITION != "" ]
then
if [ ! -b /dev/$LASTPARTITION ]
then
echo "Error /dev/$LASTPARTITION is not a block device"
exit
else
echo "OK /dev/$LASTPARTITION is a block device"
fi
echo "Device found creating mount point"
if [ ! -d "$MOUNTPOINT" ]
then
mkdir $MOUNTPOINT
fi
echo "Mounting...."
mount /dev/$LASTPARTITION $MOUNTPOINT
echo "Checking if rescue script exists"
# Test the rescue script exists(-e) and is not 0 length (-s)
if [ -e $SCRIPTPATH -a -s $SCRIPTPATH ]
then
echo "Making $SCRIPTPATH executable"
chmod +x $SCRIPTPATH
echo "Running script"
$SCRIPTPATH > $LOGPATH 2>&1
echo "Disabling script"
mv $SCRIPTPATH $OKPATH
else
echo "No Rescue script found"
fi
echo "Unmounting.."
cd /
umount $MOUNTPOINT
else
echo "No rescue device found"
fi
echo "done"
If you place the script in /root/usbrescue.sh and add a cron job as outlined in the comments, it will poll for devices regularly, mount the last partition available. If it finds a script on that partition labelled rescue.sh, it will run it then rename the script to rescue.ok and write any stderror and stdout logs to rescue.log on the partition. The script could perhaps be improved by adding a lock file so that it does not get run again if it is already running (if it takes longer than a minute to run for example), buts its a good starting point for a system rescue if things go wrong. Now the engineer on site can simply pop in his usb stick and any recovery commands will be run from it.