Project

General

Profile

Restoration of a broken database » restoredb.bat

Edvīns Liepa, 2021-03-09 15:27

 
@echo off
setlocal EnableExtensions EnableDelayedExpansion

set mastersoft_dir=C:\Mastersoft
set dbadm=%mastersoft_dir%\Deployment\DbAdm\DbAdm.exe
set sysexp=%mastersoft_dir%\Deployment\SysExp\SysExp.exe
set dsn=IPSDATABASE
set dbpath=C:\Mastersoft\Database\IPSDATABASE

if "%~1" neq "" (
set dsn=%~1
if "%~2" neq "" (
set dbpath=%~2
)
)

if not exist "%dbpath%" (
echo Location "%dbpath%" for "%dsn%" does not exist. Exiting...
exit /b 1
)
echo Restoration of "%dsn%" located at "%dbpath%" will be performed

set /p recreate_tables="Enter a comma separated list of tables to recreate, or leave empty for none: "
if /I "!recreate_tables!" equ "" (
echo No tables will be recreated
) else (
echo Tables %recreate_tables% will be recreated
)

echo Warning: Database triggers in the broken database shall be deleted

set /p UserAgree=Continue? (y/n):
if /I "%UserAgree%" neq "y" (
goto:eof
)

echo Restoring BOS database...

echo Creating brand new %dsn%...
%dbadm% --opts=nokey --create --db=%dbpath% --dbn=%dsn% --dsn=%dsn%
if ERRORLEVEL 1 goto:eof

echo Copying data from broken database...
for /L %%i in (0, 1, 99) do (
set table_num=0%%i
set table_num=!table_num:~-2!
set must_copy=1
for %%r in ("%recreate_tables:,=" "%") do (
if "%%~r" equ "!table_num!" (
set must_copy=0
echo Skipping copying of table !table_num!...
)
)
if "!must_copy!" equ "1" (
echo Copying table DATA_!table_num!...
if "!table_num!" equ "43" (
%dbadm% --opts=nokey --dsn=%dsn% --create --tables=43
if ERRORLEVEL 1 exit /b 1
echo insert into DATA_43 select * from brokendb.DATA_43;>copy.sql
%dbadm% --opts=nokey --dsn=%dsn% --script --file=copy.sql
if ERRORLEVEL 1 (
%dbadm% --opts=nokey --dsn=%dsn% --delete -f --tables=43
exit /b 1
)
) else (
echo select * into DATA_!table_num! from brokendb.DATA_!table_num!;>copy.sql
%dbadm% --opts=nokey --dsn=%dsn% --script --file=copy.sql
if ERRORLEVEL 1 exit /b 1
)
)
)
for /L %%i in (100, 1, 113) do (
set table_num=%%i
set must_copy=1
for %%r in ("%recreate_tables:,=" "%") do (
if "%%~r" equ "!table_num!" (
set must_copy=0
echo Skipping copying of table !table_num!...
)
)
if "!must_copy!" equ "1" (
echo Copying table DATA_!table_num!...
echo select * into DATA_!table_num! from brokendb.DATA_!table_num!;>copy.sql
%dbadm% --opts=nokey --dsn=%dsn% --script --file=copy.sql
if ERRORLEVEL 1 exit /b 1
)
)

if "!recreate_tables!" neq "" (
echo Creating lost tables...
%dbadm% --opts=nokey --dsn=%dsn% --create --tables=!recreate_tables!
if ERRORLEVEL 1 goto:eof
)

echo Migrating tables...
%dbadm% --opts=nokey --dsn=%dsn% --migrate --tables=all
if ERRORLEVEL 1 exit /b 1

echo %dsn% restored successfully.
    (1-1/1)