3 # Bacula interface to mtx autoloader
5 # Created OCT/31/03 by Alexander Kuehn, derived from Ludwig Jaffe's script
7 # Works with the HP C1537A L708 DDS3
10 # these are the labels of the tapes in each virtual slot, not the slots!
11 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"
13 # who to send a mail to?
14 recipient=root@localhost
15 logfile=/var/log/mtx.log
17 # Delay in seconds how often to check whether a new tape has been inserted
18 TAPEDELAY=10 # the default is every 10 seconds
19 echo `date` ":" $@ >>$logfile
21 # change this if mt is not in the path (use different quotes!)
25 # how to run the console application?
26 console="/usr/local/sbin/console -c /usr/local/etc/console.conf"
30 #TAPEDRIVE0 holds the device/name of your 1st and only drive (Bacula supports only 1 drive currently)
31 #Read TAPEDRIVE from command line parameters
38 #Read slot from command line parameters
45 if [ -z "$command" ] ; then
47 echo "The mtx-changer script for Bacula"
48 echo "---------------------------------"
50 echo "usage: mtx-changer <command> <devicename of tapedrive> [slot]"
53 echo "Valid commands:"
55 echo "unload Unloads a tape into the slot"
56 echo " from where it was loaded."
57 echo "load <slot> Loads a tape from the slot <slot>"
58 echo "list Lists full storage slots"
59 echo "loaded Gives slot from where the tape was loaded."
60 echo " 0 means the tape drive is empty."
61 echo "slots Gives Number of avialable slots."
62 echo "volumes List avialable slots and the label of the."
63 echo " tape in it (slot:volume)"
65 echo " mtx-changer load /dev/nst0 1 loads a tape from slot1"
66 echo " mtx-changer %a %o %S "
74 # At first do mt -f /dev/st0 offline to unload the tape
76 # Check if you want to fool me
77 echo "unmount"|$console >/dev/null 2>/dev/null
78 echo "mtx-changer: Checking if drive is loaded before we unload. Request unload" >>$logfile
79 if $mt -f $TAPEDRIVE0 status >/dev/null 2>/dev/null ; then # mt says status ok
80 echo "mtx-changer: Doing mt -f $TAPEDRIVE0 rewoffl to rewind and unload the tape!" >>$logfile
81 $mt -f $TAPEDRIVE0 rewoffl
83 echo "mtx-changer: *** Don't fool me! *** The Drive $TAPEDRIVE0 is empty." >>$logfile
89 #Let's check if drive is loaded before we load it
90 echo "mtx-changer: Checking if drive is loaded before we load. I Request loaded" >>$logfile
91 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`
92 # if [ -z "$LOADEDVOL" ] ; then # this is wrong, becaus Bacula would try to use the tape if we mount it!
93 # LOADEDVOL=`echo "mount"|$console|$grep $TAPEDRIVE0|grep Device|grep -v "not open."|grep -v "ERR="|sed -e s/^.*Volume\ //|cut -d\" -f2`
94 # if [ -z "$LOADEDVOL" ] ; then
95 # echo "mtx-changer: The Drive $TAPEDRIVE0 is empty." >>$logfile
96 # else # restore state?
97 # if [ $LOADEDVOL = $3 ] ; then # requested Volume mounted -> exit
98 # echo "mtx-changer: *** Don't fool me! *** Tape $LOADEDVOL is already in drive $TAPEDRIVE0!" >>$logfile
100 # else # oops, wrong volume
101 # echo "unmount"|$console >/dev/null 2>/dev/null
105 if [ -z "$LOADEDVOL" ] ; then
106 echo "unmount"|$console >/dev/null 2>/dev/null
109 #Check if you want to fool me
110 if [ $LOADEDVOL = $3 ] ; then
111 echo "mtx-changer: *** Don't fool me! *** Tape $LOADEDVOL is already in drive $TAPEDRIVE0!" >>$logfile
114 echo "mtx-changer: The Drive $TAPEDRIVE0 is loaded with the tape $LOADEDVOL" >>$logfile
115 echo "mtx-changer: Unmounting..." >>$logfile
116 echo "unmount"|$console >/dev/null 2>/dev/null
118 echo "mtx-changer: Unloading..." >>$logfile
119 echo "mtx-changer: Doing mt -f $TAPEDRIVE0 rewoffl to rewind and unload the tape!" >>$logfile
120 mt -f $TAPEDRIVE0 rewoffl 2>/dev/null
121 #Now we can load the drive as desired
122 echo "mtx-changer: Doing mtx -f $1 $2 $3" >>$logfile
123 # extract label for the mail
125 for label in $labels ; do
126 if [ $slot -eq $count ] ; then volume=$label ; fi
127 count=`expr $count + 1`
130 mail -s "Bacula needs volume $volume." $recipient <<END_OF_DATA
131 Please insert volume $volume from slot $slot into $TAPEDRIVE0 .
136 $mt status >/dev/null 2>/dev/null
137 while [ $? -ne 0 ] ; do
139 $mt status >/dev/null 2>/dev/null
141 mail -s "Bacula says thank you." $recipient <<END_OF_DATA
142 Thank you for inserting the new tape! (I requested volume $volume from slot $slot.)
146 echo "Successfully loaded a tape into drive $TAPEDRIVE0 (requested $volume from slot $slot)." >>$logfile
147 echo "Loading finished." ; >>$logfile
153 echo "mtx-changer: Requested list" >>$logfile
154 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`
155 if [ -z $LOADEDVOL ] ; then # try mounting
156 LOADEDVOL=`echo "mount"|$console|$grep $TAPEDRIVE0|grep Device|grep -v "not open."|grep -v "ERR="|sed -e s/^.*Volume\ //|cut -d\" -f2`
157 if [ -z $LOADEDVOL ] ; then # no luck
160 echo "unmount"|$console >/dev/null 2>/dev/null
164 for label in $labels ; do
165 if [ "$label" != "$LOADEDVOL" ] ; then
168 count=`expr $count + 1`
174 echo "mtx-changer: Request loaded, dev $TAPEDRIVE0" >>$logfile
175 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`
176 if [ -z $LOADEDVOL ] ; then
177 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`
178 if [ -z "$LOADEDVOL" ] ; then # no luck
179 echo "$TAPEDRIVE0 not mounted!" >>$logfile
181 echo "unmount"|$console >/dev/null 2>/dev/null
184 if [ -z "$LOADEDVOL" ] ; then
185 LOADEDVOL="_no_tape" >>$logfile
189 for label in $labels ; do
190 if [ $LOADEDVOL = $label ] ; then echo $count ; fi
191 count=`expr $count + 1`
198 echo "mtx-changer: Request slots" >>$logfile
200 for label in $labels ; do
201 count=`expr $count + 1`
207 echo "mtx-changer: Request volumes" >>$logfile
209 for label in $labels ; do
210 printf "$count:$label "
211 count=`expr $count + 1`