]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/ak-mtx-changer.txt
Make out of freespace non-fatal for removable devices -- i.e. behaves like tape
[bacula/bacula] / bacula / examples / autochangers / ak-mtx-changer.txt
1 #!/bin/sh
2 #
3 # The use of this script for Totally Automating Tape usage is
4 #   described in the Tips chapter of the manual.
5 #
6 # Bacula interface to mtx autoloader
7 #
8 #   Created  OCT/31/03 by Alexander Kuehn, derived from Ludwig Jaffe's script
9 #
10 #  Works with the HP C1537A L708 DDS3
11 #
12 #set -x
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"
15
16 # who to send a mail to?
17 recipient=root@localhost
18 logfile=/var/log/mtx.log
19
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
23
24 # change this if mt is not in the path (use different quotes!)
25 mt=`which mt`
26 grep=`which grep`
27 #
28 # how to run the console application?
29 console="/usr/local/sbin/console -c /usr/local/etc/console.conf"
30
31 command="$1"
32
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
35 if [ -z "$2" ] ; then
36   TAPEDRIVE0=/dev/nsa0 
37 else
38   TAPEDRIVE0=$2 
39 fi
40
41 #Read slot from command line parameters
42 if [ -z "$3" ] ; then
43   slot=`expr 1`
44 else
45   slot=`expr $3`
46 fi
47
48 if [ -z "$command" ] ; then
49   echo ""
50   echo "The mtx-changer script for Bacula"
51   echo "---------------------------------"
52   echo ""
53   echo "usage: mtx-changer <command> <devicename of tapedrive> [slot]"
54   echo "       mtx-changer"
55   echo ""
56   echo "Valid commands:"
57   echo ""
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)"
67   echo "Example:"
68   echo "  mtx-changer load /dev/nst0 1   loads a tape from slot1"
69   echo "  mtx-changer %a %o %S   "
70   echo ""
71   exit 0
72 fi
73
74
75 case "$command" in 
76         unload)
77                 # At first do mt -f /dev/st0 offline to unload the tape 
78                 #
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
85                 else
86                         echo "mtx-changer:  *** Don't fool me! *** The Drive $TAPEDRIVE0 is empty." >>$logfile
87                 fi
88                 exit 0
89                 ;;
90         
91    load)
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
102 #                                       exit
103 #                               else                                    # oops, wrong volume
104 #                                       echo "unmount"|$console >/dev/null 2>/dev/null
105 #                               fi
106 #                       fi
107 #               fi
108                 if [ -z "$LOADEDVOL" ] ; then
109                         echo "unmount"|$console >/dev/null 2>/dev/null
110                         LOADEDVOL=0
111                 else
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
115                                 exit
116                         fi
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
120                 fi
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
127                 count=`expr 1`
128                 for label in $labels ; do
129                         if [ $slot -eq $count ] ; then volume=$label ; fi
130                         count=`expr $count + 1`
131                 done
132
133                 mail -s "Bacula needs volume $volume." $recipient <<END_OF_DATA
134 Please insert volume $volume from slot $slot into $TAPEDRIVE0 .
135 Kind regards,
136 Bacula.
137 END_OF_DATA
138                 sleep 15
139                 $mt status >/dev/null 2>/dev/null
140                 while [ $? -ne 0 ] ; do
141                         sleep $TAPEDELAY
142                         $mt status >/dev/null 2>/dev/null
143                 done
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.)
146 Kind regards,
147 Bacula.
148 END_OF_DATA
149                 echo "Successfully loaded a tape into drive $TAPEDRIVE0 (requested $volume from slot $slot)." >>$logfile
150                 echo "Loading finished." ; >>$logfile
151                 echo "$slot"
152                 exit 0
153                 ;;
154
155         list) 
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
161                                 LOADEDVOL="_no_tape"
162                         else                                                    # restore state
163                                 echo "unmount"|$console >/dev/null 2>/dev/null
164                         fi
165                 fi
166                 count=`expr 1`
167                 for label in $labels ; do
168                         if [ "$label" != "$LOADEDVOL" ] ; then
169                                 printf "$count "
170                         fi
171                         count=`expr $count + 1`
172                 done
173                 printf "\n"
174       ;;
175
176    loaded)
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
183                         else                                                    # restore state
184                                 echo "unmount"|$console >/dev/null 2>/dev/null
185                         fi
186                 fi
187                 if [ -z "$LOADEDVOL" ] ; then 
188                         LOADEDVOL="_no_tape" >>$logfile
189                         echo "0"
190                 else
191                         count=`expr 1`
192                         for label in $labels ; do
193                                 if [ $LOADEDVOL = $label ] ; then echo $count ; fi
194                                 count=`expr $count + 1`
195                         done
196                 fi
197                 exit 0
198                 ;;
199
200    slots)
201                 echo "mtx-changer: Request slots" >>$logfile
202                 count=`expr 0`
203                 for label in $labels ; do
204                         count=`expr $count + 1`
205                 done
206                 echo $count
207       ;;
208
209    volumes)
210                 echo "mtx-changer: Request volumes" >>$logfile
211                 count=`expr 1`
212                 for label in $labels ; do
213                         printf "$count:$label "
214                         count=`expr $count + 1`
215                 done
216                 printf "\n"
217       ;;
218 esac