#!/bin/sh # /usr/local/sbin/rmbackup # # Use this script to remove an external SATA drive from the SCSI device list # and to properly power down the disk for removal. # Originally designed for a dedicated backup drive but expanded to include # any external drive. source /etc/shell-colors source /etc/functions-system # variables DRIVEMODEL and SMARTPARAMS are stored in /etc/functions-system # Presume the disk is the regular backup drive. BACKUPDRIVE=$DRIVEMODEL # Allow overriding the presumed default drive model. if [ -n "$1" ]; then BACKUPDRIVE=$1 fi if [ -n "$2" ]; then if [ -n "`echo $2 | grep '/dev/'`" ] ; then DEVICE="`echo $2 | sed 's/\/dev\///'`" else DEVICE=$2 fi fi if [ -z "`lsscsi | grep $BACKUPDRIVE`" ]; then echo -e "${BOLDGREEN}$BACKUPDRIVE${BOLDRED} does not seem to be connected!${COLOR_RESET}" lsscsi echo exit 0 elif [ -n "$DEVICE" ]; then lsscsi | grep $BACKUPDRIVE | grep $DEVICE &>/dev/null elif [ "`lsscsi | grep -c $BACKUPDRIVE`" -gt "1" ]; then echo -e "${BOLDRED}There is more than one device with that model number:${COLOR_RESET}" lsscsi | grep $BACKUPDRIVE echo "Try again and add a device node as a parameter (e.g., hdb, sdb, etc.)." exit 0 fi if [ -n "$DEVICE" ]; then echo -e "Attempting to remove ${BOLDGREEN}$BACKUPDRIVE${COLOR_RESET} (${BOLDGREEN}${DEVICE}${COLOR_RESET}) from the SCSI list." HOSTS="`lsscsi | grep $BACKUPDRIVE | grep $DEVICE | sed 's/\[//' | sed 's/\]//' | sed 's/\:/ /g' | awk '{print $1}'`" CHANNELS="`lsscsi | grep $BACKUPDRIVE | grep $DEVICE | sed 's/\[//' | sed 's/\]//' | sed 's/\:/ /g' | awk '{print $2}'`" IDS="`lsscsi | grep $BACKUPDRIVE | grep $DEVICE | sed 's/\[//' | sed 's/\]//' | sed 's/\:/ /g' | awk '{print $3}'`" LUNS="`lsscsi | grep $BACKUPDRIVE | grep $DEVICE | sed 's/\[//' | sed 's/\]//' | sed 's/\:/ /g' | awk '{print $4}'`" else echo -e "Attempting to remove ${BOLDGREEN}$BACKUPDRIVE${COLOR_RESET} from the SCSI list." DEVICE="`lsscsi | grep $BACKUPDRIVE | awk '{print $7}'`" HOSTS="`lsscsi | grep $BACKUPDRIVE | sed 's/\[//' | sed 's/\]//' | sed 's/\:/ /g' | awk '{print $1}'`" CHANNELS="`lsscsi | grep $BACKUPDRIVE | sed 's/\[//' | sed 's/\]//' | sed 's/\:/ /g' | awk '{print $2}'`" IDS="`lsscsi | grep $BACKUPDRIVE | sed 's/\[//' | sed 's/\]//' | sed 's/\:/ /g' | awk '{print $3}'`" LUNS="`lsscsi | grep $BACKUPDRIVE | sed 's/\[//' | sed 's/\]//' | sed 's/\:/ /g' | awk '{print $4}'`" fi echo lsscsi | grep $BACKUPDRIVE | grep $DEVICE echo echo -e "DEVICE=${BOLDGREEN}$DEVICE${COLOR_RESET}" echo -e "HOSTS=${BOLDGREEN}$HOSTS${COLOR_RESET}" echo -e "CHANNELS=${BOLDGREEN}$CHANNELS${COLOR_RESET}" echo -e "IDS=${BOLDGREEN}$IDS${COLOR_RESET}" echo -e "LUNS=${BOLDGREEN}$LUNS${COLOR_RESET}" echo echo -e "${BOLDYELLOW}Removing ${BOLDGREEN}$BACKUPDRIVE${BOLDYELLOW} (${BOLDGREEN}$DEVICE${BOLDYELLOW}) from SCSI list...${COLOR_RESET}" Wait_For_Response "Continue?" Proceed_From_Response # modify disk for quietest performance # echo echo "Restoring AAM..." hdparm -q -M 128 /dev/${DEVICE} echo echo -e "${BOLDWHITE}Checking mount status...${COLOR_RESET}" if [ -z "$DEVICE" ]; then DEVICE="`lsscsi | grep $BACKUPDRIVE | awk '{print $7}'`" fi MOUNTPOINT="`mount | grep $DEVICE | awk '{print $1}'`" if [ -n "$MOUNTPOINT" ]; then echo -e "${BOLDYELLOW}Oops! Found ${BOLDGREEN}$BACKUPDRIVE${BOLDYELLOW} mounted at $MOUNTPOINT. Unmounting...${COLOR_RESET}" umount $MOUNTPOINT else echo "$BACKUPDRIVE does not seem to be mounted anywhere." fi echo mount echo # quietly remove any separate conky displays for the backup drive if [ -n "`ps ax | grep 'conky -t' | grep $DRIVEMODEL | grep -v grep`" ]; then PIDS="`ps ax | grep 'conky -t' | grep $DRIVEMODEL | awk '{print $1}' | grep -v grep`" for p in $PIDS ; do kill -9 $p done; fi if [ -n "`ps ax | grep '/tmp/conkyrc-backup' | grep -v grep`" ]; then PIDS="`ps ax | grep '/tmp/conkyrc-backup' | awk '{print $1}' | grep -v grep`" for p in $PIDS ; do kill -9 $p done; fi echo -e "${BOLDWHITE}This might take a few seconds...${COLOR_RESET}" date # This never hurts. echo "Synchronizing disk cache." /bin/sync # Stop (spin-down) the device. echo "Stopping (spinning-down) /dev/${DEVICE}." /usr/bin/sg_start -i -v --stop /dev/${DEVICE} # remove from scsi bus # caveat: the rescan-scsi-bus script will fail unless the sg3_utils package is installed # rescan-scsi-bus --remove --hosts=$HOSTS --channels=$CHANNELS --ids=$IDS --luns=$LUNS # &>/dev/null echo "scsi remove-single-device $HOSTS $CHANNELS $IDS $LUNS" >/proc/scsi/scsi echo # show the user the results grep $DEVICE /var/log/messages | grep "$HOSTS:$CHANNELS:$IDS:$LUNS" | grep 'Synchronizing SCSI cache' | tail -n1 grep $DEVICE /var/log/messages | grep "$HOSTS:$CHANNELS:$IDS:$LUNS" | grep 'Stopping disk' | tail -n1 echo lsscsi echo echo -e "${BOLDYELLOW}The $BACKUPDRIVE may be powered off.${COLOR_RESET}" echo "The device cannot be restored to the scsi list without cycling power." echo # restart hddtemp daemon if [ -e /usr/sbin/hddtemp ]; then echo -e "${BOLDWHITE}Restarting hddtemp daemon..."${COLOR_RESET} killall hddtemp &>/dev/null if [ "`uname -n`" = "ovenbird" ] || [ "`uname -n`" = "nuthatch" ] || [ "`uname -n`" = "goldfinch" ]; then /usr/sbin/hddtemp -d /dev/hda /dev/sda elif [ "`uname -n`" = "robin" ] || [ "`uname -n`" = "downy" ] || [ "`uname -n`" = "hairy" ]; then /usr/sbin/hddtemp -d /dev/sda else /usr/sbin/hddtemp -d /dev/hda fi fi echo echo -e "${BOLDWHITE}Done.${COLOR_RESET}" echo