weird behaviour in a Windows 2019 .BAT script
I have to restore a backup from a Linux MariaDB to a Windows MariaDB, where the PowerBI gateway will import its data. "mariabackup" is MariaDB's physical backup tool. But to restore it, the destination folder (%programfiles%\MariaDB 10.3\data\
) must be empty.
Since rmdir /S /Q "%programfiles%\MariaDB 10.3\data\
will remove the "data" directory (what I don't want!!!), I have been working to avoid this unwanted behavior in the following script (uncompress.bat):
rem Uncompress the backup sent by the linux server
rem and imports it to MariaDB
rem Gilberto Martins - 19/11/2021
rem Uncompress the backup
rem The backup path is "mnt\external01\"
tar -xf c:\users\mariabkp\bkp.tgz -C c:\users\mariabkp\
rem Stop MariaDB
net stop mysql
rem Prepare the Backup for Restoration
"C:\Program Files\MariaDB 10.3\bin\mariabackup.exe" --prepare --target-dir="c:\users\mariabkp\mnt\external01\backup"\
rem Erase the Database files
del /q "C:\Program Files\MariaDB 10.3\data\*.*"
FOR /D %p IN ("c:\Program Files\MariaDB 10.3\data\*") DO rmdir "%p" /s /q
rem Import backup to MariaDB
"C:\Program Files\MariaDB 10.3\bin\mariabackup.exe" --move-back --target-dir="c:\users\mariabkp\mnt\external01\backup"\
rem Restore MariaDB conf file
copy "c:\Users\Administrator\my.ini" "c:\Program Files\MariaDB 10.3\data" /y
rem Start MariaDB
net start mysql
I works well, up to the point where I have to erase the files to the restoration, as you can see ahead:
Microsoft Windows [Version 10.0.17763.1935]
(c) 2018 Microsoft Corporation. All rights reserved.
C:\Users\Administrator>uncompress.bat
C:\Users\Administrator>rem Uncompress the backup sent by the linux server
C:\Users\Administrator>rem and imports it to MariaDB
C:\Users\Administrator>rem Gilberto Martins - 19/11/2021
C:\Users\Administrator>rem Uncompress the backup
C:\Users\Administrator>rem The backup path is "mnt\external01\backup"
C:\Users\Administrator>tar -xf c:\users\mariabkp\bkp.tgz -C c:\users\mariabkp\
C:\Users\Administrator>rem Stop MariaDB
C:\Users\Administrator>net stop mysql
The MySQL service is stopping.
The MySQL service was stopped successfully.
C:\Users\Administrator>rem Prepare the Backup for Restoration
C:\Users\Administrator>"C:\Program Files\MariaDB 10.3\bin\mariabackup.exe" --prepare --target-dir="c:\users\mariabkp\mnt\external01\backup"\
C:\Program Files\MariaDB 10.3\bin\mariabackup.exe based on MariaDB server 10.3.31-MariaDB Win64 (AMD64)
[00] 2021-11-22 16:26:29 cd to c:\users\mariabkp\mnt\external01\backup\
[00] 2021-11-22 16:26:29 open files limit requested 0, set to 0
[00] 2021-11-22 16:26:29 This target seems to be not prepared yet.
[00] 2021-11-22 16:26:29 mariabackup: using the following InnoDB configuration for recovery:
[00] 2021-11-22 16:26:29 innodb_data_home_dir = .
[00] 2021-11-22 16:26:29 innodb_data_file_path = ibdata1:12M:autoextend
[00] 2021-11-22 16:26:29 innodb_log_group_home_dir = .
[00] 2021-11-22 16:26:29 Starting InnoDB instance for recovery.
[00] 2021-11-22 16:26:29 mariabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
2021-11-22 16:26:29 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2021-11-22 16:26:29 0 [Note] InnoDB: Uses event mutexes
2021-11-22 16:26:29 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2021-11-22 16:26:29 0 [Note] InnoDB: Number of pools: 1
2021-11-22 16:26:29 0 [Note] InnoDB: Using SSE2 crc32 instructions
2021-11-22 16:26:29 0 [Note] InnoDB: Initializing buffer pool, total size = 100M, instances = 1, chunk size = 100M
2021-11-22 16:26:29 0 [Note] InnoDB: Completed initialization of buffer pool
2021-11-22 16:26:29 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=7032026737757
2021-11-22 16:26:29 0 [Note] InnoDB: Starting final batch to recover 68 pages from redo log.
[00] 2021-11-22 16:26:30 Last binlog file , position 0
[00] 2021-11-22 16:26:31 completed OK!
C:\Users\Administrator>rem Erase the Database files
C:\Users\Administrator>del /q "C:\Program Files\MariaDB 10.3\data\*.*"
\Program was unexpected at this time.
C:\Users\Administrator>FOR /D \Program Files\MariaDB 10.3\data\*") DO rmdir "p" /s /q
C:\Users\Administrator>
My questions:
-
Why the message
\Program was unexpected at this time.
when I try todel /q "C:\Program Files\MariaDB 10.3\data\*.*"
-
Why the original instruction
FOR /D %p IN ("c:\Program Files\MariaDB 10.3\data\*") DO rmdir "%p" /s /q
is echoed asFOR /D \Program Files\MariaDB 10.3\data\*") DO rmdir "p" /s /q
? It SEEMS (but I'm not sure) it might be something related to the "%p" variable. -
Why the remaining instructions (import backup, restore my.ini and start MariaDB) are not executed?
I have to admit, I am a rookie in Windows Scripting. And I consider unnecessary to install the full Python3 for only one script!
Update: I tried to "Erase the Database files" before the "Prepare the Backup for Restoration", but I got the same result, that is it stops on the "FOR" instruction.
Solution 1:
The output is confusing, indeed, because it's showing the output of FOR before the echo of FOR, so it looks like the line above it is failing, but it's not.
The fix is easy, just double-% your variable, like so...
FOR /D %%p IN ("c:\Program Files\MariaDB 10.3\data\*") DO rmdir "%%p" /s /q
I can tell you even seasoned batch-file scripters from back in the day would pull their hair out over this stuff. I'd personally would have automated this in PowerShell since it's out-of-the-box for all Windows installs these days. Python is great too, when it's there. :-)