]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/chio-bacula
First cut restore GUI
[bacula/bacula] / bacula / examples / autochangers / chio-bacula
1 #!/bin/sh
2 #
3 # Bacula interface to mtx autoloader
4 # (By Lars Koeller, lars+bacula@koellers.net)
5 #
6 #  If you set in your Device resource
7 #
8 #  Changer Command = "path-to-this-script/chio-bacula" %c %o %S %a
9 #    you will have the following input to this script:
10 #
11 #  chio-bacula "changer-device" "command" "slot" "archive-device"
12 #
13 #  for example:
14 #
15 #  chio-bacula /dev/sg0 load 1 /dev/nst0 (on a FreeBSD system)
16 #
17 #  If you need to to an offline, refer to the drive as $4
18 #    e.g.   mt -f $f offline
19 #
20 #  Many changers need an offline after the unload. Also many
21 #   changers need a sleep 60 after the mtx load.
22 #
23 #  N.B. If you change the script, take care to return either
24 #   the mtx exit code or a 0. If the script exits with a non-zero
25 #   exit code, Bacula will assume the request failed.
26 #
27
28 # This simulates a barcode reader in the changer.
29 # The labes of the virtual barcode reader are located in the BARCODE_FILE
30 SIMULATE_BARCODE=true
31 BARCODE_FILE=/usr/local/etc/bacula-barcodes
32
33 me=$(basename $0)
34
35 # Debug
36 echo "$me $@" > /dev/console
37
38 if [ -z "$1" ] ; then
39     usage;
40 fi
41
42 if [ -z "$2" ] ; then
43     usage;
44 fi
45
46 MTX=/bin/chio
47 CHANGER=$1
48 COMMAND=$2
49 if [ ! -z "$3" ]; then
50     SLOT=$3
51 fi
52 if [ ! -z "$4" ]; then
53     TAPE=$4
54 else
55     TAPE=/dev/nrsa2
56 fi
57
58 # Time to wait for loading
59 SLEEP=20
60 # What drive of the autochanger should be used primary
61 # At the moment bacula (1.31a) could not deal with more drives
62 DRIVE=1
63
64 usage()
65 {
66   echo ""
67   echo "The $me script for bacula"
68   echo "--------------------------------------"
69   echo ""
70   echo "usage: $me <changer-device> <command> [slot] [devicename of tapedrive]"
71   echo ""
72   echo "Valid commands:"
73   echo ""
74   echo "unload          Unloads a tape into the slot"
75   echo "                from where it was loaded."
76   echo "load <slot>     Loads a tape from the slot <slot>"
77   echo "                (slot-base is calculated to 1 as first slot)"
78   echo "list            Lists full storage slots"
79   echo "loaded          Gives slot from where the tape was loaded."
80   echo "                0 means the tape drive is empty."
81   echo "slots           Gives Number of aviable slots."
82   echo ""
83   echo "Example:"
84   echo "  mtx-changer /dev/changer load 1   loads a tape from slot 1"
85   echo ""
86   exit 2
87 }
88
89
90 case ${COMMAND} in
91     unload)
92         # enable the following line if you need to eject the cartridge
93         #mt -f ${TAPE} off
94         #sleep 2
95         ${MTX} -f ${CHANGER} return drive ${DRIVE}
96         ;;
97
98     load)
99         ${MTX} -f ${CHANGER} move slot $((${SLOT}-1)) drive ${DRIVE}
100         rtn=$?
101         # Increase the sleep time if you have a slow device
102         sleep $SLEEP
103         exit $rtn
104         ;;
105
106     list)
107         if [ "${SIMULATE_BARCODE}" = "true" ]; then
108             if [ -f "$BARCODE_FILE" ]; then
109                 cat $BARCODE_FILE | grep -v "^#"
110                 exit 0
111             else
112                 echo "Barcode file $BARCODE_FILE missing ... exiting!"
113                 exit 1
114             fi
115         else
116             ${MTX} -f ${CHANGER} status | grep "^slot .*: .*FULL>" | awk '{print $2}' | awk -F: '{print $1+1" "}' | tr -d "[\r\n]"
117         fi
118       ;;
119
120     loaded)
121         # echo "Request loaded"
122         ${MTX} -f ${CHANGER} status -S > /tmp/mtx.$$
123         rtn=$?
124         cat /tmp/mtx.$$ | grep "^drive ${DRIVE}: <FULL>" | awk '{print $6+1}' | tr -d ">"
125         cat /tmp/mtx.$$ | grep "^drive ${DRIVE}:  source: <>" | awk "{print 0}"
126         rm -f /tmp/mtx.$$
127         exit $rtn
128         ;;
129
130     slots)
131         # echo "Request slots"
132         ${MTX} -f ${CHANGER} status | grep "^slot " | tail -1 | awk '{print $2+1}' | tr -d ":"
133       ;;
134
135     *)
136         usage
137       ;;
138 esac