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 # Define Suffix for amanda binaries
42 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
43 if test "$USE_VERSION_SUFFIXES" = "yes"; then
50 # Load configuration data from the config file
53 ourconf=`amgetconf$SUF changerfile`
57 if [ ! -f "$ourconf" ]; then
59 echo `_ 'Command Line ->'` $myname $@
60 echo `_ 'Exit(%s): %s not found as listed in amanda.conf' "$code" "$ourconf"` 1>&2
66 tmpval1=`grep ^mcutil $ourconf | awk -F\ '{print $2}'`
67 tmpval2=`grep ^mcutil $ourconf | awk -F= '{print $2}'`
68 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
70 echo `_ 'Command Line ->'` $myname $@
71 echo `_ 'Exit(%s): mcutil not specified in %s' "$code" "$ourconf"` 1>&2
73 elif [ -z "$tmpval1" ]; then
81 tmpval1=`grep ^tape $ourconf | awk -F\ '{print $2}'`
82 tmpval2=`grep ^tape $ourconf | awk -F= '{print $2}'`
83 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
85 echo `_ 'Command Line ->'` $myname $@
86 echo `_ 'Exit(%s): tape not specified in %s' "$code" "$ourconf"` 1>&2
88 elif [ -z "$tmpval1" ]; then
96 tmpval1=`grep ^firstslot $ourconf | awk -F\ '{print $2}'`
97 tmpval2=`grep ^firstslot $ourconf | awk -F= '{print $2}'`
98 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
100 echo `_ 'Command Line ->'` $myname $@
101 echo `_ 'Exit(%s): firstslot not specified in %s' "$code" "$ourconf"` 1>&2
103 elif [ -z "$tmpval1" ]; then
111 tmpval1=`grep ^lastslot $ourconf | awk -F\ '{print $2}'`
112 tmpval2=`grep ^lastslot $ourconf | awk -F= '{print $2}'`
113 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
115 echo `_ 'Command Line ->'` $myname $@
116 echo `_ 'Exit(%s): lastslot not specified in %s' "$code" "$ourconf"` 1>&2
117 elif [ -z "$tmpval1" ]; then
124 # grab use_cleaning info
125 tmpval1=`grep ^use_cleaning $ourconf | awk -F\ '{print $2}'`
126 tmpval2=`grep ^use_cleaning $ourconf | awk -F= '{print $2}'`
127 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
129 echo `_ 'Command Line ->'` $myname $@
130 echo `_ 'Exit(%s): use_cleaning not specified in %s' "$code" "$ourconf"` 1>&2
132 elif [ -z "$tmpval1" ]; then
133 use_cleaning=$tmpval2
135 use_cleaning=$tmpval1
139 # grab cleanslot info
140 tmpval1=`grep ^cleanslot $ourconf | awk -F\ '{print $2}'`
141 tmpval2=`grep ^cleanslot $ourconf | awk -F= '{print $2}'`
142 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
144 echo `_ 'Command Line ->'` $myname $@
145 echo `_ 'Exit(%s): cleanslot not specified in %s' "$code" "$ourconf"` 1>&2
147 elif [ -z "$tmpval1" ]; then
154 # grab cleansleep info
155 tmpval1=`grep ^cleansleep $ourconf | awk -F\ '{print $2}'`
156 tmpval2=`grep ^cleansleep $ourconf | awk -F= '{print $2}'`
157 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
159 echo `_ 'Command Line ->'` $myname $@
160 echo `_ 'Exit(%s): cleansleep not specified in %s' "$code" "$ourconf"` 1>&2
162 elif [ -z "$tmpval1" ]; then
170 tmpval1=`grep ^cleanme $ourconf | awk -F\ '{print $2}'`
171 tmpval2=`grep ^cleanme $ourconf | awk -F= '{print $2}'`
172 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
174 echo `_ 'Command Line ->'` $myname $@
175 echo `_ 'Exit(%s): cleanme not specified in %s' "$code" "$ourconf"` 1>&2
177 elif [ -z "$tmpval1" ]; then
184 # grab cleanfile info
185 tmpval1=`grep ^cleanfile $ourconf | awk -F\ '{print $2}'`
186 tmpval2=`grep ^cleanfile $ourconf | awk -F= '{print $2}'`
187 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
189 echo `_ 'Command Line ->'` $myname $@
190 echo `_ 'Exit(%s): cleanfile not specified in %s' "$code" "$ourconf"` 1>&2
192 elif [ -z "$tmpval1" ]; then
200 tmpval1=`grep ^lastfile $ourconf | awk -F\ '{print $2}'`
201 tmpval2=`grep ^lastfile $ourconf | awk -F= '{print $2}'`
202 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
204 echo `_ 'Command Line ->'` $myname $@
205 echo `_ 'Exit(%s): lastfile not specified in %s' "$code" "$ourconf"` 1>&2
207 elif [ -z "$tmpval1" ]; then
214 # grab currentslot info
215 tmpval1=`grep ^currentslot $ourconf | awk -F\ '{print $2}'`
216 tmpval2=`grep ^currentslot $ourconf | awk -F= '{print $2}'`
217 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
219 echo `_ 'Command Line ->'` $myname $@
220 echo `_ 'Exit(%s): currentslot not specified in %s' "$code" "$ourconf"` 1>&2
222 elif [ -z "$tmpval1" ]; then
230 tmpval1=`grep ^logfile $ourconf | awk -F\ '{print $2}'`
231 tmpval2=`grep ^logfile $ourconf | awk -F= '{print $2}'`
232 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
234 echo `_ 'Command Line ->'` $myname $@
235 echo `_ 'Exit(%s): logfile not specified in %s' "$code" "$ourconf"` 1>&2
237 elif [ -z "$tmpval1" ]; then
243 [ ! -w $logfile ] && logfile=/dev/null
246 # grab slot0source info
247 tmpval1=`grep ^slot0source $ourconf | awk -F\ '{print $2}'`
248 tmpval2=`grep ^slot0source $ourconf | awk -F= '{print $2}'`
249 if [ -z "$tmpval1" ] && [ -z "$tmpval2" ]; then
251 echo `_ 'Command Line ->'` $myname $@
252 echo `_ 'Exit(%s): slot0source not specified in %s' "$code" "$ourconf"` 1>&2
254 elif [ -z "$tmpval1" ]; then
263 # Verify currentslot contains a value
265 if [ ! -f $currentslot ] || [ `cat $currentslot` -lt $firstslot ];then
267 echo $used > $currentslot
270 current=`cat $currentslot`
273 # Start logging to $logfile
276 echo "==== `date` ====" >> $logfile
277 echo `_ 'Command Line ->'` $myname $@ >> $logfile
281 # is Use Cleaning activated?
283 if [ $use_cleaning -eq 1 ]; then
287 [ ! -f $cleanfile ] && echo 0 > $cleanfile
288 [ ! -f $lastfile ] && echo $curday,$curyear > $lastfile
292 # Check to see when tape drive was last cleaned
293 # output warning message if it's been too long
294 # Currently, if it's been more than 45days, then
295 # an error message is displayed everytime the
296 # script is called, until the clean parameter
299 cleaned=`cat $cleanfile`
300 lastcleaned=`cut -d, -f1 $lastfile`
301 yearcleaned=`cut -d, -f2 $lastfile`
303 if [ `expr $curday - $lastcleaned` -lt 0 ];then
304 diffday=`expr $curday - $lastcleaned + 365`
305 diffyear=`expr $curyear - $yearcleaned - 1`
307 diffday=`expr $curday - $lastcleaned`
308 diffyear=`expr $curyear - $yearcleaned`
311 if [ $diffday -gt $cleanme ] || [ $diffyear -ge 1 ];then
312 if [ $diffyear -ge 1 ];then
313 echo `_ "Warning, it's been %s year(s) & %s day(s) since you last cleaned the tape drive!" "$diffyear" "$diffday"`
315 echo `_ "Warning, it's been %s day(s) since you last cleaned the tape drive!" "$diffday"`
323 # Read if there is a tape in the tape drive
324 # If so, determine what slot is the tape from
327 echo `_ "querying tape drive....."` >> $logfile
328 used=`expr \`$MCUTIL -e drive | tr = \] | cut -d\] -f2\` - $slot0source`
329 echo `_ " Done"` >> $logfile
331 # Give changer a chance to reset itself
337 # If tape is in the drive, eject it
340 echo `_ "tape drive eject was called"` >> $logfile
344 if [ $used -ge $firstslot ];then
345 $MCUTIL -m drive slot:$used
351 if [ $code -eq 0 ];then
352 answer=`_ 'Cartridge %s successfully ejected from %s' "$used" "$tape"`
353 echo `_ "Exit(%s): %s" "$code" "$answer"` >> $logfile
354 echo $current $answer #For amtape output
356 elif [ $code -eq 1 ];then
357 answer=`_ "No Cartridge in Tape Drive"`
358 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
359 echo $current $answer #For amtape output
362 answer=`_ 'Tape abnormally failed'`
363 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
364 echo $current $answer #For amtape output
371 # reset tape drive to a current state.
372 # This involves ejecting the current tape (if occupied)
373 # and inserting the tape in $firstslot
376 echo `_ 'tape drive reset was called'` >> $logfile
380 if [ $used -ge $firstslot ];then
384 res=`$MCUTIL -m slot:$firstslot drive`
388 if [ $code -eq 0 ];then
389 echo $firstslot > $currentslot
390 answer=`_ '%s - Tape drive was successfully reset' "$firstslot"`
391 elif [ $code -eq 1 ];then
392 answer=`_ '%s - Tape drive reset failed\nCommand -> %s' "$firstslot" "$res"`
395 answer=`_ '%s - Tape abnormally failed -> %s' "$firstslot" "$res"`
398 echo `_ 'Exit(%s): slot %s' "$code" "$answer"` >> $logfile
399 echo $firstslot #For amtape output
407 # Load a specific cartridge into the changer
410 echo `_ "loadslot was called"` >> $logfile
418 if [ $current -ge $firstslot ];then
427 [ $used -lt $firstslot ] && used=$current
429 load=`expr $used + 1`
430 [ $load -gt $lastslot ] && load=$firstslot
432 if [ $whichslot = advance ];then
433 echo $load > $currentslot
435 answer=`_ 'advancing to slot %s' "$load"`
436 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
444 [ $used -lt $firstslot ] && used=$current
446 load=`expr $used - 1`
447 [ $load -lt $firstslot ] && load=$lastslot
458 [$firstslot-$lastslot])
463 if [ use_cleaning -eq 1 ];then
466 $MCUTIL slot:$cleanslot drive
468 echo "$curday,$curyear" > $lastfile
469 echo `expr $cleaned + 1` > $cleanfile
473 answer=`_ "Cleaning not enabled in config"`
474 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
475 echo $cleanslot $answer
481 answer=`_ '"%s" invalid menu option' "$whichslot"`
482 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
491 # sub-function that slot calls to actually eject the tape
492 # & load in the correct slot cartridge
496 # If the requested slot is already loaded in the tape drive
497 if [ $load -eq $used ]; then
499 answer=`_ 'slot %s is already loaded' "$load"`
500 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
501 echo $load $tape # For amtape output
503 elif [ $used -ge $firstslot ];then
507 echo $load $tape # For amtape output
510 echo `_ 'Loading slot %s into Tape drive' "$load"` >> $logfile
511 $MCUTIL -m slot:$load drive
514 if [ $code -eq 0 ];then
515 echo $load > $currentslot
516 answer=`_ 'Cartridge %s successfully loaded in Tape drive' "$load"`
518 answer=`_ 'Cartridge %s failed to load in Tape drive' "$load"`
520 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
526 echo `_ 'tape drive info was called'` >> $logfile
530 if [ $used -lt 0 ];then
535 answer="$used $lastslot 1"
536 echo `_ 'Exit(%s): %s' "$code" "$answer"` >> $logfile
563 echo `_ '-slot {current|next|previous|first|last|%s-%s|clean}' "$firstslot" "$lastslot"`
564 echo `_ ' current - show contents of current slot'`
565 echo `_ ' next - load tape from next slot'`
566 echo `_ ' previous - load tape from previous slot'`
567 echo `_ ' first - load tape from first slot'`
568 echo `_ ' last - load tape from last slot'`
569 echo `_ ' %s - %s - load tape from slot <slot #>' "$firstslot" "$lastslot"`
570 echo `_ ' clean - Clean the drive'`
571 echo `_ '-device : Show current tape device'`
572 echo `_ '-reset : Reset changer to known state'`
573 echo `_ '-eject : Eject current tape from drive'`
574 echo `_ '-info : Output {current slot | # of slots | can changer go backwards}'`
575 echo `_ '-help : Display this help'`
578 echo `_ "<usage> %s -{slot|device|reset|eject|help}" "$myname"`