]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/disk-changer.in
correct spelling mistake: containg => containing
[bacula/bacula] / bacula / scripts / disk-changer.in
1 #!/bin/sh
2 #
3 # Bacula interface to virtual autoloader using disk storage
4 #
5 #  $Id$
6 #
7 #  If you set in your Device resource
8 #
9 #  Changer Command = "path-to-this-script/disk-changer %c %o %S %a %d"
10 #    you will have the following input to this script:
11 #
12 #  So Bacula will always call with all the following arguments, even though
13 #    in come cases, not all are used.
14 #
15 #  disk-changer "changer-device" "command" "slot" "archive-device" "drive-index"
16 #                   $1              $2       $3        $4               $5
17 #
18 # By default the autochanger has 10 Volumes and 1 Drive.
19 #
20 # Note: For this script to work, you *must" specify
21 #    Device Type = File 
22 # in each of the Devices associated with your AutoChanger resource.
23 #
24 # changer-device is the name of a file that overrides the default
25 #   volumes and drives.  It may have:
26 #        maxslot=n   where n is one based (default 10)
27 #        maxdrive=m  where m is zero based (default 1 -- i.e. 2 drives)
28 #  
29 #   This code can also simulate barcodes. You simply put
30 #   a list of the slots and barcodes in the "base" directory/barcodes.
31 #   See below for the base directory definition.  Example of a 
32 #   barcodes file:
33 #      /var/bacula/barcodes
34 #      1:Vol001
35 #      2:Vol002
36 #      ...
37
38 # archive-device is the name of the base directory where you want the
39 #  Volumes stored appended with /drive0 for the first drive; /drive1
40 #  for the second drive, ... For example, you might use
41 #  /var/bacula/drive0  Note: you must not have a trailing slash, and
42 #  the string (e.g. /drive0) must be unique, and it must not match
43 #  any other part of the directory name. These restrictions could be
44 #  easily removed by any clever script jockey.
45 #
46 #  Full example: disk-changer /var/bacula/conf load 1 /var/bacula/drive0 0
47 #
48 # The Volumes will be created with names slot1, slot2, slot3, ... maxslot in the
49 #  base directory. In the above example the base directory is /var/bacula.
50 #  However, as with tapes, their Bacula Volume names will be stored inside the
51 #  Volume label. In addition to the Volumes (e.g. /var/bacula/slot1, 
52 #  /var/bacula/slot3, ...) this script will create a /var/bacula/loadedn
53 #  file to keep track of what Slot is loaded. You should not change this file.
54 #
55 #
56
57 wd=@working_dir@
58
59 #
60 # log whats done
61 #
62 # to turn on logging, uncomment the following line
63 #touch $wd/disk-changer.log
64 #
65 dbgfile="$wd/disk-changer.log"
66 debug() {
67     if test -e $dbgfile; then
68         echo "`date +\"%Y%m%d-%H:%M:%S\"` $*" >> $dbgfile
69     fi
70 }
71
72
73 #
74 # Create a temporary file
75 #
76 make_temp_file() {
77   TMPFILE=`mktemp -t mtx.XXXXXXXXXX`
78   if test x${TMPFILE} = x; then
79      TMPFILE="$wd/disk-changer.$$"
80      if test -f ${TMPFILE}; then
81         echo "Temp file security problem on: ${TMPFILE}"
82         exit 1
83      fi
84   fi
85 }
86
87 # check parameter count on commandline
88 #
89 check_parm_count() {
90     pCount=$1
91     pCountNeed=$2
92     if test $pCount -lt $pCountNeed; then
93         echo "usage: disk-changer ctl-device command [slot archive-device drive-index]"
94         echo "  Insufficient number of arguments arguments given."
95         if test $pCount -lt 2; then
96             echo "  Mimimum usage is first two arguments ..."
97         else
98             echo "  Command expected $pCountNeed arguments"
99         fi
100         exit 1
101     fi
102 }
103
104 #
105 # Strip off the final name in order to get the Directory ($dir)
106 #  that we are dealing with.
107 #
108 get_dir() {
109    bn=`basename $device`
110    dir=`echo "$device" | sed -e s%/$bn%%g -`
111 }
112
113
114 # Setup arguments
115 ctl=$1
116 cmd="$2"
117 slot=$3
118 device=$4
119 drive=$5
120
121 # set defaults
122 maxdrive=1
123 maxslot=10
124
125 # Pull in conf file
126 if [ -f $ctl ]; then 
127    . $ctl
128 fi
129
130
131 # Check for special cases where only 2 arguments are needed, 
132 #  all others are a minimum of 5
133 #
134 case $2 in
135     list)
136         check_parm_count $# 2
137         ;;
138     slots)
139         check_parm_count $# 2
140         ;;
141     *)
142         check_parm_count $# 5
143         if [ $drive -gt $maxdrive ]; then
144            echo "Drive ($drive) out of range (0-$maxdrive)"
145            exit 1
146         fi
147         if [ $slot -gt $maxslot ]; then
148            echo "Slot ($slot) out of range (1-$maxslot)"
149            exit 1
150         fi
151         ;;
152 esac
153
154
155
156 debug "Parms: $ctl $cmd $slot $device $drive"
157
158 case $cmd in 
159    unload)
160       debug "Doing disk -f $ctl unload $slot $device $drive"
161       get_dir
162       echo "0" >$dir/loaded${drive}
163       unlink $device 2>/dev/null >/dev/null
164       rm -f $device
165       ;;
166
167    load)
168       debug "Doing disk $ctl load $slot $device $drive"
169       get_dir
170       echo "0" >$dir/loaded${drive}
171       unlink $device 2>/dev/null >/dev/null
172       rm -f $device
173       ln -s $dir/slot${slot} $device
174       rtn=$?
175       if [ $rtn -eq 0 ]; then
176          echo $slot >$dir/loaded${drive}
177       fi
178       exit $rtn
179       ;;
180
181    list) 
182       debug "Doing disk -f $ctl -- to list volumes"
183       get_dir 
184       if [ -f $dir/barcodes ]; then
185          cat $dir/barcodes
186       else
187          i=1
188          while [ $i -le $maxslot ]; do
189             echo "$i:"
190             i=`expr $i + 1`
191          done
192       fi
193       exit 0
194       ;;
195
196    loaded)
197       debug "Doing disk -f $ctl $drive -- to find what is loaded"
198       get_dir
199       if [ -f $dir/loaded${drive} ]; then
200          cat $dir/loaded${drive}
201       else
202          echo "0"
203       fi
204       exit
205       ;;
206
207    slots)
208       debug "Doing disk -f $ctl -- to get count of slots"
209       echo $maxslot
210       ;;
211 esac