rem @echo off
rem Batch file for safe shutting down and restarting Firebird
rem This Batch file is only for Win-NT, not Win-9x

SET BATLOGFILE=%1
IF "%BATLOGFILE%" == "" SET BATLOGFILE="Nightly.log"
echo ------------------------------------------------- >> %BATLOGFILE%
Date /T >> %BATLOGFILE%
Time /T >> %BATLOGFILE%

rem ========= Step 0: stop Isectd =========
echo try Stop Isectd services... >> %BATLOGFILE%
..\isectD\isdClient -h localhost:5501 -c "stop all"
rem if errorlevel 1 GOTO NOT_STOPPED_ISECTD
start /w cscript sleep.vbs 5
echo stopped Isectd services OK >> %BATLOGFILE%

rem ========= Step 1: backup database =========
echo backup Database... >> %BATLOGFILE%
..\FirebirdSQL\gbak -B -t -user SYSDBA -pass masterkey -se service_mgr "..\YourData\Classmaker++.fdb" "..\YourData\Classmaker++.bak"
if errorlevel 1 GOTO NOT_BACKEDUP
echo backup Database OK >> %BATLOGFILE%

rem ========= Step 2: restore database =========
echo restore Database... >> %BATLOGFILE%
IF EXIST ..\YourData\Classmaker++.lkg del ..\YourData\Classmaker++.lkg
..\FirebirdSQL\gbak -R -user SYSDBA -pass masterkey  -c -p 8192 -se service_mgr "..\YourData\Classmaker++.bak" "..\YourData\Classmaker++.lkg"
if errorlevel 1 GOTO NOT_RESTORED
echo restore Database OK >> %BATLOGFILE%
echo copying last known good Database over on-line Database... >> %BATLOGFILE%
copy "..\YourData\Classmaker++.lkg" "..\YourData\Classmaker++.fdb"
if errorlevel 1 GOTO NOT_COPIED
echo copying last known good Database over on-line Database OK >> %BATLOGFILE%

rem ========= Step 3: start Isectd =========
echo try Start Isectd services... >> %BATLOGFILE%
..\IsectD\isdClient -h localhost:5501 -c "start all"
rem if errorlevel 1 GOTO NOT_STARTED_ISECTD
echo started Isectd services OK >> %BATLOGFILE%

GOTO END_BATCH

:NOT_STOPPED_ISECTD
echo Stop Isectd services failed! >> %BATLOGFILE%
GOTO END_BATCH

:NOT_STARTED_ISECTD
echo Start Isectd services failed! >> %BATLOGFILE%
GOTO END_BATCH

:NOT_BACKEDUP
type errout.tmp >> %BATLOGFILE%
echo Backup failed! >> %BATLOGFILE%
GOTO END_BATCH

:NOT_RESTORED
echo Restore failed! >> %BATLOGFILE%
GOTO END_BATCH

:NOT_COPIED
echo Copy failed! >> %BATLOGFILE%
GOTO END_BATCH

:END_BATCH
rem del *.tmp
