备份 MySql 同时使用批处理文件排除某些数据库,我 运行 遇到了问题

Backing up MySql while excluding certain databases with batch file and I'm running into problems

我正在尝试通过批处理文件在 Windows 2012 上备份我的 MySql 数据库。

这是我的:

    %mysql% -u %USER% -p%PASSWORD% -B -s -e"show databases" > %backupdir%\mysqldblist.tmp

    FOR /F %%D IN (%backupdir%\mysqldblist.tmp) DO (
        set DONTBACKUP=NOTHING
        ECHO Creating backup for database ''%%D''
        if [%%D]==[information_schema] set DONTBACKUP=TRUE
        if [%%D]==[mysql] set DONTBACKUP=TRUE
        if [%%D]==[performance_schema] set DONTBACKUP=TRUE
        if [%%D]==[test] set DONTBACKUP=TRUE
        PAUSE
        IF [%DONTBACKUP%]==[TRUE] %mysqldump% -u %USER% -p%PASSWORD% --result-file="%backupdir%%%D_%mydate%.sql" "%%D" 
        PAUSE
    )

我正在尝试排除 mysql、performance_schema 和测试等数据库,但它在 第一次迭代 后退出。我想我只是设置一个变量并检查它是否是某个数据库,但它没有发生,首先检查它是否退出。

有人能指出我做错了什么吗?

附加信息

这是我在命令提示符中看到的 window,如果有帮助:

ECHO Creating backup for database '%D'
if [%D] == [information_schema] set DONTBACKUP=TRUE
if [%D] == [mysql] set DONTBACKUP=TRUE
if [%D] == [performance_schema] set DONTBACKUP=TRUE
if [%D] == [test] set DONTBACKUP=TRUE
PAUSE
IF [] == [TRUE] "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump"...

我发现有些地方不对,我正在尝试调试它,但我没有立即发现。我不确定为什么没有填充 %%D 或为什么 DONTBACKUP...

附加信息 #2

这是我认为失败的地方:

 if [information_schema] == [information_schema] set DONTBACKUP=TRUE

它没有设置为 TRUE,所以它会尝试进行备份。我不确定我忽略了什么,但为什么这个比较会失败?

在文件开头添加这一行:

setlocal enabledelayedexpansion

在for循环中,查看DONTBACKUP的值时,使用!DONTBACKUP!。 另外,按照逻辑,应该反转 DONTBACKUP 的检查。

修改后的文件如下:

setlocal enabledelayedexpansion
%mysql% -u %USER% -p%PASSWORD% -B -s -e"show databases" > %backupdir%\mysqldblist.tmp

FOR /F %%D IN (%backupdir%\mysqldblist.tmp) DO (
    set DONTBACKUP=NOTHING
    ECHO Creating backup for database ''%%D''
    if [%%D]==[information_schema] set DONTBACKUP=TRUE
    if [%%D]==[mysql] set DONTBACKUP=TRUE
    if [%%D]==[performance_schema] set DONTBACKUP=TRUE
    if [%%D]==[test] set DONTBACKUP=TRUE
    PAUSE
    IF not [!DONTBACKUP!]==[TRUE] %mysqldump% -u %USER% -p%PASSWORD% --result-file="%backupdir%%%D_%mydate%.sql" "%%D" 
    PAUSE
)