Batch file issue with dynamic calling of mariadb import

I have a batch file that has been runnign fine for months, during my vacation it started to fail, such nice timing! The temporary fix is to restart the mariadb server every day… not ideal.

I then tried to add a few parameters in the mysqldump which seems to have worked. When I started to change the call to import the data I got a few different errors depending on what I tried. This is the current batch file, later I’ll share the orginal 2 lines of code that used to work but that needs to also have the foreign_key_checks

Batch File

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@echo off
setlocal enableDelayedExpansion
REM Get current date in YYYYMMDD format
set "dt=%date:~0,4%%date:~5,2%%date:~8,2%"
REM Define backup path
set "backups_path=%~dp0backups"
REM Create the backups folder if it doesn't exist
if not exist "%backups_path%" mkdir "%backups_path%"
REM Load locations array and location length
call load_locations.bat
REM Loop through each location
for /l %%x in (0, 1, %locations_len%) do (
REM Build dynamic full path for each location
set "dump_file=%backups_path%!locations[%%x].brand!_!locations[%%x].name!_!locations[%%x].db!_%dt%.sql"
set "file_name=!locations[%%x].brand!_!locations[%%x].name!_!locations[%%x].db!_%dt%.sql"
IF NOT "%~1"=="--no-dump" (
REM Clean up old backups for this location, keeping only the latest two
call :printMessage "Cleaning up old backups for location !locations[%%x].db!"
for /f "skip=2 delims=" %%f in ('dir /b /o-d "%backups_path%!locations[%%x].brand!_!locations[%%x].name!_!locations[%%x].db!_*.sql"') do (
del "%backups_path%%%f"
call :printMessage "Deleted old backup file: %%f"
)
REM Perform the mysqldump
call :printMessage "Exporting database !locations[%%x].db! to !file_name!"
mysqldump --login-path=!locations[%%x].name! --single-transaction --quick --opt --extended-insert --disable-keys --add-drop-table --set-gtid-purged=OFF !locations[%%x].db! > !dump_file!
IF ERRORLEVEL 1 (
call :printMessage "mysqldump failed for database !locations[%%x].db!"
goto:eof
) ELSE (
call :printMessage "Export of !locations[%%x].db! completed successfully."
)
)
IF EXIST !dump_file! (
call :printMessage "Importing dump file !file_name! into database !locations[%%x].db!"
REM Build MariaDB import command
set "cmd_mariadb="C:Program FilesMariaDB 11.2binmariadb.exe" --defaults-extra-file="C:Mattdevkpismariadb_login.ini" !locations[%%x].db! -e "SET foreign_key_checks=0; source !dump_file!; SET foreign_key_checks=1;""
REM Execute MariaDB import command
REM call !cmd_mariadb!
cmd /c %cmd_mariadb%
IF ERRORLEVEL 1 (
call :printMessage "MariaDB import failed for !locations[%%x].db!"
goto:eof
) ELSE (
call :printMessage "Import of !locations[%%x].db! completed successfully."
)
) ELSE (
call :printMessage "Dump file !dump_file! not found, skipping import."
)
)
endlocal
goto:eof
REM Function to print messages to both console and log file
:printMessage
echo %dt% - %time:~0,8% - %~1 >> "%~dp0logs.txt"
echo %dt% - %time:~0,8% - %~1
goto:eof
</code>
<code>@echo off setlocal enableDelayedExpansion REM Get current date in YYYYMMDD format set "dt=%date:~0,4%%date:~5,2%%date:~8,2%" REM Define backup path set "backups_path=%~dp0backups" REM Create the backups folder if it doesn't exist if not exist "%backups_path%" mkdir "%backups_path%" REM Load locations array and location length call load_locations.bat REM Loop through each location for /l %%x in (0, 1, %locations_len%) do ( REM Build dynamic full path for each location set "dump_file=%backups_path%!locations[%%x].brand!_!locations[%%x].name!_!locations[%%x].db!_%dt%.sql" set "file_name=!locations[%%x].brand!_!locations[%%x].name!_!locations[%%x].db!_%dt%.sql" IF NOT "%~1"=="--no-dump" ( REM Clean up old backups for this location, keeping only the latest two call :printMessage "Cleaning up old backups for location !locations[%%x].db!" for /f "skip=2 delims=" %%f in ('dir /b /o-d "%backups_path%!locations[%%x].brand!_!locations[%%x].name!_!locations[%%x].db!_*.sql"') do ( del "%backups_path%%%f" call :printMessage "Deleted old backup file: %%f" ) REM Perform the mysqldump call :printMessage "Exporting database !locations[%%x].db! to !file_name!" mysqldump --login-path=!locations[%%x].name! --single-transaction --quick --opt --extended-insert --disable-keys --add-drop-table --set-gtid-purged=OFF !locations[%%x].db! > !dump_file! IF ERRORLEVEL 1 ( call :printMessage "mysqldump failed for database !locations[%%x].db!" goto:eof ) ELSE ( call :printMessage "Export of !locations[%%x].db! completed successfully." ) ) IF EXIST !dump_file! ( call :printMessage "Importing dump file !file_name! into database !locations[%%x].db!" REM Build MariaDB import command set "cmd_mariadb="C:Program FilesMariaDB 11.2binmariadb.exe" --defaults-extra-file="C:Mattdevkpismariadb_login.ini" !locations[%%x].db! -e "SET foreign_key_checks=0; source !dump_file!; SET foreign_key_checks=1;"" REM Execute MariaDB import command REM call !cmd_mariadb! cmd /c %cmd_mariadb% IF ERRORLEVEL 1 ( call :printMessage "MariaDB import failed for !locations[%%x].db!" goto:eof ) ELSE ( call :printMessage "Import of !locations[%%x].db! completed successfully." ) ) ELSE ( call :printMessage "Dump file !dump_file! not found, skipping import." ) ) endlocal goto:eof REM Function to print messages to both console and log file :printMessage echo %dt% - %time:~0,8% - %~1 >> "%~dp0logs.txt" echo %dt% - %time:~0,8% - %~1 goto:eof </code>
@echo off
setlocal enableDelayedExpansion

