@ECHO OFF
REM (ZZZZZZBACKUP.BAT) BACKUP DATABASE AND SYSTEM 
REM 1=i_ver 2=lang 3={TEST,REAL} 4=system 5=work
IF "%5"=="" EXIT /B
SET I_EXIT=%SYSTEMDRIVE%\I\I.%4\TMP_%3\%5.EXIT
ECHO START ZZZZZZBACKUP.BAT > %I_EXIT%
SET I_VER=%1
SET I_SYSTEM=%4
SET I_WORK=%5
IF "%3"=="TEST" GOTO FIN
SET I_ADD=
SHIFT&SHIFT&SHIFT&SHIFT&SHIFT
REM (SHIFT/5) 1=db        2=server_name 3=date_time 4=drive2 5=drive14 6=path1/3 7=path2/3 8=path3/3
REM (SHIFT/5) 1=COPY_BACKUP 2=drive  3=file
REM (SHIFT/5) 1=ERASE_LOG 2=db            3=drive     4=file
REM (SHIFT/5) 1=ERASE_DAT 2=drive         3=file
REM (SHIFT/5) 1=MOVE_DAT  2=drive
REM (SHIFT/5) 1=DIR       2=drive         3=path
REM (SHIFT/5) 1=SAVE 2=in_drive  3=out_drive 4=in_dir1 5=in_dir2
SET TMP=%SYSTEMDRIVE%\I\I.%I_SYSTEM%\TMP_REAL
SET BACKUP=I.BACKUP.%I_SYSTEM%.%1.%2.%3
SET PT=%6
IF NOT "%7"=="" SET PT=%6 %7
IF NOT "%8"=="" SET PT=%6 %7 %8
IF "%1"=="COPY_BACKUP" GOTO COPY_BACKUP
IF "%1"=="ERASE_LOG" GOTO ERASE_LOG
IF "%1"=="ERASE_DAT" GOTO ERASE_DAT
IF "%1"=="DIR"   GOTO DIR
IF "%1"=="SAVE"  GOTO SAVE
ECHO  PROGRAM ERROR > %I_EXIT%
IF "%1"=="MOVE_DAT"  GOTO MOVE_DAT
IF "%1"=="M" GOTO BACKUP
IF "%1"=="P" GOTO BACKUP
IF "%1"=="D" GOTO BACKUP 
IF "%1"=="O" GOTO BACKUP 
IF "%1"=="I" GOTO BACKUP 
IF "%1"=="L" GOTO BACKUP 
ECHO PARAMATER1 %1 ERROR > %I_EXIT%
EXIT /B
REM ***(SAVE)*** 1=SAVE 2=in_drive  3=out_drive  4=in_dir1 5=in_dir2
:SAVE 
ECHO (ERROR) save_directory is space > %I_EXIT%
IF "%3"=="" EXIT /B 
SET SAVE_IN=%2:\
SET SAVE_OUT=%3:\I-SAVE-%I_SYSTEM%-%2
IF "%4"=="" GOTO SVAE_GO
SET SAVE_IN=%SAVE_IN%%4\
SET SAVE_OUT=%SAVE_OUT%-%4
IF "%5"=="" GOTO SVAE_GO
SET SAVE_IN=%SAVE_IN%%5\
SET SAVE_OUT=%SAVE_OUT%-%5
:SAVE_GO
ECHO (ERROR) ROBOCOPY %SAVE_IN% %SAVE_OUT% /MIR /COPYALL /DCOPY:DAT /R:0 /W:0 /NP /XJD /XJF /FFT > %I_EXIT%
             ROBOCOPY %SAVE_IN% %SAVE_OUT% /MIR /COPYALL /DCOPY:DAT /R:0 /W:0 /NP /XJD /XJF /FFT 
