@echo off setlocal EnableDelayedExpansion Rem Description Rem This bat file calls SnowMirror API to start a synchronization and checks whether it's finished or not. If the synchronization is successful the script returns return code 0. In case of an error return code 1 is returned. Rem CHANGE CREDENTIALS AND PATH TO YOUR SNOWMIRROR INSTALLATION set _user=snowmirror_api set _password=password set _snowmirror_path=http://localhost:9090/api/v1 set _polling_interval=20 cd C:/Temp/snowMirror Rem DO NOT CHANGE THE REST OF THE FILE set _sync_id=%1 Rem Verify parameters if not "%_sync_id%"=="" goto paramsOk echo "Missing parameter - synchronization id" exit /b 1 :paramsOk set _tmp=%Temp%\snowMirror\batchScheduler\%_sync_id% echo Temporary folder: %_tmp% md "%_tmp%" Rem Get latest history id call :load_latest_history_id if %errorlevel% NEQ 0 exit /b 1 set /p _begin_history_id=< "%_tmp%\response-latest-history-id.txt" echo Begin latest history id: %_begin_history_id% Rem Start synchronization call :clean_tmp_files curl -u "%_user%:%_password%" -v -D "%_tmp%\response-headers.txt" -X POST -H "Content-Type: application/json" --data "{\"action\": \"SYNCHRONIZE\"}" %_snowmirror_path%/synchronization/%_sync_id%/action call :quit_if_not_200 if %errorlevel% NEQ 0 exit /b 1 Rem Check status until it has finished :loop Rem Wait for awhile before polling ping 127.0.0.1 -n "%_polling_interval%" > nul Rem If the latest history id has not changed it means synchronization hasn't been started yet call :load_latest_history_id if %errorlevel% NEQ 0 exit /b 1 set /p _latest_history_id=< "%_tmp%\response-latest-history-id.txt" if "%_begin_history_id%"=="%_latest_history_id%" goto :loop Rem Check status of the latest synchronization run call :clean_tmp_files curl -u "%_user%:%_password%" -v -D "%_tmp%\response-headers.txt" -o "%_tmp%\response-body.txt" %_snowmirror_path%/synchronization/%_sync_id%/history/%_latest_history_id% call :quit_if_not_200 if %errorlevel% NEQ 0 exit /b 1 jq ".history.result" "%_tmp%\response-body.txt" > "%_tmp%\response-jq.txt" set /p jq_status=< "%_tmp%\response-jq.txt" Rem Synchronization has finished if %jq_status%=="SUCCESSFUL" exit /b 0 if %jq_status%=="FAILED" exit /b 1 goto :loop goto :error_exit Rem Cleans tmp files :clean_tmp_files if exist "%_tmp%\response-headers.txt" del "%_tmp%\response-headers.txt" if exist "%_tmp%\response-body.txt" del "%_tmp%\response-body.txt" if exist "%_tmp%\response-jq.txt" del "%_tmp%\response-jq.txt if exist "%_tmp%\response-latest-history-id.txt" del "%_tmp%\response-latest-history-id.txt" goto :eof Rem Quit script execution if the response is not 200 :quit_if_not_200 set /p line_http_code=< "%_tmp%\response-headers.txt" echo Status code: %line_http_code% for /F "tokens=2" %%G in ("%line_http_code%") do set http_code=%%G if "%http_code%" NEQ "200" exit /b 1 goto :eof Rem Loads latest history id into a file :load_latest_history_id setlocal call :clean_tmp_files curl -u "%_user%:%_password%" -v -D "%_tmp%\response-headers.txt" -o "%_tmp%\response-body.txt" %_snowmirror_path%/synchronization/%_sync_id% :quit_if_not_200 if %errorlevel% NEQ 0 exit /b 1 jq ".status.latestHistoryId" "%_tmp%\response-body.txt" > "%_tmp%\response-latest-history-id.txt" goto :eof :error_exit echo Finishing script with error exit /b 1