]> git.sur5r.net Git - bacula/bacula/blob - regress/win32/scripts/disk-changer.cmd
Delete unwanted subdirectory
[bacula/bacula] / regress / win32 / scripts / disk-changer.cmd
1 @ECHO off
2 setlocal ENABLEDELAYEDEXPANSION
3 REM
4 REM
5 REM Bacula interface to virtual autoloader using disk storage
6 REM
7 REM  $Id$
8 REM
9 REM  If you set in your Device resource
10 REM
11 REM  Changer Command = "disk-changer %c %o %S %a %d"
12 REM    you will have the following input to this script:
13 REM
14 REM  So Bacula will always call with all the following arguments, even though
15 REM    in some cases, not all are used.
16 REM
17 REM  disk-changer "changer-device" "command" "slot" "archive-device" "drive-index"
18 REM                      %1           %2       %3          %4             %5
19 REM
20 REM By default the autochanger has 10 Volumes and 1 Drive.
21 REM
22 REM Note: For this script to work, you *must" specify
23 REM    Device Type = File 
24 REM in each of the Devices associated with your AutoChanger resource.
25 REM
26 REM changer-device is the name of a file that overrides the default
27 REM   volumes and drives.  It may have:
28 REM       maxslot=n   where n is one based (default 10)
29 REM       maxdrive=m  where m is zero based (default 1 -- i.e. 2 drives)
30 REM  
31 REM   This code can also simulate barcodes. You simply put
32 REM   a list of the slots and barcodes in the "base" directory/barcodes.
33 REM   See below for the base directory definition.  Example of a 
34 REM   barcodes file:
35 REM      C:\TEMP\bacula\barcodes
36 REM      1:Vol001
37 REM      2:Vol002
38 REM      ...
39 REM 
40 REM archive-device is the name of the base directory where you want the
41 REM  Volumes stored appended with \drive0 for the first drive; \drive1
42 REM  for the second drive, ... For example, you might use
43 REM  C:\Temp\bacula\drive0  Note: you must not have a trailing slash, and
44 REM  the string (e.g. \drive0) must be unique, and it must not match
45 REM  any other part of the directory name. These restrictions could be
46 REM  easily removed by any clever script jockey.
47 REM
48 REM  Full example: disk-changer C:\Temp\bacula\conf load 1 C:\Temp\bacula\drive0 0
49 REM
50 REM The Volumes will be created with names slot1, slot2, slot3, ... maxslot in the
51 REM  base directory. In the above example the base directory is C:\Temp\bacula.
52 REM  However, as with tapes, their Bacula Volume names will be stored inside the
53 REM  Volume label. In addition to the Volumes (e.g. C:\Temp\bacula\slot1, 
54 REM  C:\Temp\bacula\slot3, ...) this script will create a C:\Temp\bacula\loadedn
55 REM  file to keep track of what Slot is loaded. You should not change this file.
56 REM
57
58 SET dbgfile=%CD%\disk-changer.log
59
60 REM to turn on logging, uncomment the following line
61 IF NOT EXIST %dbgfile% COPY nul %dbgfile% >nul
62
63 REM
64 REM check parameter count on commandline
65 REM
66 REM Check for special cases where only 2 arguments are needed, 
67 REM  all others are a minimum of 5
68 REM
69 IF "%1" EQU "" goto :param_count_invalid
70 IF "%2" EQU "" goto :param_count_invalid
71 IF "%2" EQU "list" goto :param_count_valid
72 IF "%2" EQU "slots" goto :param_count_valid
73 IF "%3" EQU "" goto :param_count_invalid
74 IF "%4" EQU "" goto :param_count_invalid
75 IF "%5" EQU "" goto :param_count_invalid
76 GOTO :param_count_valid
77
78 :param_count_invalid
79    echo Insufficient number of arguments given.
80    IF "%2" EQU "" (
81       echo   At least two arguments must be specified.
82    ) else echo   Command expected 5 arguments.
83 :usage
84    ECHO.
85    ECHO usage: disk-changer ctl-device command [slot archive-device drive-index]
86    ECHO        Valid commands are: unload, load, list, loaded, and slots.
87    EXIT /B 1
88
89 :param_count_valid
90
91 REM Setup arguments
92 SET ctl=%1
93 SET cmd=%2
94 SET slot=%3
95 SET device=%4
96 SET drive=%5
97
98 REM set defaults
99 SET maxdrive=1
100 SET maxslot=10
101
102 SET ctl=%ctl:/=\%
103 SET ctl=%ctl:\\=\%
104
105 SET device=%device:/=\%
106 SET device=%device:\\=\%
107
108 REM Pull in conf file
109 IF EXIST %ctl% CALL %ctl%
110
111 FOR %%i IN ( %ctl% ) DO SET dirname=%%~dpi
112 IF NOT EXIST %dirname%nul (
113    ECHO ERROR: Autochanger directory "%dirname%" does not exist.
114    ECHO        You must create it.
115    EXIT /b 1
116 )
117
118 CALL :debug "Parms: %ctl% %cmd% %slot% %device% %drive%"
119 IF "%cmd%" EQU "unload" GOTO :cmdUnload
120 IF "%cmd%" EQU "load" GOTO :cmdLoad
121 IF "%cmd%" EQU "list" GOTO :cmdList
122 IF "%cmd%" EQU "loaded" GOTO :cmdLoaded
123 IF "%cmd%" EQU "slots" GOTO :cmdSlots
124 GOTO :cmdUnknown
125
126 :cmdUnload
127    CALL :debug "Doing disk -f %ctl% unload %slot% %device% %drive%"
128    IF NOT EXIST %dirname%loaded%drive% ECHO 0 >%dirname%loaded%drive%
129    FOR /f %%i IN ( %dirname%loaded%drive% ) DO SET ld=%%i
130
131    IF "%slot%" EQU "%ld%" ( 
132       CALL :debug "Unloaded slot %ld% from drive %drive%"
133       ECHO 0 >%dirname%loaded%drive%
134       DEL %dirname%%device% >nul 2>nul
135       SET rtn=0
136    ) ELSE (
137       ECHO Storage Element %slot% is Already Full
138       CALL :debug "Storage Element %slot% is Already Full"
139       SET rtn=1
140    )
141    GOTO :cmdExit
142
143 :cmdLoad
144    CALL :debug "Doing disk -f %ctl% load %slot% %device% %drive%"
145    SET ld=0
146    IF NOT EXIST %dirname%loaded%drive% ECHO 0 >%dirname%loaded%drive%
147    FOR /f %%i IN ( %dirname%loaded%drive% ) DO SET ld=%%i
148    IF %ld% EQU 0 (
149       IF NOT EXIST %dirname%slot%slot% COPY nul %dirname%slot%slot% >nul
150       DEL %device%
151       fsutil hardlink create %device% %dirname%slot%slot% >nul 2>&1
152       SET rtn=%ERRORLEVEL%
153       IF !rtn! EQU 0 (
154          ECHO %slot% >%dirname%loaded%drive%
155          CALL :debug "Loaded slot %slot% into drive %drive%"
156       ) ELSE (
157          CALL :debug "Create hardlink failed, return = !rtn!"
158       )
159    ) ELSE (
160       ECHO Drive %drive% Full - Storage element %ld% loaded
161       CALL :debug "Drive %drive% Full - Storage element %ld% loaded"
162    )
163    GOTO :cmdExit
164
165 :cmdList
166    CALL :debug "Doing disk -f %ctl% -- to list volumes"
167    IF EXIST %dirname%barcodes (
168       TYPE %dirname%barcodes 
169    ) ELSE (
170       FOR /l %%i IN ( 1, 1, %maxslot% ) DO ECHO %%i:
171    )
172    SET rtn=0
173    GOTO :cmdExit
174
175 :cmdLoaded
176    CALL :debug "Doing disk -f %ctl% %drive% -- to find what is loaded"
177    IF EXIST %dirname%loaded%drive% ( TYPE %dirname%loaded%drive% ) ELSE ECHO 0
178    SET rtn=0
179    GOTO :cmdExit
180
181 :cmdSlots
182    CALL :debug "Doing disk -f %ctl% -- to get count of slots"
183    ECHO %maxslot%
184    SET rtn=0
185    GOTO :cmdExit
186
187 :cmdExit
188    EXIT /b %rtn%
189
190 :cmdUnknown
191    ECHO '%cmd%' is an invalid command.
192    GOTO :usage
193
194 REM
195 REM log whats done
196 REM
197 :debug
198    IF NOT EXIST %dbgfile% GOTO :EOF
199    FOR /f "usebackq tokens=2-4,5-7 delims=/:. " %%i IN ( '%DATE% %TIME%' ) do SET TIMESTAMP=%%k%%i%%j-%%l:%%m:%%n
200    ECHO %TIMESTAMP% %* >> %dbgfile%
201    GOTO :EOF