]> git.sur5r.net Git - bacula/bacula/blobdiff - 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
diff --git a/bacula/src/win32/scripts/disk-changer.cmd b/bacula/src/win32/scripts/disk-changer.cmd
new file mode 100644 (file)
index 0000000..4deb4e9
--- /dev/null
@@ -0,0 +1,201 @@
+@ECHO off\r
+setlocal ENABLEDELAYEDEXPANSION\r
+REM\r
+REM\r
+REM Bacula interface to virtual autoloader using disk storage\r
+REM\r
+REM  $Id: disk-changer.in,v 1.3 2006/07/30 16:00:32 kerns Exp $\r
+REM\r
+REM  If you set in your Device resource\r
+REM\r
+REM  Changer Command = "disk-changer %c %o %S %a %d"\r
+REM    you will have the following input to this script:\r
+REM\r
+REM  So Bacula will always call with all the following arguments, even though\r
+REM    in some cases, not all are used.\r
+REM\r
+REM  disk-changer "changer-device" "command" "slot" "archive-device" "drive-index"\r
+REM                      %1           %2       %3          %4             %5\r
+REM\r
+REM By default the autochanger has 10 Volumes and 1 Drive.\r
+REM\r
+REM Note: For this script to work, you *must" specify\r
+REM    Device Type = File \r
+REM in each of the Devices associated with your AutoChanger resource.\r
+REM\r
+REM changer-device is the name of a file that overrides the default\r
+REM   volumes and drives.  It may have:\r
+REM       maxslot=n   where n is one based (default 10)\r
+REM       maxdrive=m  where m is zero based (default 1 -- i.e. 2 drives)\r
+REM  \r
+REM   This code can also simulate barcodes. You simply put\r
+REM   a list of the slots and barcodes in the "base" directory/barcodes.\r
+REM   See below for the base directory definition.  Example of a \r
+REM   barcodes file:\r
+REM      C:\TEMP\bacula\barcodes\r
+REM      1:Vol001\r
+REM      2:Vol002\r
+REM      ...\r
+REM \r
+REM archive-device is the name of the base directory where you want the\r
+REM  Volumes stored appended with \drive0 for the first drive; \drive1\r
+REM  for the second drive, ... For example, you might use\r
+REM  C:\Temp\bacula\drive0  Note: you must not have a trailing slash, and\r
+REM  the string (e.g. \drive0) must be unique, and it must not match\r
+REM  any other part of the directory name. These restrictions could be\r
+REM  easily removed by any clever script jockey.\r
+REM\r
+REM  Full example: disk-changer C:\Temp\bacula\conf load 1 C:\Temp\bacula\drive0 0\r
+REM\r
+REM The Volumes will be created with names slot1, slot2, slot3, ... maxslot in the\r
+REM  base directory. In the above example the base directory is C:\Temp\bacula.\r
+REM  However, as with tapes, their Bacula Volume names will be stored inside the\r
+REM  Volume label. In addition to the Volumes (e.g. C:\Temp\bacula\slot1, \r
+REM  C:\Temp\bacula\slot3, ...) this script will create a C:\Temp\bacula\loadedn\r
+REM  file to keep track of what Slot is loaded. You should not change this file.\r
+REM\r
+\r
+SET dbgfile=%CD%\disk-changer.log\r
+\r
+REM to turn on logging, uncomment the following line\r
+IF NOT EXIST %dbgfile% COPY nul %dbgfile% >nul\r
+\r
+REM\r
+REM check parameter count on commandline\r
+REM\r
+REM Check for special cases where only 2 arguments are needed, \r
+REM  all others are a minimum of 5\r
+REM\r
+IF "%1" EQU "" goto :param_count_invalid\r
+IF "%2" EQU "" goto :param_count_invalid\r
+IF "%2" EQU "list" goto :param_count_valid\r
+IF "%2" EQU "slots" goto :param_count_valid\r
+IF "%3" EQU "" goto :param_count_invalid\r
+IF "%4" EQU "" goto :param_count_invalid\r
+IF "%5" EQU "" goto :param_count_invalid\r
+GOTO :param_count_valid\r
+\r
+:param_count_invalid\r
+   echo Insufficient number of arguments given.\r
+   IF "%2" EQU "" (\r
+      echo   At least two arguments must be specified.\r
+   ) else echo   Command expected 5 arguments.\r
+:usage\r
+   ECHO.\r
+   ECHO usage: disk-changer ctl-device command [slot archive-device drive-index]\r
+   ECHO        Valid commands are: unload, load, list, loaded, and slots.\r
+   EXIT /B 1\r
+\r
+:param_count_valid\r
+\r
+REM Setup arguments\r
+SET ctl=%1\r
+SET cmd=%2\r
+SET slot=%3\r
+SET device=%4\r
+SET drive=%5\r
+\r
+REM set defaults\r
+SET maxdrive=1\r
+SET maxslot=10\r
+\r
+SET ctl=%ctl:/=\%\r
+SET ctl=%ctl:\\=\%\r
+\r
+SET device=%device:/=\%\r
+SET device=%device:\\=\%\r
+\r
+REM Pull in conf file\r
+IF EXIST %ctl% CALL %ctl%\r
+\r
+FOR %%i IN ( %ctl% ) DO SET dirname=%%~dpi\r
+IF NOT EXIST %dirname%nul (\r
+   ECHO ERROR: Autochanger directory "%dirname%" does not exist.\r
+   ECHO        You must create it.\r
+   EXIT /b 1\r
+)\r
+\r
+CALL :debug "Parms: %ctl% %cmd% %slot% %device% %drive%"\r
+IF "%cmd%" EQU "unload" GOTO :cmdUnload\r
+IF "%cmd%" EQU "load" GOTO :cmdLoad\r
+IF "%cmd%" EQU "list" GOTO :cmdList\r
+IF "%cmd%" EQU "loaded" GOTO :cmdLoaded\r
+IF "%cmd%" EQU "slots" GOTO :cmdSlots\r
+GOTO :cmdUnknown\r
+\r
+:cmdUnload\r
+   CALL :debug "Doing disk -f %ctl% unload %slot% %device% %drive%"\r
+   IF NOT EXIST %dirname%loaded%drive% ECHO 0 >%dirname%loaded%drive%\r
+   FOR /f %%i IN ( %dirname%loaded%drive% ) DO SET ld=%%i\r
+\r
+   IF "%slot%" EQU "%ld%" ( \r
+      CALL :debug "Unloaded slot %ld% from drive %drive%"\r
+      ECHO 0 >%dirname%loaded%drive%\r
+      DEL %dirname%%device% >nul 2>nul\r
+      SET rtn=0\r
+   ) ELSE (\r
+      ECHO Storage Element %slot% is Already Full\r
+      CALL :debug "Storage Element %slot% is Already Full"\r
+      SET rtn=1\r
+   )\r
+   GOTO :cmdExit\r
+\r
+:cmdLoad\r
+   CALL :debug "Doing disk -f %ctl% load %slot% %device% %drive%"\r
+   SET ld=0\r
+   IF NOT EXIST %dirname%loaded%drive% ECHO 0 >%dirname%loaded%drive%\r
+   FOR /f %%i IN ( %dirname%loaded%drive% ) DO SET ld=%%i\r
+   IF %ld% EQU 0 (\r
+      IF NOT EXIST %dirname%slot%slot% COPY nul %dirname%slot%slot% >nul\r
+      DEL %device%\r
+      fsutil hardlink create %device% %dirname%slot%slot% >nul 2>&1\r
+      SET rtn=%ERRORLEVEL%\r
+      IF !rtn! EQU 0 (\r
+         ECHO %slot% >%dirname%loaded%drive%\r
+         CALL :debug "Loaded slot %slot% into drive %drive%"\r
+      ) ELSE (\r
+         CALL :debug "Create hardlink failed, return = !rtn!"\r
+      )\r
+   ) ELSE (\r
+      ECHO Drive %drive% Full - Storage element %ld% loaded\r
+      CALL :debug "Drive %drive% Full - Storage element %ld% loaded"\r
+   )\r
+   GOTO :cmdExit\r
+\r
+:cmdList\r
+   CALL :debug "Doing disk -f %ctl% -- to list volumes"\r
+   IF EXIST %dirname%barcodes (\r
+      TYPE %dirname%barcodes \r
+   ) ELSE (\r
+      FOR /l %%i IN ( 1, 1, %maxslot% ) DO ECHO %%i:\r
+   )\r
+   SET rtn=0\r
+   GOTO :cmdExit\r
+\r
+:cmdLoaded\r
+   CALL :debug "Doing disk -f %ctl% %drive% -- to find what is loaded"\r
+   IF EXIST %dirname%loaded%drive% ( TYPE %dirname%loaded%drive% ) ELSE ECHO 0\r
+   SET rtn=0\r
+   GOTO :cmdExit\r
+\r
+:cmdSlots\r
+   CALL :debug "Doing disk -f %ctl% -- to get count of slots"\r
+   ECHO %maxslot%\r
+   SET rtn=0\r
+   GOTO :cmdExit\r
+\r
+:cmdExit\r
+   EXIT /b %rtn%\r
+\r
+:cmdUnknown\r
+   ECHO '%cmd%' is an invalid command.\r
+   GOTO :usage\r
+\r
+REM\r
+REM log whats done\r
+REM\r
+:debug\r
+   IF NOT EXIST %dbgfile% GOTO :EOF\r
+   FOR /f "usebackq tokens=2-4,5-7 delims=/:. " %%i IN ( '%DATE% %TIME%' ) do SET TIMESTAMP=%%k%%i%%j-%%l:%%m:%%n\r
+   ECHO %TIMESTAMP% %* >> %dbgfile%\r
+   GOTO :EOF\r