diff --git a/base/Resources/mysql/import-data-into-mysql.bat b/base/Resources/mysql/import-data-into-mysql.bat index 9ae7044..89073cd 100644 --- a/base/Resources/mysql/import-data-into-mysql.bat +++ b/base/Resources/mysql/import-data-into-mysql.bat @@ -3,75 +3,171 @@ setlocal enabledelayedexpansion chcp 65001 >nul title Import Data Into MySQL -:: MySQL connection configuration +set "SCRIPT_PATH=%~dp0" + set MYSQL_HOST=127.0.0.1 set MYSQL_PORT=33306 set MYSQL_USER=root set MYSQL_PASS=Root@2025 set MYSQL_EXE=mysql.exe - -:: Script directory set SCRIPTS_DIR=script -:: 1. Check if mysql.exe exists where %MYSQL_EXE% >nul 2>&1 if %errorLevel% neq 0 ( - echo Error: mysql.exe not found in PATH - pause - exit /b 1 + call :log_error Error: mysql.exe not found in PATH + pause && exit /b 1 +) + +where rar.exe >nul 2>&1 +if %errorLevel% neq 0 ( + call :log_error Error: rar.exe not found in PATH + call :log_error Please install WinRAR or add it to PATH + pause && exit /b 1 ) -:: 2. Check if scripts directory exists if not exist "%SCRIPTS_DIR%" ( - echo Error: Scripts directory not found at %SCRIPTS_DIR% - pause - exit /b 1 + call :log_error Error: Scripts directory not found at %SCRIPTS_DIR% + pause && exit /b 1 ) -:: 3. Verify MySQL connection -echo Testing MySQL connection... +call :log_info Testing MySQL connection... %MYSQL_EXE% -h %MYSQL_HOST% -P %MYSQL_PORT% -u %MYSQL_USER% -p%MYSQL_PASS% -e "SELECT 1" >nul 2>&1 if %errorLevel% neq 0 ( - echo Error: Failed to connect to MySQL server - echo Please verify your connection parameters: - echo Host: %MYSQL_HOST% - echo Port: %MYSQL_PORT% - echo User: %MYSQL_USER% + call :log_error Failed to connect to MySQL server + call :log_error Please verify your connection parameters: + call :log_error Host: %MYSQL_HOST% + call :log_error Port: %MYSQL_PORT% + call :log_error User: %MYSQL_USER% + pause && exit /b 1 +) +call :log_info MySQL connection successful. Starting import process... + +call :GetDate +set CURRENT_DATE=%FormattedDate% +call :log_info CURRENT_DATE: %CURRENT_DATE% +set TEMP_SQL_DIR=%temp%\%CURRENT_DATE% +if not exist "!TEMP_SQL_DIR!" mkdir "!TEMP_SQL_DIR!" +call :log_info TEMP_SQL_DIR: %TEMP_SQL_DIR% + +for /d %%D in ("%SCRIPTS_DIR%\*") do ( + set "folder=%%~nxD" + call :log_info Processing database: !folder! + + call :log_info Creating database '!folder!' if not exists... + %MYSQL_EXE% -h %MYSQL_HOST% -P %MYSQL_PORT% -u %MYSQL_USER% -p%MYSQL_PASS% -e "CREATE DATABASE IF NOT EXISTS `!folder!`;" + + if %errorLevel% neq 0 ( + call :log_error Failed to create database '!folder!' + pause && exit /b 1 + ) + + for %%F in ("%%D\*.sql") do ( + call :log_info Importing file: %%~nxF into database !folder! + %MYSQL_EXE% -h %MYSQL_HOST% -P %MYSQL_PORT% -u %MYSQL_USER% -p%MYSQL_PASS% !folder! < "%%F" + + if %errorLevel% neq 0 ( + call :log_error Failed to import file %%~nxF + pause && exit /b 1 + ) + + copy "%%F" "!TEMP_SQL_DIR!\" >nul + del "%%F" >nul + ) + call :log_info Completed processing database: !folder! + call :log_info ---------------------------------------- +) + +call :log_info All SQL files imported successfully! +call :rar +exit /b 0 + +:log_info +call :GetFormattedTime +echo [%FormattedTime%] - [INFO] - %* +exit /b 0 + +:log_warn +call :GetFormattedTime +echo [%FormattedTime%] - [WARN] - %* +exit /b 0 + +:log_error +call :GetFormattedTime +echo [%FormattedTime%] - [ERROR] - %* +exit /b 0 + +:rar +call :RandomStr 16 RAND_NAME +set TARGET_DIR=%temp%\!CURRENT_DATE! +if not exist "!TARGET_DIR!" mkdir "!TARGET_DIR!" + +call :log_info Creating encrypted archive... +rar a -ep1 -hp"Password#!CURRENT_DATE!" "!TARGET_DIR!\!RAND_NAME!.rar" "!TEMP_SQL_DIR!\*" >nul + +if %errorLevel% neq 0 ( + call :log_error Error: Failed to create encrypted archive pause exit /b 1 ) -echo MySQL connection successful. Starting import process... +if not exist "%SCRIPT_PATH%/patch/!CURRENT_DATE!" mkdir "%SCRIPT_PATH%/patch/!CURRENT_DATE!" +call :log_info TEMP_SQL_DIR: %TEMP_SQL_DIR% -:: Process all folders in the scripts directory -for /d %%D in ("%SCRIPTS_DIR%\*") do ( - set "folder=%%~nxD" - echo Processing database: !folder! - - :: Create database (if not exists) - echo Creating database '!folder!' if not exists... - %MYSQL_EXE% -h %MYSQL_HOST% -P %MYSQL_PORT% -u %MYSQL_USER% -p%MYSQL_PASS% -e "CREATE DATABASE IF NOT EXISTS `!folder!`;" - - if %errorLevel% neq 0 ( - echo Error: Failed to create database '!folder!' - pause - exit /b 1 - ) - - :: Process all .sql files in the current folder - for %%F in ("%%D\*.sql") do ( - echo Importing file: %%~nxF into database !folder! - %MYSQL_EXE% -h %MYSQL_HOST% -P %MYSQL_PORT% -u %MYSQL_USER% -p%MYSQL_PASS% !folder! < "%%F" - - if %errorLevel% neq 0 ( - echo Error: Failed to import file %%~nxF - pause - exit /b 1 - ) - ) - echo Completed processing database: !folder! - echo ---------------------------------------- +mv !TEMP_SQL_DIR!/*.rar "%SCRIPT_PATH%/patch/!CURRENT_DATE!" +rd /s /q "!TEMP_SQL_DIR!" 2>nul + +call :log_info Encryption completed successfully! +call :log_info Archive: !TARGET_DIR!\!RAND_NAME!.rar +call :log_info Password: Password#!CURRENT_DATE! + +exit /b 0 + +:RandomStr +setlocal +set "len=%~1" +set "varname=%~2" +if "!varname!"=="" set "varname=RAND_STR" + +set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" +set "random_str=" + +for /l %%i in (1,1,!len!) do ( + set /a rand_index=!random! %% 62 + call set "char=%%chars:~!rand_index!,1%%" + set "random_str=!random_str!!char!" ) -echo All SQL files imported successfully! -pause \ No newline at end of file +endlocal & set "%varname%=%random_str%" +exit /b 0 + +:Start +setlocal +set DESCRIPTION=%1 +set SCRIPT_PATH=%2 +set SCRIPT_FILE=%3 + +if exist "%SCRIPT_PATH%\%SCRIPT_FILE%" ( + echo. + call :log_info Starting %DESCRIPTION%... + cd /d "%SCRIPT_PATH%" + start "%DESCRIPTION% Startup" /B cmd /c "%SCRIPT_FILE%" + if errorlevel 1 ( + call :log_error %DESCRIPTION% start Failed. + pause && exit /b 1 + ) +) else ( + call :log_error Script file not found: %SCRIPT_PATH%\%SCRIPT_FILE% + pause && exit /b 1 +) +endlocal +exit /b 0 + +:GetFormattedTime +for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "datetime=%%a" +set "FormattedTime=%datetime:~0,4%-%datetime:~4,2%-%datetime:~6,2% %datetime:~8,2%:%datetime:~10,2%:%datetime:~12,2%" +exit /b 0 + +:GetDate +for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "datetime=%%a" +set "FormattedDate=%datetime:~0,4%%datetime:~4,2%%datetime:~6,2%" +exit /b 0 \ No newline at end of file