]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/mtx-changer.in
Use make_catalog_backup.pl by default
[bacula/bacula] / bacula / scripts / mtx-changer.in
1 #!/bin/sh
2 #
3 # Bacula interface to mtx autoloader
4 #
5 #  $Id$
6 #
7 #  If you set in your Device resource
8 #
9 #  Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d"
10 #    you will have the following input to this script:
11 #
12 #  So Bacula will always call with all the following arguments, even though
13 #    in come cases, not all are used.
14 #
15 #  mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
16 #                  $1              $2       $3        $4               $5
17 #
18 #  for example:
19 #
20 #  mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
21
22 #  will request to load the first cartidge into drive 0, where
23 #   the SCSI control channel is /dev/sg0, and the read/write device
24 #   is /dev/nst0.
25 #
26 #  The commands are:
27 #      Command            Function
28 #      unload             unload a given slot
29 #      load               load a given slot
30 #      loaded             which slot is loaded?
31 #      list               list Volume names (requires barcode reader)
32 #      slots              how many slots total?
33 #      listall            list all info
34 #      transfer
35 #
36 #  Slots are numbered from 1 ...
37 #  Drives are numbered from 0 ...
38 #
39 #
40 #  If you need to an offline, refer to the drive as $4
41 #    e.g.   mt -f $4 offline
42 #
43 #  Many changers need an offline after the unload. Also many
44 #   changers need a sleep 60 after the mtx load.
45 #
46 #  N.B. If you change the script, take care to return either 
47 #   the mtx exit code or a 0. If the script exits with a non-zero
48 #   exit code, Bacula will assume the request failed.
49 #
50
51 # source our conf file
52 . @scriptdir@/mtx-changer.conf
53
54 MTX=@MTX@
55
56 if test ${debug_log} -ne 0 ; then
57   touch @working_dir@/mtx.log
58 fi
59 dbgfile="@working_dir@/mtx.log"
60 debug() {
61     if test -f $dbgfile; then
62         echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
63     fi
64 }
65
66
67 #
68 # Create a temporary file
69 #
70 make_temp_file() {
71   TMPFILE=`mktemp @working_dir@/mtx.XXXXXXXXXX`
72   if test x${TMPFILE} = x; then
73      TMPFILE="@working_dir@/mtx.$$"
74      if test -f ${TMPFILE}; then
75         echo "Temp file security problem on: ${TMPFILE}"
76         exit 1
77      fi
78   fi
79 }
80
81 #
82 # The purpose of this function to wait a maximum 
83 #   time for the drive. It will
84 #   return as soon as the drive is ready, or after
85 #   waiting a maximum of 300 seconds.
86 # Note, this is very system dependent, so if you are
87 #   not running on Linux, you will probably need to
88 #   re-write it, or at least change the grep target.
89 #   We've attempted to get the appropriate OS grep targets
90 #   in the code at the top of this script.
91 #
92 wait_for_drive() {
93   i=0 
94   while [ $i -le 300 ]; do  # Wait max 300 seconds
95     if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
96       break
97     fi
98     debug "Device $1 - not ready, retrying..."
99     sleep 1
100     i=`expr $i + 1`
101   done
102 }
103
104 # check parameter count on commandline
105 #
106 check_parm_count() {
107     pCount=$1
108     pCountNeed=$2
109     if test $pCount -lt $pCountNeed; then
110         echo "usage: mtx-changer ctl-device command [slot archive-device drive-index]"
111         echo "  Insufficient number of arguments given."
112         if test $pCount -lt 2; then
113             echo "  Mimimum usage is first two arguments ..."
114         else
115             echo "  Command expected $pCountNeed arguments"
116         fi
117         exit 1
118     fi
119 }
120
121 # Check for special cases where only 2 arguments are needed, 
122 #  all others are a minimum of 5
123 #
124 case $2 in
125     list|listall)
126         check_parm_count $# 2
127         ;;
128     slots)
129         check_parm_count $# 2
130         ;;
131     transfer)
132         check_parm_count $# 4
133         ;;
134     *)
135         check_parm_count $# 5
136         ;;
137 esac
138
139
140 # Setup arguments
141 ctl=$1
142 cmd="$2"
143 slot=$3
144 device=$4
145 drive=$5
146
147 debug "Parms: $ctl $cmd $slot $device $drive"
148
149 case $cmd in 
150    unload)
151       debug "Doing mtx -f $ctl unload $slot $drive"
152
153       if test ${offline} -eq 1 ; then
154         mt -f $device offline
155       fi
156       if test ${offline_sleep} -ne 0 ; then
157         sleep ${offline_sleep}
158       fi
159       ${MTX} -f $ctl unload $slot $drive
160       ;;
161
162    load)
163       debug "Doing mtx -f $ctl load $slot $drive"
164       ${MTX} -f $ctl load $slot $drive
165       rtn=$?
166       if test ${load_sleep} -ne 0 ; then
167         sleep ${load_sleep}
168       fi
169       wait_for_drive $device
170       exit $rtn
171       ;;
172
173    list) 
174       debug "Doing mtx -f $ctl -- to list volumes"
175       make_temp_file
176       if test ${inventory} -ne 0 ; then
177         ${MTX} -f $ctl inventory
178       fi
179       ${MTX} -f $ctl status >${TMPFILE}
180       rtn=$?
181       if test ${vxa_packetloader} -ne 0 ; then
182         cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
183       else
184         cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
185       fi
186       cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
187       rm -f ${TMPFILE} >/dev/null 2>&1
188       exit $rtn
189       ;;
190
191    listall)
192 #  Drive content:         D:Drive num:F:Slot loaded:Volume Name
193 #  D:0:F:2:vol2        or D:Drive num:E
194 #  D:1:F:42:vol42   
195 #  D:3:E
196
197 #  Slot content:
198 #  S:1:F:vol1             S:Slot num:F:Volume Name
199 #  S:2:E               or S:Slot num:E
200 #  S:3:F:vol4
201
202 #  Import/Export tray slots:
203 #  I:10:F:vol10           I:Slot num:F:Volume Name
204 #  I:11:E              or I:Slot num:E
205 #  I:12:F:vol40
206  
207       debug "Doing mtx -f $ctl -- to list all"
208       make_temp_file
209       if test ${inventory} -ne 0 ; then
210         ${MTX} -f $ctl inventory
211       fi
212       ${MTX} -f $ctl status >${TMPFILE}
213       rtn=$?
214       # can be converted to awk+sed+cut, contributions are welcome
215       perl -ne '
216 /Data Transfer Element (\d+):Empty/ && print "D:$1:E\n";
217 /Data Transfer Element (\d+):Full \(Storage Element (\d+) Loaded\)(:VolumeTag =\s*(.+))?/ && print "D:$1:F:$2:$4\n";
218 /Storage Element (\d+):Empty/ && print "S:$1:E\n";
219 /Storage Element (\d+):Full( :VolumeTag=(.+))?/ && print "S:$1:F:$3\n";
220 /Storage Element (\d+) IMPORT.EXPORT:Empty/ && print "I:$1:E\n";
221 /Storage Element (\d+) IMPORT.EXPORT:Full( :VolumeTag=(.+))?/ && print "I:$1:F:$3\n";' ${TMPFILE}
222       rm -f ${TMPFILE} >/dev/null 2>&1
223       exit $rtn
224       ;;
225
226    transfer)
227       slotdest=$device
228       debug "Doing transfer from $slot to $slotdest"
229       ${MTX} -f $ctl transfer $slot $slotdest
230       rtn=$?
231       exit $rtn
232       ;;
233
234    loaded)
235       debug "Doing mtx -f $ctl $drive -- to find what is loaded"
236       make_temp_file
237       ${MTX} -f $ctl status >${TMPFILE}
238       rtn=$?
239       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
240       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
241       rm -f ${TMPFILE} >/dev/null 2>&1
242       exit $rtn
243       ;;
244
245    slots)
246       debug "Doing mtx -f $ctl -- to get count of slots"
247       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
248       ;;
249 esac