Skip to content

Instantly share code, notes, and snippets.

@albarki
Created April 25, 2017 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albarki/d143d6b62e19a5c01d0f4c50b88f9686 to your computer and use it in GitHub Desktop.
Save albarki/d143d6b62e19a5c01d0f4c50b88f9686 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Bacula interface to mtx autoloader
#
# Created OCT/31/03 by Alexander Kuehn, derived from Ludwig Jaffe's script
#
# *Heavily* hacked between 31/10/07 & 02/11/07 by Sean Cardus to make it work
# with the current Bacula version & our LTO tape drive. This works for me,
# it might work for you too - But, don't blame me if it blows up :)
#
# John Murray added storagename feature on 6th Nov 2007 to cater for situation
# where more than one Storage resource exists - we need to supply the correct
# qualifier after 'status Storage'
#
# * Check that the bacula-sd process has permission to run this script,
# run bconsole and to write to the logfile specified below! *
#
#set -x
# these are the labels of the tapes in each virtual slot, not the slots!
labels="DC01 DC02 DC03 DC04 DC05 DC06 DC07 DC08 DC09 DC10"
# If more than one Storage resource exists, specify which to check status
# of (name is case-sensitive)
# e.g.
# storagename="Tape"
storagename=""
# who to send a mail to?
recipient=operator@my.domain.name
logfile=/var/log/mtx.log
# Delay in seconds how often to check whether a new tape has been inserted
TAPEDELAY=10 # the default is every 10 seconds
echo `date` ":" $@ >>$logfile
# change this if mt is not in the path (use different quotes!)
mt=`which mt`
grep=`which grep`
#
# how to run the console application?
console="/usr/sbin/bconsole -c /etc/bacula/bconsole.conf"
command="$1"
#TAPEDRIVE0 holds the device/name of your 1st and only drive (Bacula supports only 1 drive currently)
#Read TAPEDRIVE from command line parameters
if [ -z "$2" ] ; then
TAPEDRIVE0=/dev/nst0
else
TAPEDRIVE0=$2
fi
#Read slot from command line parameters
if [ -z "$3" ] ; then
slot=`expr 1`
else
slot=`expr $3`
fi
if [ -z "$command" ] ; then
echo ""
echo "The mtx-changer script for Bacula"
echo "---------------------------------"
echo ""
echo "usage: mtx-changer <command> <devicename of tapedrive> [slot]"
echo " mtx-changer"
echo ""
echo "Valid commands:"
echo ""
echo "unload Unloads a tape into the slot"
echo " from where it was loaded."
echo "load <slot> Loads a tape from the slot <slot>"
echo "list Lists full storage slots"
echo "loaded Gives slot from where the tape was loaded."
echo " 0 means the tape drive is empty."
echo "slots Gives Number of avialable slots."
echo "volumes List avialable slots and the label of the."
echo " tape in it (slot:volume)"
echo "Example:"
echo " mtx-changer load /dev/nst0 1 loads a tape from slot1"
echo " mtx-changer %a %o %S "
echo ""
exit 0
fi
case "$command" in
unload)
echo "mtx-changer: Unload request on $TAPEDRIVE0" >>$logfile
DRIVESTATUS=`$mt -f $TAPEDRIVE0 status | grep ONLINE`
echo "DriveStatus: $DRIVESTATUS" >>$logfile
if [ -z $DRIVESTATUS ] ; then
echo "Drive is OFFLINE" >>$logfile
else
echo "Drive is ONLINE" >>$logfile
echo "Taking drive offline and ejecting..." >>$logfile
mt -f $TAPEDRIVE0 rewoffl 2>/dev/null
fi
exit 0
;;
load)
echo "mtx-changer: load request on $TAPEDRIVE0" >>$logfile
#Let's check if drive is loaded before we load it
MOUNTED=`echo "status Storage=$storagename" | $console | $grep $TAPEDRIVE0 | $grep "is mounted with"`
if [ -z "$MOUNTED" ] ; then
echo "Drive is UNMOUNTED: $MOUNTED" >>$logfile
else
echo "Drive is MOUNTED: $MOUNTED" >>$logfile
VOLUME=`echo "status Storage=$storagename" | $console | $grep -A2 $TAPEDRIVE0 | $grep "Volume:" | awk {'print $2'}`
echo "Mounted Volume: $VOLUME" >>$logfile
if [ $MOUNTED = $3 ] ; then
echo "$3 is already mounted in $TAPEDRIVE0 as $VOLUME" >>$logfile
exit
else
echo "$VOLUME is currently mounted in $TAPEDRIVE0, unmounting..." >>$logfile
echo "unmount" | $console >/dev/null 2>/dev/null
fi
fi
echo "Taking drive offline and ejecting..." >>$logfile
mt -f $TAPEDRIVE0 rewoffl 2>/dev/null
# extract label for the mail
count=`expr 1`
for label in $labels ; do
if [ $slot -eq $count ] ; then volume=$label ; fi
count=`expr $count + 1`
done
mail -s "Bacula needs volume $volume." $recipient <<END_OF_DATA
Please insert tape labelled $volume from slot $slot into $TAPEDRIVE0.
Kind regards,
Bacula.
END_OF_DATA
sleep $TAPEDELAY
# Wait for the tape to be inserted
STATUS=`$mt -f $TAPEDRIVE0 status | $grep ONLINE`
while [ -z "$STATUS" ] ; do
sleep $TAPEDELAY
STATUS=`$mt -f $TAPEDRIVE0 status | $grep ONLINE`
done
mail -s "Bacula says thank you." $recipient <<END_OF_DATA
Thank you for inserting the new tape! (I requested volume $volume from slot $slot.)
Kind regards,
Bacula.
END_OF_DATA
echo "Successfully loaded a tape into drive $TAPEDRIVE0 (requested $volume from slot $slot)." >>$logfile
echo "Loading finished." ; >>$logfile
echo "$slot"
exit 0
;;
list)
echo "mtx-changer: Requested list" >>$logfile
count=`expr 1`
for label in $labels ; do
echo $count:$label
count=`expr $count + 1`
done
printf "\n"
;;
loaded)
echo "mtx-changer: tape loaded request on $TAPEDRIVE0" >>$logfile
DRIVESTATUS=`mt -f $TAPEDRIVE0 status 2>/dev/null`
MTRTN=$?
if [ $MTRTN = "0" ] ; then
echo "MT ran ok. Return Code: $MTRTN" >>$logfile
DRIVESTATUS=`mt -f $TAPEDRIVE0 status | grep "DR_OPEN"`
if [ -z "$DRIVESTATUS"] ; then
echo "MT says the drive has a tape" >>$logfile
echo "0"
else
echo "MT says the drive is empty" >>$logfile
echo "0"
fi
else
echo "MT error, bacula might be using it! Return code: $MTRTN" >>$logfile
MOUNTED=`echo "status Storage=$storagename" | $console | $grep $TAPEDRIVE0 | $grep "is mounted with"`
if [ -z "$MOUNTED" ] ; then
echo "Drive is UNMOUNTED: $MOUNTED">>$logfile
echo "0"
else
echo "Drive is MOUNTED: $MOUNTED">>$logfile
VOLUME=`echo "status Storage=$storagename" | $console | $grep -A2 $TAPEDRIVE0 | $grep "Volume:" | awk {'print $2'}`
echo "Mounted Volume: $VOLUME" >>$logfile
count=`expr 1`
for label in $labels ; do
if [ $VOLUME = $label ] ; then
echo "$VOLUME = $label in slot $count" >>$logfile
echo "$count"
fi
count=`expr $count + 1`
done
fi
fi
exit 0
;;
slots)
echo "mtx-changer: Request slots" >>$logfile
count=`expr 0`
for label in $labels ; do
count=`expr $count + 1`
done
echo $count
;;
volumes)
echo "mtx-changer: Request volumes" >>$logfile
count=`expr 1`
for label in $labels ; do
printf "$count:$label"
count=`expr $count + 1`
done
printf "\n"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment