]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/mtx-changer.in
Backport from BEE
[bacula/bacula] / bacula / scripts / mtx-changer.in
1 #!/bin/sh
2 #
3 # Bacula interface to mtx autoloader
4 #
5 #  Written by Kern Sibbald
6 #
7 #  Bacula® - The Network Backup Solution
8 #
9 #  Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
10 #
11 #  The main author of Bacula is Kern Sibbald, with contributions from many
12 #  others, a complete list can be found in the file AUTHORS.
13 #
14 #  You may use this file and others of this release according to the
15 #  license defined in the LICENSE file, which includes the Affero General
16 #  Public License, v3.0 ("AGPLv3") and some additional permissions and
17 #  terms pursuant to its AGPLv3 Section 7.
18 #
19 #  Bacula® is a registered trademark of Kern Sibbald.
20 #
21 #
22 #  If you set in your Device resource
23 #
24 #  Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d"
25 #    you will have the following input to this script:
26 #
27 #  So Bacula will always call with all the following arguments, even though
28 #    in come cases, not all are used.
29 #
30 #  mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
31 #                  $1              $2       $3        $4               $5
32 #
33 #  for example:
34 #
35 #  mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
36 #
37 #  will request to load the first cartidge into drive 0, where
38 #   the SCSI control channel is /dev/sg0, and the read/write device
39 #   is /dev/nst0.
40 #
41 #  The commands are:
42 #      Command            Function
43 #      unload             unload a given slot
44 #      load               load a given slot
45 #      loaded             which slot is loaded?
46 #      list               list Volume names (requires barcode reader)
47 #      slots              how many slots total?
48 #      listall            list all info
49 #      transfer
50 #
51 #  Slots are numbered from 1 ...
52 #  Drives are numbered from 0 ...
53 #
54 #
55 #  If you need to an offline, refer to the drive as $4
56 #    e.g.   mt -f $4 offline
57 #
58 #  Many changers need an offline after the unload. Also many
59 #   changers need a sleep 60 after the mtx load.
60 #
61 #  N.B. If you change the script, take care to return either
62 #   the mtx exit code or a 0. If the script exits with a non-zero
63 #   exit code, Bacula will assume the request failed.
64 #
65
66 # source our conf file
67 if test ! -f @scriptdir@/mtx-changer.conf ; then
68   echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
69   echo "ERROR: @scriptdir@/mtx-changer.conf file not found!!!!"
70   echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
71   exit 1
72 fi
73 . @scriptdir@/mtx-changer.conf
74
75 MTX=@MTX@
76
77 if test ${debug_log} -ne 0 ; then
78   touch @working_dir@/mtx.log
79 fi
80 dbgfile="@working_dir@/mtx.log"
81 debug() {
82     if test -f $dbgfile; then
83         echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
84     fi
85 }
86
87
88 #
89 # Create a temporary file
90 #
91 make_temp_file() {
92   TMPFILE=`mktemp @working_dir@/mtx.XXXXXXXXXX`
93   if test x${TMPFILE} = x; then
94      TMPFILE="@working_dir@/mtx.$$"
95      if test -f ${TMPFILE}; then
96         echo "ERROR: Temp file security problem on: ${TMPFILE}"
97         exit 1
98      fi
99   fi
100 }
101
102 #
103 #  Create a temporary file for stderr
104 #
105 #  Note, this file is used because sometime mtx emits
106 #  unexpected error messages followed by the output
107 #  expected during success.
108 #  So we separate STDOUT and STDERR in
109 #  certain of the mtx commands. The contents of STDERR
110 #  is then printed after the STDOUT produced by mtx
111 #  thus we sometimes get better changer results.
112 #
113 make_err_file() {
114   ERRFILE=`mktemp @working_dir@/mtx.err.XXXXXXXXXX`
115   if test x${ERRFILE} = x; then
116      ERRFILE="@working_dir@/mtx.err.$$"
117      if test -f ${ERRFILE}; then
118         echo "ERROR: Temp file security problem on: ${ERRFILE}"
119         exit 1
120      fi
121   fi
122 }
123
124
125 #
126 # The purpose of this function to wait a maximum
127 #   time for the drive. It will
128 #   return as soon as the drive is ready, or after
129 #   waiting a maximum of 300 seconds.
130 # Note, this is very system dependent, so if you are
131 #   not running on Linux, you will probably need to
132 #   re-write it, or at least change the grep target.
133 #   We've attempted to get the appropriate OS grep targets
134 #   in the code at the top of this script.
135 #
136 wait_for_drive() {
137   i=0
138   while [ $i -le 300 ]; do  # Wait max 300 seconds
139     if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
140       break
141     fi
142     debug "Device $1 - not ready, retrying..."
143     sleep 1
144     i=`expr $i + 1`
145   done
146 }
147
148 # check parameter count on commandline
149 #
150 check_parm_count() {
151     pCount=$1
152     pCountNeed=$2
153     if test $pCount -lt $pCountNeed; then
154         echo "ERROR: usage: mtx-changer ctl-device command [slot archive-device drive-index]"
155         echo "  Insufficient number of arguments given."
156         if test $pCount -lt 2; then
157             echo "  Mimimum usage is first two arguments ..."
158         else
159             echo "  Command expected $pCountNeed arguments"
160         fi
161         exit 1
162     fi
163 }
164
165 # Check for special cases where only 2 arguments are needed,
166 #  all others are a minimum of 5
167 #
168 case $2 in
169     list|listall)
170         check_parm_count $# 2
171         ;;
172     slots)
173         check_parm_count $# 2
174         ;;
175     transfer)
176         check_parm_count $# 4
177         ;;
178     *)
179         check_parm_count $# 5
180         ;;
181 esac
182
183
184 # Setup arguments
185 ctl=$1
186 cmd="$2"
187 slot=$3
188 device=$4
189 drive=$5
190
191 debug "Parms: $ctl $cmd $slot $device $drive"
192
193 case $cmd in
194    unload)
195       debug "Doing mtx -f $ctl unload $slot $drive"
196
197       if test ${offline} -eq 1 ; then
198         mt -f $device offline
199       fi
200       if test ${offline_sleep} -ne 0 ; then
201         sleep ${offline_sleep}
202       fi
203       make_err_file
204       ${MTX} -f $ctl unload $slot $drive 2>${ERRFILE}
205       rtn=$?
206       cat ${ERRFILE}
207       rm -f ${ERRFILE} >/dev/null 2>&1
208       exit $rtn
209       ;;
210
211    load)
212       debug "Doing mtx -f $ctl load $slot $drive"
213       make_err_file
214       ${MTX} -f $ctl load $slot $drive 2>${ERRFILE}
215       rtn=$?
216       if test ${load_sleep} -ne 0 ; then
217         sleep ${load_sleep}
218       fi
219       wait_for_drive $device
220       cat ${ERRFILE}
221       rm -f ${ERRFILE} >/dev/null 2>&1
222       exit $rtn
223       ;;
224
225    list)
226       debug "Doing mtx -f $ctl -- to list volumes"
227       make_temp_file
228       if test ${inventory} -ne 0 ; then
229         ${MTX} -f $ctl inventory
230       fi
231       ${MTX} -f $ctl status >${TMPFILE}
232       rtn=$?
233       if test ${vxa_packetloader} -ne 0 ; then
234         cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
235       else
236         cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
237       fi
238       cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
239       rm -f ${TMPFILE} >/dev/null 2>&1
240       exit $rtn
241       ;;
242
243    listall)
244 #  Drive content:         D:Drive num:F:Slot loaded:Volume Name
245 #  D:0:F:2:vol2        or D:Drive num:E
246 #  D:1:F:42:vol42
247 #  D:3:E
248 #
249 #  Slot content:
250 #  S:1:F:vol1             S:Slot num:F:Volume Name
251 #  S:2:E               or S:Slot num:E
252 #  S:3:F:vol4
253 #
254 #  Import/Export tray slots:
255 #  I:10:F:vol10           I:Slot num:F:Volume Name
256 #  I:11:E              or I:Slot num:E
257 #  I:12:F:vol40
258
259       debug "Doing mtx -f $ctl -- to list all"
260       make_temp_file
261       if test ${inventory} -ne 0 ; then
262         ${MTX} -f $ctl inventory
263       fi
264       ${MTX} -f $ctl status >${TMPFILE}
265       rtn=$?
266       # can be converted to awk+sed+cut, see below
267       perl -ne '
268 /Data Transfer Element (\d+):Empty/ && print "D:$1:E\n";
269 /Data Transfer Element (\d+):Full \(Storage Element (\d+) Loaded\)(:VolumeTag =\s*(.+))?/ && print "D:$1:F:$2:$4\n";
270 /Storage Element (\d+):Empty/ && print "S:$1:E\n";
271 /Storage Element (\d+):Full( :VolumeTag=(.+))?/ && print "S:$1:F:$3\n";
272 /Storage Element (\d+) IMPORT.EXPORT:Empty/ && print "I:$1:E\n";
273 /Storage Element (\d+) IMPORT.EXPORT:Full( :VolumeTag=(.+))?/ && print "I:$1:F:$3\n";' ${TMPFILE}
274       # If perl isn't installed, you can use by those commands
275 #cat ${TMPFILE} | grep "Data Transfer Element" | awk "{print \"D:\"\$4 \$7 \$9 \$10}" | sed "s/=/:/" | sed "s/Full/F:/" | sed "s/Empty/E/"
276 #cat ${TMPFILE} | grep -v "Data Transfer Element" | grep "Storage Element" | grep -v "IMPORT/EXPORT" | awk "{print \"S:\"\$3 \$4 \$5}" | sed "s/IMPORT\/EXPORT//" | sed "s/Full *:VolumeTag=/F:/" | sed "s/Empty/E/"
277 #cat ${TMPFILE} | grep -v "Data Transfer Element" | grep "Storage Element" | grep "IMPORT/EXPORT" | awk "{print \"I:\"\$3 \$4 \$5}" | sed "s/IMPORT\/EXPORT//" | sed "s/Full *:VolumeTag=/F:/" | sed "s/Empty/E/"
278
279       rm -f ${TMPFILE} >/dev/null 2>&1
280       exit $rtn
281       ;;
282
283    transfer)
284       slotdest=$device
285       debug "Doing transfer from $slot to $slotdest"
286       ${MTX} -f $ctl transfer $slot $slotdest
287       rtn=$?
288       exit $rtn
289       ;;
290
291    loaded)
292       debug "Doing mtx -f $ctl $drive -- to find what is loaded"
293       make_temp_file
294       ${MTX} -f $ctl status >${TMPFILE}
295       rtn=$?
296       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
297       cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
298       rm -f ${TMPFILE} >/dev/null 2>&1
299       exit $rtn
300       ;;
301
302    slots)
303       debug "Doing mtx -f $ctl -- to get count of slots"
304       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
305       ;;
306 esac