setlocal enabledelayedexpansion enableextensions
@echo off
set SERVER_NAME=localhost
set IB_NAME=SmallBusiness
set IB_USER=Администратор
set IB_PWD=123
set PERMISSION_CODE=Cron-backup
set BACKUP_DIR=L:\Backup1c\%IB_NAME%
set RESERVE_DIR=x:\backup1c\%IB_NAME%
set LOG_FILE=%BACKUP_DIR%\backup.log
set BIN=C:\Program Files\1cv8\8.3.19.1229\bin
set RAC="%BIN%\rac"
set EXE="%BIN%\1cv8"
set DAY=%DATE:~0,2%
set MONTH=%DATE:~3,2%
set YEAR=%DATE:~6,4%
set HOUR=%TIME:~0,2%
set MINUTE=%TIME:~3,2%
set SECOND=%TIME:~6,2%
set YYYYMMDD=%YEAR%%MONTH%%DAY%
echo. >> %LOG_FILE%
echo Operation started at %DATE% %TIME% >> %LOG_FILE%
echo Opetation started at %DATE% %TIME%
rem %RAC% cluster list %SERVER_NAME%:1545
for /f "usebackq tokens=1,2 delims=: " %%i in (`%RAC% cluster list %SERVER_NAME%:1545`) do (
set %%i=%%j
call :IsEQU %SERVER_NAME% !host! Proceedcl
)
:Proceedcl
echo Found cluster = %cluster%
rem pause
rem %RAC% infobase summary list %SERVER_NAME%:1545 --cluster=%cluster%
for /f "usebackq tokens=1,2 delims=: " %%i in (`%RAC% infobase summary list %SERVER_NAME%:1545 --cluster=%cluster%`) do (
set %%i=%%j
call :IsEQU %IB_NAME% !name! Proceedib
)
:Proceedib
echo Found %name% = %infobase%
rem pause
echo Denying sessions and scheduled jobs at %DATE% %TIME% >> %LOG_FILE%
echo Denying sessions and scheduled jobs at %DATE% %TIME%
set DENIED_MSG="Для администратора: Чтобы принудительно разблокировать информационную базу, воспользуйтесь консолью кластера серверов или запустите 1С:Предприятие с параметрами: ENTERPRISE /S%SERVER_NAME%\%IB_NAME% /CРазрешитьРаботуПользователей /UC%PERMISSION_CODE%"
set DENIED_DAYTIME=%DATE% %TIME%
%RAC% infobase update %SERVER_NAME%:1545 --cluster=%cluster% --infobase=%infobase% --sessions-deny=on --permission-code=%PERMISSION_CODE% --denied-from=%DENIED_DAYTIME% --denied-to= --denied-message=%DENIED_MSG% --scheduled-jobs-deny=on --infobase-user=%IB_USER% --infobase-pwd=%IB_PWD%
echo Denied sessions and scheduled jobs at %DATE% %TIME% >> %LOG_FILE%
echo Denied sessions and scheduled jobs at %DATE% %TIME%
rem echo Disconnecting users started at %DATE% %TIME% >> %LOG_FILE%
rem echo Disconnecting users started at %DATE% %TIME%
rem rem %RAC% connection list %SERVER_NAME%:1545 --infobase=%infobase% --cluster=%cluster%
rem for /f "usebackq tokens=1,2 delims=: " %%i in (`%RAC% connection list %SERVER_NAME%:1545 --infobase=%infobase% --cluster=%cluster%`) do (
rem set %%i=%%j
rem if %%i EQU process (
rem %RAC% connection disconnect %SERVER_NAME%:1545 --cluster=%cluster% --process=!process! --connection=!connection! --IB_USER=%UserName% --IB_PWD=%UserPass%
rem echo User disconnected !connection!
rem echo User disconnected !connection! >> %LOG_FILE%
rem )
rem )
rem echo Users disconnected at %DATE% %TIME% >> %LOG_FILE%
rem echo Users disconnected at %DATE% %TIME%
echo Terminating user sessions started at %DATE% %TIME% >> %LOG_FILE%
echo Terminating user sessions started at %DATE% %TIME%
:Terminate
rem %RAC% session list %SERVER_NAME%:1545 --infobase=%infobase% --cluster=%cluster%
for /f "usebackq tokens=1,2 delims=: " %%i in (`%RAC% session list %SERVER_NAME%:1545 --infobase=%infobase% --cluster=%cluster%`) do (
set %%i=%%j
if %%i EQU process (
for /f "usebackq tokens=1,2 delims=: " %%i in (`%RAC% session info %SERVER_NAME%:1545 --cluster=%cluster% --session=!session!`) do (
set %%i=%%j
if %%i EQU app-id (
echo !app-id! !user-name!
rem if !app-id! == BackgroundJob (
rem echo Waiting for !app-id! with id = !session!
rem goto Terminate
rem
rem ) else (
%RAC% session terminate %SERVER_NAME%:1545 --cluster=%cluster% --session=!session!
echo User session terminated !connection!
echo User session terminated !connection! >> %LOG_FILE%
rem )
)
)
)
)
echo User sessions terminated at %DATE% %TIME% >> %LOG_FILE%
echo User sessions terminated at %DATE% %TIME%
echo Backup started at %DATE% %TIME% >> %LOG_FILE%
echo Backup started at %DATE% %TIME%
set DirectiveBackup=/DumpIB "%BACKUP_DIR%\%IB_NAME%_%YYYYMMDD%.dt"
set DirectiveLog=/Out "%LOG_FILE%" -NoTruncate
set ConnectionParam=CONFIG /S %SERVER_NAME%\%IB_NAME% /N%IB_USER% /P%IB_PWD% /UC%PERMISSION_CODE%
set /a COUNTER=0
:do
set /a COUNTER+=1
%EXE% %ConnectionParam% %DirectiveBackup% %DirectiveLog%
:while
if exist "%BACKUP_DIR%\%IB_NAME%_%YYYYMMDD%.dt" (goto next)
echo Failed to do backup. Trying again...
if %COUNTER% geq 4 (goto next) else (goto do)
:next
echo Backup done at %DATE% %TIME% >> %LOG_FILE%
echo Backup done at %DATE% %TIME%
echo DB config update started at %DATE% %TIME% >> %LOG_FILE%
echo DB config update started at %DATE% %TIME%
set DirectiveUpdate=/UpdateDBCfg
set DirectiveLog=/Out "%LOG_FILE%" -NoTruncate
set ConnectionParam=CONFIG /S %SERVER_NAME%\%IB_NAME% /N%IB_USER% /P%IB_PWD% /UC%PERMISSION_CODE%
%EXE% %ConnectionParam% %DirectiveUpdate% %DirectiveLog%
echo DB config update done at %DATE% %TIME% >> %LOG_FILE%
echo DB config update done at %DATE% %TIME%
echo Permitting sessions and scheduled jobs at %DATE% %TIME% >> %LOG_FILE%
echo Permitting sessions and scheduled jobs at %DATE% %TIME%
%RAC% infobase update %SERVER_NAME%:1545 --cluster=%cluster% --infobase=%infobase% --sessions-deny=off --permission-code= --denied-from= --denied-to= --denied-message= --scheduled-jobs-deny=off --infobase-user=%IB_USER% --infobase-pwd=%IB_PWD%
echo Permitted sessions and scheduled jobs at %DATE% %TIME% >> %LOG_FILE%
echo Permitted sessions and scheduled jobs at %DATE% %TIME%
if exist "%BACKUP_DIR%\%IB_NAME%_%YYYYMMDD%.dt" (
copy "%BACKUP_DIR%\%IB_NAME%_%YYYYMMDD%.dt" "%RESERVE_DIR%\%IB_NAME%_%YYYYMMDD%.dt"
forfiles /p %BACKUP_DIR%\ /s /d -5 /c "cmd /c del /f /a /q @file"
forfiles /p %RESERVE_DIR%\ /s /d -5 /c "cmd /c del /f /a /q @file"
)
rem net stop "1C:Enterprise 8.3 Server Agent (x86-64)"
rem net start "1C:Enterprise 8.3 Server Agent (x86-64)"
exit
:IsEQU
if %1==%2 goto :%3