REM Get current date in YYYYMMDD format
set "dt=%date:~0,4%%date:~5,2%%date:~8,2%"

REM Define backup path
set "backups_path=%~dp0backups"

REM Create the backups folder if it doesn't exist
if not exist "%backups_path%" mkdir "%backups_path%"

REM Load locations array and location length
call load_locations.bat

REM Loop through each location
for /l %%x in (0, 1, %locations_len%) do (
    REM Build dynamic full path for each location
    set "dump_file=%backups_path%!locations[%%x].brand!_!locations[%%x].name!_!locations[%%x].db!_%dt%.sql"
    set "file_name=!locations[%%x].brand!_!locations[%%x].name!_!locations[%%x].db!_%dt%.sql"

    IF NOT "%~1"=="--no-dump" (
        REM Clean up old backups for this location, keeping only the latest two
        call :printMessage "Cleaning up old backups for location !locations[%%x].db!"
        for /f "skip=2 delims=" %%f in ('dir /b /o-d "%backups_path%!locations[%%x].brand!_!locations[%%x].name!_!locations[%%x].db!_*.sql"') do (
            del "%backups_path%%%f"
            call :printMessage "Deleted old backup file: %%f"
        )

        REM Perform the mysqldump
        call :printMessage "Exporting database !locations[%%x].db! to !file_name!"
        mysqldump --login-path=!locations[%%x].name! --single-transaction --quick --opt --extended-insert --disable-keys --add-drop-table --set-gtid-purged=OFF !locations[%%x].db! > !dump_file!

        IF ERRORLEVEL 1 (
            call :printMessage "mysqldump failed for database !locations[%%x].db!"
            goto:eof
        ) ELSE (
            call :printMessage "Export of !locations[%%x].db! completed successfully."
        )
    )

    IF EXIST !dump_file! (
        call :printMessage "Importing dump file !file_name! into database !locations[%%x].db!"

    REM Build MariaDB import command
    set "cmd_mariadb="C:Program FilesMariaDB 11.2binmariadb.exe" --defaults-extra-file="C:Mattdevkpismariadb_login.ini" !locations[%%x].db! -e "SET foreign_key_checks=0; source !dump_file!; SET foreign_key_checks=1;""

    REM Execute MariaDB import command
    REM call !cmd_mariadb!
    cmd /c %cmd_mariadb%


        IF ERRORLEVEL 1 (
            call :printMessage "MariaDB import failed for !locations[%%x].db!"
            goto:eof
        ) ELSE (
            call :printMessage "Import of !locations[%%x].db! completed successfully."
        )
    ) ELSE (
        call :printMessage "Dump file !dump_file! not found, skipping import."
    )
)

endlocal
goto:eof

REM Function to print messages to both console and log file
:printMessage
echo %dt% - %time:~0,8% - %~1 >> "%~dp0logs.txt"
echo %dt% - %time:~0,8% - %~1
goto:eof

