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
264 echo "\n\n==== `date` ====" >> $logfile
265 echo `_ 'Command Line ->'` $myname $@ >> $logfile
269 # is Use Cleaning activated?
271 if [ $use_cleaning -eq 1 ]; then
275 [ ! -f $cleanfile ] && echo 0 > $cleanfile
276 [ ! -f $lastfile ] && echo $curday,$curyear > $lastfile
280 # Check to see when tape drive was last cleaned
281 # output warning message if it's been too long
282 # Currently, if it's been more than 45days, then
283 # an error message is displayed everytime the
284 # script is called, until the clean parameter
287 cleaned=`cat $cleanfile`
288 lastcleaned=`cut -d, -f1 $lastfile`
289 yearcleaned=`cut -d, -f2 $lastfile`
291 if [ `expr $curday - $lastcleaned` -lt 0 ];then
292 diffday=`expr $curday - $lastcleaned + 365`
293 diffyear=`expr $curyear - $yearcleaned - 1`
295 diffday=`expr $curday - $lastcleaned`
296 diffyear=`expr $curyear - $yearcleaned`
299 if [ $diffday -gt $cleanme ] || [ $diffyear -ge 1 ];then
300 if [ $diffyear -ge 1 ];then
301 echo `_ "Warning, it's been %s year(s) & %s day(s) since you last cleaned the tape drive!" "$diffyear" "$diffday"`
303 echo `_ "Warning, it's been %s day(s) since you last cleaned the tape drive!" "$diffday"`
311 # Read if there is a tape in the tape drive
312 # If so, determine what slot is the tape from
315 echo `_ "querying tape drive....."` >> $logfile
316 used=`expr \`$MCUTIL -e drive | tr = \] | cut -d\] -f2\` - $slot0source`
317 echo `_ " Done"` >> $logfile
319 # Give changer a chance to reset itself
325 # If tape is in the drive, eject it
328 echo `_ "tape drive eject was called"` >> $logfile
332 if [ $used -ge $firstslot ];then
333 $MCUTIL -m drive slot:$used
339 if [ $code -eq 0 ];then
340 answer=`_ 'Cartridge %s successfully ejected from %s' "$used" "$tape"`
341 echo `_ "Exit(%s): %s" "$code" "$answer"` >> $logfile
342 echo $current $answer #For amtape output
344 elif [ $code -eq 1 ];then
345 answer=`_ "No Cartridge in Tape Drive"`
346 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
347 echo $current $answer #For amtape output
350 answer=`_ 'Tape abnormally failed'`
351 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
352 echo $current $answer #For amtape output
359 # reset tape drive to a current state.
360 # This involves ejecting the current tape (if occupied)
361 # and inserting the tape in $firstslot
364 echo `_ 'tape drive reset was called'` >> $logfile
368 if [ $used -ge $firstslot ];then
372 res=`$MCUTIL -m slot:$firstslot drive`
376 if [ $code -eq 0 ];then
377 echo $firstslot > $currentslot
378 answer=`_ '%s - Tape drive was successfully reset' "$firstslot"`
379 elif [ $code -eq 1 ];then
380 answer=`_ '%s - Tape drive reset failed\nCommand -> %s' "$firstslot" "$res"`
383 answer=`_ '%s - Tape abnormally failed -> %s' "$firstslot" "$res"`
386 echo `_ 'Exit(%s): slot %s' "$code" "$answer"` >> $logfile
387 echo $firstslot #For amtape output
395 # Load a specific cartridge into the changer
398 echo `_ "loadslot was called"` >> $logfile
406 if [ $current -ge $firstslot ];then
415 [ $used -lt $firstslot ] && used=$current
417 load=`expr $used + 1`
418 [ $load -gt $lastslot ] && load=$firstslot
420 if [ $whichslot = advance ];then
421 echo $load > $currentslot
423 answer=`_ 'advancing to slot %s' "$load"`
424 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
432 [ $used -lt $firstslot ] && used=$current
434 load=`expr $used - 1`
435 [ $load -lt $firstslot ] && load=$lastslot
446 [$firstslot-$lastslot])
451 if [ use_cleaning -eq 1 ];then
454 $MCUTIL slot:$cleanslot drive
456 echo "$curday,$curyear" > $lastfile
457 echo `expr $cleaned + 1` > $cleanfile
461 answer=`_ "Cleaning not enabled in config"`
462 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
463 echo $cleanslot $answer
469 answer=`_ '"%s" invalid menu option' "$whichslot"`
470 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
479 # sub-function that slot calls to actually eject the tape
480 # & load in the correct slot cartridge
484 # If the requested slot is already loaded in the tape drive
485 if [ $load -eq $used ]; then
487 answer=`_ 'slot %s is already loaded' "$load"`
488 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
489 echo $load $tape # For amtape output
491 elif [ $used -ge $firstslot ];then
495 echo $load $tape # For amtape output
498 echo `_ 'Loading slot %s into Tape drive' "$load"` >> $logfile
499 $MCUTIL -m slot:$load drive
502 if [ $code -eq 0 ];then
503 echo $load > $currentslot
504 answer=`_ 'Cartridge %s successfully loaded in Tape drive' "$load"`
506 answer=`_ 'Cartridge %s failed to load in Tape drive' "$load"`
508 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
514 echo `_ 'tape drive info was called'` >> $logfile
518 if [ $used -lt 0 ];then
523 answer="$used $lastslot 1"
524 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
551 echo `_ '-slot {current|next|previous|first|last|%s-%s|clean}' "$firstslot" "$lastslot"`
552 echo `_ ' current - show contents of current slot'`
553 echo `_ ' next - load tape from next slot'`
554 echo `_ ' previous - load tape from previous slot'`
555 echo `_ ' first - load tape from first slot'`
556 echo `_ ' last - load tape from last slot'`
557 echo `_ ' %s - %s - load tape from slot <slot #>' "$firstslot" "$lastslot"`
558 echo `_ ' clean - Clean the drive'`
559 echo `_ '-device : Show current tape device'`
560 echo `_ '-reset : Reset changer to known state'`
561 echo `_ '-eject : Eject current tape from drive'`
562 echo `_ '-info : Output {current slot | # of slots | can changer go backwards}'`
563 echo `_ '-help : Display this help'`
566 echo `_ "<usage> %s -{slot|device|reset|eject|help}" "$myname"`