Debian Codegraph @

examples/autochangers/chio-bacula

raw symbols SCIP
1 #!/bin/sh
2 #
3 # Bacula interface to mtx autoloader
4 # (By Lars Koeller, [email protected])
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 labels of the virtual barcode reader are located in the BARCODE_FILE
30 SIMULATE_BARCODE=true
31 BARCODE_FILE=/usr/local/etc/bacula-barcodes
32 TMPDIR=/tmp
33
34 make_temp_file()
35 {
36 TMPFILE=`mktemp ${TMPDIR}/mtx$1.XXXXXXXXXX 2> /dev/null`
37 if test $? -ne 0 || test x${TMPFILE} = x; then
38 TMPFILE="${TMPDIR}/mtx$1.$$"
39 if test -f ${TMPFILE}; then
40 echo "ERROR: Temp file security problem on: ${TMPFILE}"
41 exit 1
42 fi
43 fi
44 }
45
46 me=$(basename $0)
47
48 # Debug
49 echo "$me $@" > /dev/console
50
51 if [ -z "$1" ] ; then
52 usage;
53 fi
54
55 if [ -z "$2" ] ; then
56 usage;
57 fi
58
59 MTX=/bin/chio
60 CHANGER=$1
61 COMMAND=$2
62 if [ ! -z "$3" ]; then
63 SLOT=$3
64 fi
65 if [ ! -z "$4" ]; then
66 TAPE=$4
67 else
68 TAPE=/dev/nrsa2
69 fi
70
71 # Time to wait for loading
72 SLEEP=20
73 # What drive of the autochanger should be used primary
74 # At the moment bacula (1.31a) could not deal with more drives
75 DRIVE=1
76
77 usage()
78 {
79 echo ""
80 echo "The $me script for bacula"
81 echo "--------------------------------------"
82 echo ""
83 echo "usage: $me <changer-device> <command> [slot] [devicename of tapedrive]"
84 echo ""
85 echo "Valid commands:"
86 echo ""
87 echo "unload Unloads a tape into the slot"
88 echo " from where it was loaded."
89 echo "load <slot> Loads a tape from the slot <slot>"
90 echo " (slot-base is calculated to 1 as first slot)"
91 echo "list Lists full storage slots"
92 echo "loaded Gives slot from where the tape was loaded."
93 echo " 0 means the tape drive is empty."
94 echo "slots Gives Number of available slots."
95 echo ""
96 echo "Example:"
97 echo " mtx-changer /dev/changer load 1 loads a tape from slot 1"
98 echo ""
99 exit 2
100 }
101
102
103 case ${COMMAND} in
104 unload)
105 # enable the following line if you need to eject the cartridge
106 #mt -f ${TAPE} off
107 #sleep 2
108 ${MTX} -f ${CHANGER} return drive ${DRIVE}
109 ;;
110
111 load)
112 ${MTX} -f ${CHANGER} move slot $((${SLOT}-1)) drive ${DRIVE}
113 rtn=$?
114 # Increase the sleep time if you have a slow device
115 sleep $SLEEP
116 exit $rtn
117 ;;
118
119 list)
120 if [ "${SIMULATE_BARCODE}" = "true" ]; then
121 if [ -f "$BARCODE_FILE" ]; then
122 cat $BARCODE_FILE | grep -v "^#"
123 exit 0
124 else
125 echo "Barcode file $BARCODE_FILE missing ... exiting!"
126 exit 1
127 fi
128 else
129 ${MTX} -f ${CHANGER} status | grep "^slot .*: .*FULL>" | awk '{print $2}' | awk -F: '{print $1+1" "}' | tr -d "[\r\n]"
130 fi
131 ;;
132
133 loaded)
134 # echo "Request loaded"
135 make_temp_file
136 ${MTX} -f ${CHANGER} status -S > ${TMPFILE}
137 rtn=$?
138 cat ${TMPFILE} | grep "^drive ${DRIVE}: <FULL>" | awk '{print $6+1}' | tr -d ">"
139 cat ${TMPFILE} | grep "^drive ${DRIVE}: source: <>" | awk "{print 0}"
140 rm -f ${TMPFILE}
141 exit $rtn
142 ;;
143
144 slots)
145 # echo "Request slots"
146 ${MTX} -f ${CHANGER} status | grep "^slot " | tail -1 | awk '{print $2+1}' | tr -d ":"
147 ;;
148
149 *)
150 usage
151 ;;
152 esac