8 # fixed last_cleaned file so that if it doesn't exist, it gets created with current date, not '0,0'
9 # fixed a bug that was reporting the wrong slot # to amcheck
13 # amverify was failing when using -slot current. Fixed exit $code from 1 -> 0.
14 # removed useless $current variables from movetape() function.
26 # Set Path so that it includes Amanda binaries, and access to tapechanger & drive programs
29 exec_prefix="@exec_prefix@"
31 amlibexecdir="@amlibexecdir@"
32 . "${amlibexecdir}/amanda-sh-lib.sh"
34 # add sbin and ucb dirs
35 PATH="$PATH:/usr/sbin:/sbin:/usr/ucb"
40 # Load configuration data from the config file
43 ourconf=`amgetconf changerfile`
47 if [ ! -f "$ourconf" ]; then
49 echo `_ 'Command Line ->'` $myname $@
50 echo `_ 'Exit(%s): %s not found as listed in amanda.conf' "$code" "$ourconf"` 1>&2
56 tmpval1=`grep ^mcutil $ourconf | awk -F\ '{print $2}'`
57 tmpval2=`grep ^mcutil $ourconf | awk -F= '{print $2}'`
58 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
60 echo `_ 'Command Line ->'` $myname $@
61 echo `_ 'Exit(%s): mcutil not specified in %s' "$code" "$ourconf"` 1>&2
63 elif [ -z "$tmpval1" ]; then
71 tmpval1=`grep ^tape $ourconf | awk -F\ '{print $2}'`
72 tmpval2=`grep ^tape $ourconf | awk -F= '{print $2}'`
73 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
75 echo `_ 'Command Line ->'` $myname $@
76 echo `_ 'Exit(%s): tape not specified in %s' "$code" "$ourconf"` 1>&2
78 elif [ -z "$tmpval1" ]; then
86 tmpval1=`grep ^firstslot $ourconf | awk -F\ '{print $2}'`
87 tmpval2=`grep ^firstslot $ourconf | awk -F= '{print $2}'`
88 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
90 echo `_ 'Command Line ->'` $myname $@
91 echo `_ 'Exit(%s): firstslot not specified in %s' "$code" "$ourconf"` 1>&2
93 elif [ -z "$tmpval1" ]; then
101 tmpval1=`grep ^lastslot $ourconf | awk -F\ '{print $2}'`
102 tmpval2=`grep ^lastslot $ourconf | awk -F= '{print $2}'`
103 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
105 echo `_ 'Command Line ->'` $myname $@
106 echo `_ 'Exit(%s): lastslot not specified in %s' "$code" "$ourconf"` 1>&2
107 elif [ -z "$tmpval1" ]; then
114 # grab use_cleaning info
115 tmpval1=`grep ^use_cleaning $ourconf | awk -F\ '{print $2}'`
116 tmpval2=`grep ^use_cleaning $ourconf | awk -F= '{print $2}'`
117 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
119 echo `_ 'Command Line ->'` $myname $@
120 echo `_ 'Exit(%s): use_cleaning not specified in %s' "$code" "$ourconf"` 1>&2
122 elif [ -z "$tmpval1" ]; then
123 use_cleaning=$tmpval2
125 use_cleaning=$tmpval1
129 # grab cleanslot info
130 tmpval1=`grep ^cleanslot $ourconf | awk -F\ '{print $2}'`
131 tmpval2=`grep ^cleanslot $ourconf | awk -F= '{print $2}'`
132 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
134 echo `_ 'Command Line ->'` $myname $@
135 echo `_ 'Exit(%s): cleanslot not specified in %s' "$code" "$ourconf"` 1>&2
137 elif [ -z "$tmpval1" ]; then
144 # grab cleansleep info
145 tmpval1=`grep ^cleansleep $ourconf | awk -F\ '{print $2}'`
146 tmpval2=`grep ^cleansleep $ourconf | awk -F= '{print $2}'`
147 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
149 echo `_ 'Command Line ->'` $myname $@
150 echo `_ 'Exit(%s): cleansleep not specified in %s' "$code" "$ourconf"` 1>&2
152 elif [ -z "$tmpval1" ]; then
160 tmpval1=`grep ^cleanme $ourconf | awk -F\ '{print $2}'`
161 tmpval2=`grep ^cleanme $ourconf | awk -F= '{print $2}'`
162 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
164 echo `_ 'Command Line ->'` $myname $@
165 echo `_ 'Exit(%s): cleanme not specified in %s' "$code" "$ourconf"` 1>&2
167 elif [ -z "$tmpval1" ]; then
174 # grab cleanfile info
175 tmpval1=`grep ^cleanfile $ourconf | awk -F\ '{print $2}'`
176 tmpval2=`grep ^cleanfile $ourconf | awk -F= '{print $2}'`
177 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
179 echo `_ 'Command Line ->'` $myname $@
180 echo `_ 'Exit(%s): cleanfile not specified in %s' "$code" "$ourconf"` 1>&2
182 elif [ -z "$tmpval1" ]; then
190 tmpval1=`grep ^lastfile $ourconf | awk -F\ '{print $2}'`
191 tmpval2=`grep ^lastfile $ourconf | awk -F= '{print $2}'`
192 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
194 echo `_ 'Command Line ->'` $myname $@
195 echo `_ 'Exit(%s): lastfile not specified in %s' "$code" "$ourconf"` 1>&2
197 elif [ -z "$tmpval1" ]; then
204 # grab currentslot info
205 tmpval1=`grep ^currentslot $ourconf | awk -F\ '{print $2}'`
206 tmpval2=`grep ^currentslot $ourconf | awk -F= '{print $2}'`
207 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
209 echo `_ 'Command Line ->'` $myname $@
210 echo `_ 'Exit(%s): currentslot not specified in %s' "$code" "$ourconf"` 1>&2
212 elif [ -z "$tmpval1" ]; then
220 tmpval1=`grep ^logfile $ourconf | awk -F\ '{print $2}'`
221 tmpval2=`grep ^logfile $ourconf | awk -F= '{print $2}'`
222 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
224 echo `_ 'Command Line ->'` $myname $@
225 echo `_ 'Exit(%s): logfile not specified in %s' "$code" "$ourconf"` 1>&2
227 elif [ -z "$tmpval1" ]; then
233 [ ! -w $logfile ] && logfile=/dev/null
236 # grab slot0source info
237 tmpval1=`grep ^slot0source $ourconf | awk -F\ '{print $2}'`
238 tmpval2=`grep ^slot0source $ourconf | awk -F= '{print $2}'`
239 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
241 echo `_ 'Command Line ->'` $myname $@
242 echo `_ 'Exit(%s): slot0source not specified in %s' "$code" "$ourconf"` 1>&2
244 elif [ -z "$tmpval1" ]; then
253 # Verify currentslot contains a value
255 if [ ! -f $currentslot ] || [ `cat $currentslot` -lt $firstslot ];then
257 echo $used > $currentslot
260 current=`cat $currentslot`
263 # Start logging to $logfile
266 echo "==== `date` ====" >> $logfile
267 echo `_ 'Command Line ->'` $myname $@ >> $logfile
271 # is Use Cleaning activated?
273 if [ $use_cleaning -eq 1 ]; then
277 [ ! -f $cleanfile ] && echo 0 > $cleanfile
278 [ ! -f $lastfile ] && echo $curday,$curyear > $lastfile
282 # Check to see when tape drive was last cleaned
283 # output warning message if it's been too long
284 # Currently, if it's been more than 45days, then
285 # an error message is displayed everytime the
286 # script is called, until the clean parameter
289 cleaned=`cat $cleanfile`
290 lastcleaned=`cut -d, -f1 $lastfile`
291 yearcleaned=`cut -d, -f2 $lastfile`
293 if [ `expr $curday - $lastcleaned` -lt 0 ];then
294 diffday=`expr $curday - $lastcleaned + 365`
295 diffyear=`expr $curyear - $yearcleaned - 1`
297 diffday=`expr $curday - $lastcleaned`
298 diffyear=`expr $curyear - $yearcleaned`
301 if [ $diffday -gt $cleanme ] || [ $diffyear -ge 1 ];then
302 if [ $diffyear -ge 1 ];then
303 echo `_ "Warning, it's been %s year(s) & %s day(s) since you last cleaned the tape drive!" "$diffyear" "$diffday"`
305 echo `_ "Warning, it's been %s day(s) since you last cleaned the tape drive!" "$diffday"`
313 # Read if there is a tape in the tape drive
314 # If so, determine what slot is the tape from
317 echo `_ "querying tape drive....."` >> $logfile
318 used=`expr \`$MCUTIL -e drive | tr = \] | cut -d\] -f2\` - $slot0source`
319 echo `_ " Done"` >> $logfile
321 # Give changer a chance to reset itself
327 # If tape is in the drive, eject it
330 echo `_ "tape drive eject was called"` >> $logfile
334 if [ $used -ge $firstslot ];then
335 $MCUTIL -m drive slot:$used
341 if [ $code -eq 0 ];then
342 answer=`_ 'Cartridge %s successfully ejected from %s' "$used" "$tape"`
343 echo `_ "Exit(%s): %s" "$code" "$answer"` >> $logfile
344 echo $current $answer #For amtape output
346 elif [ $code -eq 1 ];then
347 answer=`_ "No Cartridge in Tape Drive"`
348 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
349 echo $current $answer #For amtape output
352 answer=`_ 'Tape abnormally failed'`
353 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
354 echo $current $answer #For amtape output
361 # reset tape drive to a current state.
362 # This involves ejecting the current tape (if occupied)
363 # and inserting the tape in $firstslot
366 echo `_ 'tape drive reset was called'` >> $logfile
370 if [ $used -ge $firstslot ];then
374 res=`$MCUTIL -m slot:$firstslot drive`
378 if [ $code -eq 0 ];then
379 echo $firstslot > $currentslot
380 answer=`_ '%s - Tape drive was successfully reset' "$firstslot"`
381 elif [ $code -eq 1 ];then
382 answer=`_ '%s - Tape drive reset failed\nCommand -> %s' "$firstslot" "$res"`
385 answer=`_ '%s - Tape abnormally failed -> %s' "$firstslot" "$res"`
388 echo `_ 'Exit(%s): slot %s' "$code" "$answer"` >> $logfile
389 echo $firstslot #For amtape output
397 # Load a specific cartridge into the changer
400 echo `_ "loadslot was called"` >> $logfile
408 if [ $current -ge $firstslot ];then
417 [ $used -lt $firstslot ] && used=$current
419 load=`expr $used + 1`
420 [ $load -gt $lastslot ] && load=$firstslot
422 if [ $whichslot = advance ];then
423 echo $load > $currentslot
425 answer=`_ 'advancing to slot %s' "$load"`
426 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
434 [ $used -lt $firstslot ] && used=$current
436 load=`expr $used - 1`
437 [ $load -lt $firstslot ] && load=$lastslot
448 [$firstslot-$lastslot])
453 if [ use_cleaning -eq 1 ];then
456 $MCUTIL slot:$cleanslot drive
458 echo "$curday,$curyear" > $lastfile
459 echo `expr $cleaned + 1` > $cleanfile
463 answer=`_ "Cleaning not enabled in config"`
464 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
465 echo $cleanslot $answer
471 answer=`_ '"%s" invalid menu option' "$whichslot"`
472 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
481 # sub-function that slot calls to actually eject the tape
482 # & load in the correct slot cartridge
486 # If the requested slot is already loaded in the tape drive
487 if [ $load -eq $used ]; then
489 answer=`_ 'slot %s is already loaded' "$load"`
490 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
491 echo $load $tape # For amtape output
493 elif [ $used -ge $firstslot ];then
497 echo $load $tape # For amtape output
500 echo `_ 'Loading slot %s into Tape drive' "$load"` >> $logfile
501 $MCUTIL -m slot:$load drive
504 if [ $code -eq 0 ];then
505 echo $load > $currentslot
506 answer=`_ 'Cartridge %s successfully loaded in Tape drive' "$load"`
508 answer=`_ 'Cartridge %s failed to load in Tape drive' "$load"`
510 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
516 echo `_ 'tape drive info was called'` >> $logfile
520 if [ $used -lt 0 ];then
525 answer="$used $lastslot 1"
526 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
553 echo `_ '-slot {current|next|previous|first|last|%s-%s|clean}' "$firstslot" "$lastslot"`
554 echo `_ ' current - show contents of current slot'`
555 echo `_ ' next - load tape from next slot'`
556 echo `_ ' previous - load tape from previous slot'`
557 echo `_ ' first - load tape from first slot'`
558 echo `_ ' last - load tape from last slot'`
559 echo `_ ' %s - %s - load tape from slot <slot #>' "$firstslot" "$lastslot"`
560 echo `_ ' clean - Clean the drive'`
561 echo `_ '-device : Show current tape device'`
562 echo `_ '-reset : Reset changer to known state'`
563 echo `_ '-eject : Eject current tape from drive'`
564 echo `_ '-info : Output {current slot | # of slots | can changer go backwards}'`
565 echo `_ '-help : Display this help'`
568 echo `_ "<usage> %s -{slot|device|reset|eject|help}" "$myname"`