]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/solaris-mtx-changer
Fix #1648 about make_catalog_backup.pl with multiple catalog
[bacula/bacula] / bacula / examples / autochangers / solaris-mtx-changer
1 #!/bin/bash
2 #
3 # /bin/sh isn't always compatible so use /bin/bash
4 #
5 # Bacula interface to mtx autoloader
6 #
7 #  $Id$
8 #
9 #  If you set in your Device resource
10 #
11 #  Changer Command = "path-to-this-script/mtx-changer  %c %o %S %a %d"
12 #    you will have the following input to this script:
13 #
14 #  mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
15 #                  $1              $2       $3        $4               $5
16 #
17 #  for example:
18 #
19 #  mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
20 #
21 #  If you need to an offline, refer to the drive as $4
22 #    e.g.   mt -f $4 offline
23 #
24 #  Many changers need an offline after the unload. Also many
25 #   changers need a sleep 60 after the mtx load.
26 #
27 #  N.B. If you change the script, take care to return either 
28 #   the mtx exit code or a 0. If the script exits with a non-zero
29 #   exit code, Bacula will assume the request failed.
30 #
31
32 # Sun sed/awk etc are not sufficient, working versions are in /usr/xpg4/bin
33 export PATH="/usr/local/bin:/usr/sfw/bin:/usr/xpg4/bin:/usr/bin"
34
35 MTX=mtx
36
37
38 #
39 # The purpose of this function to wait a maximum 
40 #   time for the drive. It will
41 #   return as soon as the drive is ready, or after
42 #   waiting a maximum of 180 seconds.
43 # Note, this is very system dependent, so if you are
44 #   not running on Linux, you will probably need to
45 #   re-write it.
46 #
47 # If you have a FreeBSD system, you might want to change
48 #  the $(seq 180) to $(jot 180) -- tip from Brian McDonald
49 #
50 wait_for_drive() {
51   for i in $(seq 180); do   # Wait max 180 seconds
52     if ( mt -f $1 status | grep 0x0 ) >/dev/null 2>&1; then
53       #echo "Device $1 READY"
54       break
55     fi
56     #echo "Device $1 - not ready, retry ${i}..."
57     sleep 1
58   done
59 }
60
61
62 if test $# -lt 2 ; then
63   echo "usage: mtx-changer ctl-device command slot archive-device drive"
64   echo "  Insufficient number of arguments arguments given."
65   echo "  Mimimum usage is first two arguments ..."
66   exit 1
67 fi
68
69 # Setup arguments
70 ctl=$1
71 cmd="$2"
72 slot=$3
73 device=$4
74 # If drive not given, default to 0
75 if test $# = 5 ; then
76   drive=$5
77 else
78   drive=0
79 fi
80
81 #
82 # Check for special cases where only 2 arguments are needed, 
83 #  all others are a minimum of 3
84 case $cmd in
85    loaded)
86      ;;
87    unload)
88      ;;
89    list)
90      ;;
91    slots)
92      ;;
93    *)
94      if test $# -lt 3; then
95         echo "usage: mtx-changer ctl-device command slot archive-device drive"
96         echo "  Insufficient number of arguments arguments given."
97         echo "  Mimimum usage is first three arguments ..."
98         exit 1
99      fi
100      ;;
101 esac
102
103
104 case $cmd in 
105    unload)
106 #     echo "Doing mtx -f $ctl unload $slot $drive"
107 #
108 # enable the following line if you need to eject the cartridge
109       #mt -f $device offline
110       mt -f $device rewoffl
111       if test x$slot = x; then
112          ${MTX} -f $ctl unload
113       else
114          ${MTX} -f $ctl unload $slot $drive
115       fi
116       ;;
117
118    load)
119 #     echo "Doing mtx -f $ctl load $slot $drive"
120       ${MTX} -f $ctl load $slot $drive
121       rtn=$?
122 #
123 # Increase the sleep time if you have a slow device
124 # or remove the sleep and add the following:
125       #sleep 15
126       wait_for_drive $device
127       exit $rtn
128       ;;
129
130    list) 
131 #     echo "Requested list"
132       # Some tape changers lose track of their inventory (well, mine does) so 
133       # do one before trying to get a status out of it.
134       ${MTX} -f $ctl inventory
135       ${MTX} -f $ctl status | grep " *Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
136 # Comment out the previous line and add a line here
137 # to print "fake" barcodes.
138 #
139 # If you have a VXA PacketLoader and the above does not work, try
140 #  turning it off and enabling the following line.
141 #     ${MTX} -f $ctl status | grep " *Storage Element [0-9]*:.*Full" | sed "s/*Storage Element //" | sed "s/Full :VolumeTag=//"
142       ;;
143
144    loaded)
145       ${MTX} -f $ctl status >/tmp/mtx.$$
146       rtn=$?
147       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
148       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
149       rm -f /tmp/mtx.$$
150       exit $rtn
151       ;;
152
153    slots)
154 #     echo "Request slots"
155       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
156       ;;
157 esac