]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/mtx-changer.in
Final changes
[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 #  mtx-changer "changer-device" "command" "slot" "archive-device" "drive-index"
13 #                  $1              $2       $3        $4               $5
14 #
15 #  for example:
16 #
17 #  mtx-changer /dev/sg0 load 1 /dev/nst0 0 (on a Linux system)
18 #
19 #  If you need to an offline, refer to the drive as $4
20 #    e.g.   mt -f $4 offline
21 #
22 #  Many changers need an offline after the unload. Also many
23 #   changers need a sleep 60 after the mtx load.
24 #
25 #  N.B. If you change the script, take care to return either 
26 #   the mtx exit code or a 0. If the script exits with a non-zero
27 #   exit code, Bacula will assume the request failed.
28 #
29
30 MTX=@MTX@
31
32 wait_for_drive() {
33   for i in $(seq 180); do   # Wait max 180 seconds
34     if mt -f $1 status >/dev/null 2>&1; then
35       break
36     fi
37 #   echo "Device $1 - not ready, retrying..."
38     sleep 1
39   done
40 }
41
42
43 if test $# -lt 2 ; then
44   echo "usage: mtx-changer ctl-device command slot archive-device drive"
45   echo "  Insufficient number of arguments arguments given."
46   echo "  Mimimum usage is first two arguments ..."
47   exit 1
48 fi
49
50 # Setup arguments
51 ctl=$1
52 cmd="$2"
53 slot=$3
54 device=$4
55 # If drive not given, default to 0
56 if test $# = 5 ; then
57   drive=$5
58 else
59   drive=0
60 fi
61
62 #
63 # Check for special cases where only 2 arguments are needed, 
64 #  all others are a minimum of 3
65 case $cmd in
66    loaded)
67      ;;
68    unload)
69      ;;
70    list)
71      ;;
72    slots)
73      ;;
74    *)
75      if test $# -lt 3; then
76         echo "usage: mtx-changer ctl-device command slot archive-device drive"
77         echo "  Insufficient number of arguments arguments given."
78         echo "  Mimimum usage is first three arguments ..."
79         exit 1
80      fi
81      ;;
82 esac
83
84
85 case $cmd in 
86    unload)
87 #     echo "Doing mtx -f $ctl unload $slot $drive"
88 #
89 # enable the following line if you need to eject the cartridge
90 #     mt -f $device offline
91       if test x$slot = x; then
92          ${MTX} -f $ctl unload
93       else
94          ${MTX} -f $ctl unload $slot $drive
95       fi
96       ;;
97
98    load)
99 #     echo "Doing mtx -f $ctl load $slot $drive"
100       ${MTX} -f $ctl load $slot $drive
101       rtn=$?
102 #
103 # Increase the sleep time if you have a slow device
104 # or remove the sleep and add the following:
105 #     wait_for_drive $device
106       sleep 15
107       exit $rtn
108       ;;
109
110    list) 
111 #     echo "Requested list"
112       ${MTX} -f $ctl status | grep " *Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
113 # Comment out the previous line and add a line here
114 # to print "fake" barcodes.
115       ;;
116
117    loaded)
118       ${MTX} -f $ctl status >/tmp/mtx.$$
119       rtn=$?
120       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Full" | awk "{print \$7}"
121       cat /tmp/mtx.$$ | grep "^Data Transfer Element $drive:Empty" | awk "{print 0}"
122       rm -f /tmp/mtx.$$
123       exit $rtn
124       ;;
125
126    slots)
127 #     echo "Request slots"
128       ${MTX} -f $ctl status | grep " *Storage Changer" | awk "{print \$5}"
129       ;;
130 esac