IF ERRORLEVEL 8 EXIT /B
GOTO FIN_OK
REM ***(COPY_BACKUP)*** 1=COPY_BACKUP 2=drive 3=file
:COPY_BACKUP 
COPY /Y %2:\I\BACKUP\%3 %SYSTEMDRIVE%\I\TMP 2> %I_EXIT%
IF ERRORLEVEL 1 EXIT /B
GOTO FIN_OK
REM ***(ERASE_LOG)*** 1=ERASE_LOG 2=db 3=drive 4=file
:ERASE_LOG
IF "%2"=="I" SET I_ADD=_%I_SYSTEM%
ERASE /Q %3:\I\BACKUP_LOG_%2%I_ADD%\%4 2> %I_EXIT%
IF ERRORLEVEL 1 EXIT /B
GOTO FIN_OK
REM ***(ERASE_DAT)*** 1=ERASE_DAT 2=drive 3=file
:ERASE_DAT 
ERASE /Q %2:\I\BACKUP\I.BACKUP.%I_SYSTEM%.%3 2> %I_EXIT%
IF ERRORLEVEL 1 EXIT /B
GOTO FIN_OK
REM ***(DIR)*** 1=DIR 2=drive 3=path
:DIR 
DIR /B %2:\%3 > %TMP%\%I_WORK%.TXT
GOTO FIN_OK
REM ***(MOVE_DAT) 1=MOVE_DAT 2=drive
:MOVE_DAT 
MOVE /Y %TMP%\I.BACKUP.* %2:\I\BACKUP 2> %I_EXIT%
IF ERRORLEVEL 1 EXIT /B
GOTO FIN_OK
REM *****( BACKUP )********************************************
:BACKUP
IF NOT EXIST %TMP%\%BACKUP%              MKDIR %TMP%\%BACKUP%
IF NOT EXIST %TMP%\%BACKUP%\BACKUP_DAT   MKDIR %TMP%\%BACKUP%\BACKUP_DAT
rem IF NOT EXIST %TMP%\%BACKUP%\BACKUP_DAT\I MKDIR %TMP%\%BACKUP%\BACKUP_DAT\I
rem FOR %%I IN (SHARE) DO (
rem     XCOPY /E /I /Y %SystemDrive%\I\%%I %TMP%\%BACKUP%\I\%%I  2>%I_EXIT%
rem     IF ERRORLEVEL 1 EXIT /B
rem )
FOR %%I IN (LINUX WINDOWS) DO (
    XCOPY /I /Y %SystemDrive%\I\SHARE\%%I\SETUP_*.* %TMP%\%BACKUP%\I\SHARE\%%I  2>%I_EXIT%
    IF ERRORLEVEL 1 EXIT /B
)
rem XCOPY /I /Y %SystemDrive%\I\%I_VER%*   %TMP%\%BACKUP%\I 2>%I_EXIT%
rem IF ERRORLEVEL 1 EXIT /B
FOR %%I IN (BAT DATA_REAL) DO (
    XCOPY /I /Y %SystemDrive%\I\I.%I_SYSTEM%\%%I\*.* %TMP%\%BACKUP%\I\I.%I_SYSTEM%\%%I 2>%I_EXIT%
    IF ERRORLEVEL 1 EXIT /B
)
rem FOR %%I IN (DATA_TEST TMP_REAL TMP_TEST) DO (
rem     MKDIR %TMP%\%BACKUP%\I\I.%I_SYSTEM%\%%I 
rem )
rem FOR /L %%I IN (1,1,9)                    DO (
rem XCOPY /E /I /Y %SystemDrive%\I\I.%I_SYSTEM%\DATA_OLD%%I_REAL %TMP%\%BACKUP%\I\I.%I_SYSTEM%\DATA_OLD%%I_REAL 2>%I_EXIT%
rem IF ERRORLEVEL 1 EXIT /B
rem     MKDIR %TMP%\%BACKUP%\I\I.%I_SYSTEM%\DATA_OLD%%I_REAL
rem )
rem XCOPY /I /Y %SystemDrive%\I\BIN%I_VER%\*.*   %TMP%\%BACKUP%\I\BIN%I_VER% 2>%I_EXIT%
rem IF ERRORLEVEL 1 EXIT /B
IF "%1"=="M" GOTO M
IF "%1"=="O" GOTO O 
IF "%1"=="P" GOTO P
CALL %SystemDrive%\I\I.%I_SYSTEM%\ZZZZZZPASSWORD.BAT
IF "%1"=="D" GOTO D 
IF "%1"=="I" GOTO I 
IF "%1"=="L" GOTO L 
EXIT /B
REM *******************( 1=db 2=server 3=date_time 4=drive2 5=drive14 6=path1/3 7=path2/3 8=path3/3 )*********************************
REM *****(Firebird (D))***(ONLY)**
:D
"%PT%\gbak" -b -e -t -user SYSDBA -password %PASSWORD% %5:\I\DATABASE_DAT_D\I_%I_SYSTEM%.FDB %TMP%\%BACKUP%\BACKUP_DAT\BACKUP_DAT_%I_SYSTEM% 2>%I_EXIT%
IF ERRORLEVEL 1 EXIT /B
GOTO COPY_BACKUP_DAT
REM *****(BD2 (I))****(ONLY)***
:I
SET I_ADD=_%I_SYSTEM%
DIR /B /S %4:\I\BACKUP_LOG_I%I_ADD%\DB2\I_%I_SYSTEM%\*.LOG > %TMP%\%I_WORK%.TXT
DB2CMD -C -I -W DB2 BACKUP DB I_%I_SYSTEM% USER DB2ADMIN USING %PASSWORD% ONLINE TO %TMP%\%BACKUP%\BACKUP_DAT INCLUDE LOGS 2>%I_EXIT%
IF ERRORLEVEL 1 EXIT /B
GOTO COPY_BACKUP_DAT
REM *****(MariaDB (L))***(ALL)**
:L
DIR /B %4:\I\BACKUP_LOG_L\ > %TMP%\%I_WORK%.TXT
"%PT%\bin\mysqldump" -uI_ROOT -p%PASSWORD% --flush-logs --master-data=2 --lock-all-tables --all-databases -x > %TMP%\%BACKUP%\BACKUP_DAT\BACKUP_DAT 2>%I_EXIT%
IF ERRORLEVEL 1 EXIT /B
GOTO COPY_BACKUP_DAT
REM *****(Oracle (O))***(ALL)**
:O
COPY %PT% %TMP%\%BACKUP%\BACKUP_DAT 2>%I_EXIT%
IF ERRORLEVEL 1 EXIT /B
GOTO COPY_BACKUP_DAT
REM *****(PostgreSQL (P))***(ALL)**
:P
REM ***( NOW )****
XCOPY /E /I /Y "%PT%" %TMP%\%BACKUP%\BACKUP_DAT 2>%I_EXIT% 
IF ERRORLEVEL 1 EXIT /B
REM ***( NEW BEGIN )**** 
REM SET PGPASSWORD=XXXXXXXX
REM SET PGBASEBACKUP=%SystemDrive%\Program Files\PostgreSQL\12\bin\pg_basebackup
REM "%PGBASEBACKUP%" -U postgres -w -D %TMP%\%BACKUP%\BACKUP_DAT 2>%I_EXIT%
REM IF ERRORLEVEL 1 EXIT /B
REM ***( NEW END )**** 
RMDIR /S /Q %TMP%\%BACKUP%\BACKUP_DAT\pg_xlog        2>%I_EXIT%
ERASE    /Q %TMP%\%BACKUP%\BACKUP_DAT\postmaster.pid 2>%I_EXIT%
IF ERRORLEVEL 1 EXIT /B
GOTO COPY_BACKUP_DAT
REM *****(MSSQL (M))***(ONLY)**
REM (SHIFT/5) 1=M 2=server_name 3=date_time 4=real_drive 5=test_drive
:M 
XCOPY /I /Y %4:\I\BACKUP_DAT_M\I_%I_SYSTEM%_R_*.DAT %TMP%\%BACKUP%\BACKUP_DAT 2>%I_EXIT%
IF ERRORLEVEL 1 EXIT /B
XCOPY /I /Y %5:\I\BACKUP_DAT_M\I_%I_SYSTEM%_T_*.DAT %TMP%\%BACKUP%\BACKUP_DAT 2>%I_EXIT%
IF ERRORLEVEL 1 EXIT /B
IF NOT EXIST %4:\I\BACKUP_LOG_M\OLD IF NOT EXIST     %4:\I\BACKUP_LOG_M\I_%I_SYSTEM%_R_*.LOG GOTO M_TEST
XCOPY /I /Y  %4:\I\BACKUP_LOG_M\I_%I_SYSTEM%_R_*.LOG %4:\I\BACKUP_LOG_M\OLD 2>%I_EXIT%
IF ERRORLEVEL 1 EXIT /B
:M_TEST
IF NOT EXIST %5:\I\BACKUP_LOG_M\OLD IF NOT EXIST     %5:\I\BACKUP_LOG_M\I_%I_SYSTEM%_T_*.LOG GOTO FIN_OK
XCOPY /I /Y  %5:\I\BACKUP_LOG_M\I_%I_SYSTEM%_T_*.LOG %5:\I\BACKUP_LOG_M\OLD 2>%I_EXIT%
IF ERRORLEVEL 1 EXIT /B
GOTO FIN_OK
REM ***( COPY_BACKUP_DAT )*****
:COPY_BACKUP_DAT
IF "%1"=="M" GOTO RMDIR_SKIP
IF "%1"=="D" GOTO RMDIR_SKIP 
RMDIR /S /Q %4:\I\BACKUP_DAT_%1%I_ADD%
:RMDIR_SKIP
IF NOT EXIST %4:\I\BACKUP_DAT_%1%I_ADD% MKDIR %4:\I\BACKUP_DAT_%1%I_ADD%
XCOPY /E /I /Y %TMP%\%BACKUP%\BACKUP_DAT      %4:\I\BACKUP_DAT_%1%I_ADD% 2>%I_EXIT%
IF ERRORLEVEL 1 EXIT /B
REM ***( FIN_OK )*****
:FIN_OK
ECHO !!! > %I_EXIT%
