3 # The use of this script for Totally Automating Tape usage is
4 # described in the Tips chapter of the manual.
6 # Bacula interface to mtx autoloader
8 # Created OCT/31/03 by Alexander Kuehn, derived from Ludwig Jaffe's script
10 # Works with the HP C1537A L708 DDS3
13 # these are the labels of the tapes in each virtual slot, not the slots!
14 labels="PSE-0001 PSE-0002 PSE-0003 PSE-0004 PSE-0005 PSE-0006 PSE-0007 PSE-0008 PSE-0009 PSE-0010 PSE-0011 PSE-0012"
16 # who to send a mail to?
17 recipient=root@localhost
18 logfile=/var/log/mtx.log
20 # Delay in seconds how often to check whether a new tape has been inserted
21 TAPEDELAY=10 # the default is every 10 seconds
22 echo `date` ":" $@ >>$logfile
24 # change this if mt is not in the path (use different quotes!)
28 # how to run the console application?
29 console="/usr/local/sbin/console -c /usr/local/etc/console.conf"
33 #TAPEDRIVE0 holds the device/name of your 1st and only drive (Bacula supports only 1 drive currently)
34 #Read TAPEDRIVE from command line parameters
41 #Read slot from command line parameters
48 if [ -z "$command" ] ; then
50 echo "The mtx-changer script for Bacula"
51 echo "---------------------------------"
53 echo "usage: mtx-changer <command> <devicename of tapedrive> [slot]"
56 echo "Valid commands:"
58 echo "unload Unloads a tape into the slot"
59 echo " from where it was loaded."
60 echo "load <slot> Loads a tape from the slot <slot>"
61 echo "list Lists full storage slots"
62 echo "loaded Gives slot from where the tape was loaded."
63 echo " 0 means the tape drive is empty."
64 echo "slots Gives Number of avialable slots."
65 echo "volumes List avialable slots and the label of the."
66 echo " tape in it (slot:volume)"
68 echo " mtx-changer load /dev/nst0 1 loads a tape from slot1"
69 echo " mtx-changer %a %o %S "
77 # At first do mt -f /dev/st0 offline to unload the tape
79 # Check if you want to fool me
80 echo "unmount"|$console >/dev/null 2>/dev/null
81 echo "mtx-changer: Checking if drive is loaded before we unload. Request unload" >>$logfile
82 if $mt -f $TAPEDRIVE0 status >/dev/null 2>/dev/null ; then # mt says status ok
83 echo "mtx-changer: Doing mt -f $TAPEDRIVE0 rewoffl to rewind and unload the tape!" >>$logfile
84 $mt -f $TAPEDRIVE0 rewoffl
86 echo "mtx-changer: *** Don't fool me! *** The Drive $TAPEDRIVE0 is empty." >>$logfile
92 #Let's check if drive is loaded before we load it
93 echo "mtx-changer: Checking if drive is loaded before we load. I Request loaded" >>$logfile
94 LOADEDVOL=`echo "status Storage"|$console|$grep $TAPEDRIVE0|grep ^Device|grep -v "not open."|grep -v "ERR="|grep -v "no Bacula volume is mounted"|sed -e s/^.*Volume\ //|cut -d\" -f2`
95 # if [ -z "$LOADEDVOL" ] ; then # this is wrong, becaus Bacula would try to use the tape if we mount it!
96 # LOADEDVOL=`echo "mount"|$console|$grep $TAPEDRIVE0|grep Device|grep -v "not open."|grep -v "ERR="|sed -e s/^.*Volume\ //|cut -d\" -f2`
97 # if [ -z "$LOADEDVOL" ] ; then
98 # echo "mtx-changer: The Drive $TAPEDRIVE0 is empty." >>$logfile
99 # else # restore state?
100 # if [ $LOADEDVOL = $3 ] ; then # requested Volume mounted -> exit
101 # echo "mtx-changer: *** Don't fool me! *** Tape $LOADEDVOL is already in drive $TAPEDRIVE0!" >>$logfile
103 # else # oops, wrong volume
104 # echo "unmount"|$console >/dev/null 2>/dev/null
108 if [ -z "$LOADEDVOL" ] ; then
109 echo "unmount"|$console >/dev/null 2>/dev/null
112 #Check if you want to fool me
113 if [ $LOADEDVOL = $3 ] ; then
114 echo "mtx-changer: *** Don't fool me! *** Tape $LOADEDVOL is already in drive $TAPEDRIVE0!" >>$logfile
117 echo "mtx-changer: The Drive $TAPEDRIVE0 is loaded with the tape $LOADEDVOL" >>$logfile
118 echo "mtx-changer: Unmounting..." >>$logfile
119 echo "unmount"|$console >/dev/null 2>/dev/null
121 echo "mtx-changer: Unloading..." >>$logfile
122 echo "mtx-changer: Doing mt -f $TAPEDRIVE0 rewoffl to rewind and unload the tape!" >>$logfile
123 mt -f $TAPEDRIVE0 rewoffl 2>/dev/null
124 #Now we can load the drive as desired
125 echo "mtx-changer: Doing mtx -f $1 $2 $3" >>$logfile
126 # extract label for the mail
128 for label in $labels ; do
129 if [ $slot -eq $count ] ; then volume=$label ; fi
130 count=`expr $count + 1`
133 mail -s "Bacula needs volume $volume." $recipient <<END_OF_DATA
134 Please insert volume $volume from slot $slot into $TAPEDRIVE0 .
139 $mt status >/dev/null 2>/dev/null
140 while [ $? -ne 0 ] ; do
142 $mt status >/dev/null 2>/dev/null
144 mail -s "Bacula says thank you." $recipient <<END_OF_DATA
145 Thank you for inserting the new tape! (I requested volume $volume from slot $slot.)
149 echo "Successfully loaded a tape into drive $TAPEDRIVE0 (requested $volume from slot $slot)." >>$logfile
150 echo "Loading finished." ; >>$logfile
156 echo "mtx-changer: Requested list" >>$logfile
157 LOADEDVOL=`echo "status Storage"|$console|$grep $TAPEDRIVE0|grep ^Device|grep -v "not open."|grep -v "ERR="|grep -v "no Bacula volume is mounted"|sed -e s/^.*Volume\ //|cut -d\" -f2`
158 if [ -z $LOADEDVOL ] ; then # try mounting
159 LOADEDVOL=`echo "mount"|$console|$grep $TAPEDRIVE0|grep Device|grep -v "not open."|grep -v "ERR="|sed -e s/^.*Volume\ //|cut -d\" -f2`
160 if [ -z $LOADEDVOL ] ; then # no luck
163 echo "unmount"|$console >/dev/null 2>/dev/null
167 for label in $labels ; do
168 if [ "$label" != "$LOADEDVOL" ] ; then
171 count=`expr $count + 1`
177 echo "mtx-changer: Request loaded, dev $TAPEDRIVE0" >>$logfile
178 LOADEDVOL=`echo "status Storage"|$console|$grep $TAPEDRIVE0|grep ^Device|grep -v "not open."|grep -v "ERR="|grep -v "no Bacula volume is mounted"|sed -e s/^.*Volume\ //|cut -d\" -f2`
179 if [ -z $LOADEDVOL ] ; then
180 LOADEDVOL=`echo "mount"|$console|$grep $TAPEDRIVE0|grep Device|grep -v "not open."|grep -v "ERR="|grep -v "no Bacula volume is mounted"|sed -e s/^.*Volume\ //|cut -d\" -f2`
181 if [ -z "$LOADEDVOL" ] ; then # no luck
182 echo "$TAPEDRIVE0 not mounted!" >>$logfile
184 echo "unmount"|$console >/dev/null 2>/dev/null
187 if [ -z "$LOADEDVOL" ] ; then
188 LOADEDVOL="_no_tape" >>$logfile
192 for label in $labels ; do
193 if [ $LOADEDVOL = $label ] ; then echo $count ; fi
194 count=`expr $count + 1`
201 echo "mtx-changer: Request slots" >>$logfile
203 for label in $labels ; do
204 count=`expr $count + 1`
210 echo "mtx-changer: Request volumes" >>$logfile
212 for label in $labels ; do
213 printf "$count:$label "
214 count=`expr $count + 1`