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