load_location.bat

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@echo off
:: Sets location 1 settings
set locations[0].brand=aaa
set locations[0].name=a
set locations[0].db=a
:: Sets location 2 settings
set locations[1].brand=bbb
set locations[1].name=b
set locations[1].db=b
:: Find the # of items to loop through
set locations_len=0
:Loop
if defined locations[%locations_len%].name (
set /a locations_len+=1
GOTO :Loop
)
:: Adjust lenght for for loop
set /a locations_len-=1
</code>
<code>@echo off :: Sets location 1 settings set locations[0].brand=aaa set locations[0].name=a set locations[0].db=a :: Sets location 2 settings set locations[1].brand=bbb set locations[1].name=b set locations[1].db=b :: Find the # of items to loop through set locations_len=0 :Loop if defined locations[%locations_len%].name ( set /a locations_len+=1 GOTO :Loop ) :: Adjust lenght for for loop set /a locations_len-=1 </code>
@echo off

:: Sets location 1 settings
set locations[0].brand=aaa
set locations[0].name=a
set locations[0].db=a

:: Sets location 2 settings
set locations[1].brand=bbb
set locations[1].name=b
set locations[1].db=b

:: Find the # of items to loop through
set locations_len=0 

:Loop 
if defined locations[%locations_len%].name ( 
set /a locations_len+=1
GOTO :Loop 
)

:: Adjust lenght for for loop
set  /a locations_len-=1

mariadb_login.ini

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[client]
user=batch_user
password=password
host=127.0.0.1
port=3306
</code>
<code>[client] user=batch_user password=password host=127.0.0.1 port=3306 </code>
[client]
user=batch_user
password=password
host=127.0.0.1
port=3306

Error message”

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>C:Mattdevkpis>dump_load_allezup_rgp3.bat
20240917 - 13:40:47 - Cleaning up old backups for location a
20240917 - 13:40:47 - Exporting database a to file_20240917.sql
20240917 - 13:43:03 - Export of a completed successfully.
20240917 - 13:43:03 - Importing dump file file_20240917.sql into database allezup '""C:Program' is not recognized as an internal or external command, operable program or batch file.
20240917 - 13:43:03 - MariaDB import failed for a
</code>
<code>C:Mattdevkpis>dump_load_allezup_rgp3.bat 20240917 - 13:40:47 - Cleaning up old backups for location a 20240917 - 13:40:47 - Exporting database a to file_20240917.sql 20240917 - 13:43:03 - Export of a completed successfully. 20240917 - 13:43:03 - Importing dump file file_20240917.sql into database allezup '""C:Program' is not recognized as an internal or external command, operable program or batch file. 20240917 - 13:43:03 - MariaDB import failed for a </code>
C:Mattdevkpis>dump_load_allezup_rgp3.bat 
20240917 - 13:40:47 - Cleaning up old backups for location a 
20240917 - 13:40:47 - Exporting database a to file_20240917.sql 
20240917 - 13:43:03 - Export of a completed successfully. 
20240917 - 13:43:03 - Importing dump file file_20240917.sql into database allezup '""C:Program' is not recognized as an internal or external command, operable program or batch file. 
20240917 - 13:43:03 - MariaDB import failed for a

I’ve tried to use ” and “”

Also tried

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> set cmd_mariadb=""C:Program FilesMariaDB 11.2binmariadb.exe" --defaults-extra-file="C:Mattdevkpismariadb_login.ini" !locations[%%x].db! -e "SET foreign_key_checks=0; source '"!dump_file!"'; SET foreign_key_checks=1;""
</code>
<code> set cmd_mariadb=""C:Program FilesMariaDB 11.2binmariadb.exe" --defaults-extra-file="C:Mattdevkpismariadb_login.ini" !locations[%%x].db! -e "SET foreign_key_checks=0; source '"!dump_file!"'; SET foreign_key_checks=1;"" </code>
    set cmd_mariadb=""C:Program FilesMariaDB 11.2binmariadb.exe" --defaults-extra-file="C:Mattdevkpismariadb_login.ini" !locations[%%x].db! -e "SET foreign_key_checks=0; source '"!dump_file!"'; SET foreign_key_checks=1;""

I’ve used a GPT, I know I know, and couldn’t make much progress:

The previous version of this file was too slow so I tried to add parameters to speed things up and avoid the mariadb server to timeout.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> set cmd_mariadb="C:Program FilesMariaDB 11.2binmariadb.exe" --defaults-extra-file=C:Mattdevkpismariadb_login.ini !locations[%%x].db! -e "source !dump_file!"
call !cmd_mariadb!
</code>
<code> set cmd_mariadb="C:Program FilesMariaDB 11.2binmariadb.exe" --defaults-extra-file=C:Mattdevkpismariadb_login.ini !locations[%%x].db! -e "source !dump_file!" call !cmd_mariadb! </code>
    set cmd_mariadb="C:Program FilesMariaDB 11.2binmariadb.exe" --defaults-extra-file=C:Mattdevkpismariadb_login.ini !locations[%%x].db! -e "source !dump_file!"
    call !cmd_mariadb!

New contributor

Matt A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật