3 # Bacula interface to mtx autoloader
5 # Written by Kern Sibbald
7 # Bacula® - The Network Backup Solution
9 # Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
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.
14 # You may use this file and others of this release according to the
15 # license defined in the LICENSE file, which includes the GNU Lesser
16 # General Public License, v3.0 ("LGPLv3") and some additional permissions and
17 # terms pursuant to its LGPLv3 Section 7.
19 # Bacula® is a registered trademark of Kern Sibbald.
21 # If you set in your Device resource
23 # Changer Command = "path-to-this-script/mtx-changer %c %o %S %a %d"
24 # you will have the following input to this script:
26 # So Bacula will always call with all the following arguments, even though
27 # in come cases, not all are used.
29 # mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
34 # mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
36 # will request to load the first cartidge into drive 0, where
37 # the SCSI control channel is /dev/sg0, and the read/write device
42 # unload unload a given slot
43 # load load a given slot
44 # loaded which slot is loaded?
45 # list list Volume names (requires barcode reader)
46 # slots how many slots total?
47 # listall list all info
50 # Slots are numbered from 1 ...
51 # Drives are numbered from 0 ...
54 # If you need to an offline, refer to the drive as $4
55 # e.g. mt -f $4 offline
57 # Many changers need an offline after the unload. Also many
58 # changers need a sleep 60 after the mtx load.
60 # N.B. If you change the script, take care to return either
61 # the mtx exit code or a 0. If the script exits with a non-zero
62 # exit code, Bacula will assume the request failed.
65 # source our conf file
66 if test ! -f @scriptdir@/mtx-changer.conf ; then
67 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
68 echo "ERROR: @scriptdir@/mtx-changer.conf file not found!!!!"
69 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
72 . @scriptdir@/mtx-changer.conf
76 if test ${debug_log} -ne 0 ; then
77 touch @working_dir@/mtx.log
79 dbgfile="@working_dir@/mtx.log"
81 if test -f $dbgfile; then
82 echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
88 # Create a temporary file
91 TMPFILE=`mktemp @working_dir@/mtx.XXXXXXXXXX`
92 if test x${TMPFILE} = x; then
93 TMPFILE="@working_dir@/mtx.$$"
94 if test -f ${TMPFILE}; then
95 echo "ERROR: Temp file security problem on: ${TMPFILE}"
102 # Create a temporary file for stderr
104 # Note, this file is used because sometime mtx emits
105 # unexpected error messages followed by the output
106 # expected during success.
107 # So we separate STDOUT and STDERR in
108 # certain of the mtx commands. The contents of STDERR
109 # is then printed after the STDOUT produced by mtx
110 # thus we sometimes get better changer results.
113 ERRFILE=`mktemp @working_dir@/mtx.err.XXXXXXXXXX`
114 if test x${ERRFILE} = x; then
115 ERRFILE="@working_dir@/mtx.err.$$"
116 if test -f ${ERRFILE}; then
117 echo "ERROR: Temp file security problem on: ${ERRFILE}"
125 # The purpose of this function to wait a maximum
126 # time for the drive. It will
127 # return as soon as the drive is ready, or after
128 # waiting a maximum of 300 seconds.
129 # Note, this is very system dependent, so if you are
130 # not running on Linux, you will probably need to
131 # re-write it, or at least change the grep target.
132 # We've attempted to get the appropriate OS grep targets
133 # in the code at the top of this script.
137 while [ $i -le 300 ]; do # Wait max 300 seconds
138 if mt -f $1 status 2>&1 | grep "${ready}" >/dev/null 2>&1; then
141 debug "Device $1 - not ready, retrying..."
147 # check parameter count on commandline
152 if test $pCount -lt $pCountNeed; then
153 echo "ERROR: usage: mtx-changer ctl-device command [slot archive-device drive-index]"
154 echo " Insufficient number of arguments given."
155 if test $pCount -lt 2; then
156 echo " Mimimum usage is first two arguments ..."
158 echo " Command expected $pCountNeed arguments"
164 # Check for special cases where only 2 arguments are needed,
165 # all others are a minimum of 5
169 check_parm_count $# 2
172 check_parm_count $# 2
175 check_parm_count $# 4
178 check_parm_count $# 5
190 debug "Parms: $ctl $cmd $slot $device $drive"
194 debug "Doing mtx -f $ctl unload $slot $drive"
196 if test ${offline} -eq 1 ; then
197 mt -f $device offline
199 if test ${offline_sleep} -ne 0 ; then
200 sleep ${offline_sleep}
203 ${MTX} -f $ctl unload $slot $drive 2>${ERRFILE}
206 rm -f ${ERRFILE} >/dev/null 2>&1
211 debug "Doing mtx -f $ctl load $slot $drive"
213 ${MTX} -f $ctl load $slot $drive 2>${ERRFILE}
215 if test ${load_sleep} -ne 0 ; then
218 wait_for_drive $device
220 rm -f ${ERRFILE} >/dev/null 2>&1
225 debug "Doing mtx -f $ctl -- to list volumes"
227 if test ${inventory} -ne 0 ; then
228 ${MTX} -f $ctl inventory
230 ${MTX} -f $ctl status >${TMPFILE}
232 if test ${vxa_packetloader} -ne 0 ; then
233 cat ${TMPFILE} | grep " *Storage Element [0-9]*:.*Full" | sed "s/ Storage Element //" | sed "s/Full :VolumeTag=//"
235 cat ${TMPFILE} | grep " Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
237 cat ${TMPFILE} | grep "^Data Transfer Element [0-9]*:Full (Storage Element [0-9]" | awk '{printf "%s:%s\n",$7,$10}'
238 rm -f ${TMPFILE} >/dev/null 2>&1
243 # Drive content: D:Drive num:F:Slot loaded:Volume Name
244 # D:0:F:2:vol2 or D:Drive num:E
249 # S:1:F:vol1 S:Slot num:F:Volume Name
250 # S:2:E or S:Slot num:E
253 # Import/Export tray slots:
254 # I:10:F:vol10 I:Slot num:F:Volume Name
255 # I:11:E or I:Slot num:E
258 debug "Doing mtx -f $ctl -- to list all"
260 if test ${inventory} -ne 0 ; then
261 ${MTX} -f $ctl inventory
263 ${MTX} -f $ctl status >${TMPFILE}
265 # can be converted to awk+sed+cut, see below
267 /Data Transfer Element (\d+):Empty/ && print "D:$1:E\n";
268 /Data Transfer Element (\d+):Full \(Storage Element (\d+) Loaded\)(:VolumeTag =\s*(.+))?/ && print "D:$1:F:$2:$4\n";
269 /Storage Element (\d+):Empty/ && print "S:$1:E\n";
270 /Storage Element (\d+):Full( :VolumeTag=(.+))?/ && print "S:$1:F:$3\n";
271 /Storage Element (\d+) IMPORT.EXPORT:Empty/ && print "I:$1:E\n";
272 /Storage Element (\d+) IMPORT.EXPORT:Full( :VolumeTag=(.+))?/ && print "I:$1:F:$3\n";' ${TMPFILE}
273 # If perl isn't installed, you can use by those commands
274 #cat ${TMPFILE} | grep "Data Transfer Element" | awk "{print \"D:\"\$4 \$7 \$9 \$10}" | sed "s/=/:/" | sed "s/Full/F:/" | sed "s/Empty/E/"
275 #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/"
276 #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 rm -f ${TMPFILE} >/dev/null 2>&1
284 debug "Doing transfer from $slot to $slotdest"
285 ${MTX} -f $ctl transfer $slot $slotdest
291 debug "Doing mtx -f $ctl $drive -- to find what is loaded"
293 ${MTX} -f $ctl status >${TMPFILE}
295 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
296 cat ${TMPFILE} | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
297 rm -f ${TMPFILE} >/dev/null 2>&1
302 debug "Doing mtx -f $ctl -- to get count of slots"
303 ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"