Windows

Windows 2019 .BAT 腳本中的奇怪行為

  • November 22, 2021

我必須將備份從 Linux MariaDB 恢復到 Windows MariaDB,PowerBI 網關將在其中導入其數據。“mariabackup”是 MariaDB 的物理備份工具。但要恢復它,目標文件夾 ( %programfiles%\MariaDB 10.3\data\) 必須為空。

由於rmdir /S /Q "%programfiles%\MariaDB 10.3\data\將刪除“數據”目錄(我不想要的!!!),我一直在努力避免在以下腳本(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

我工作得很好,直到我必須刪除要恢復的文件,正如您在前面看到的那樣:

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>

我的問題:

  1. 為什麼\Program was unexpected at this time.當我嘗試時出現消息del /q "C:\Program Files\MariaDB 10.3\data\*.*"
  2. 為什麼原始指令FOR /D %p IN ("c:\Program Files\MariaDB 10.3\data\*") DO rmdir "%p" /s /q回顯為FOR /D \Program Files\MariaDB 10.3\data\*") DO rmdir "p" /s /q?看起來(但我不確定)它可能與“%p”變數有關。
  3. 為什麼剩下的指令(導入備份、恢復 my.ini 和啟動 MariaDB)沒有執行?

我不得不承認,我是 Windows 腳本的新手。而且我認為沒有必要為一個腳本安裝完整的 Python3!

更新:我試圖在“準備備份以進行恢復”之前“擦除數據庫文件”,但我得到了相同的結果,即它在“FOR”指令處停止。

輸出確實令人困惑,因為它在 FOR 的回顯之前顯示了 FOR 的輸出,所以看起來它上面的行失敗了,但事實並非如此。

修復很簡單,只需將變數加倍,就像這樣……

FOR /D %%p IN ("c:\Program Files\MariaDB 10.3\data\*") DO rmdir "%%p" /s /q

我可以告訴你,即使是過去經驗豐富的批處理文件腳本編寫者也會對這些東西大發雷霆。我個人會在 PowerShell 中自動執行此操作,因為這些天它對於所有 Windows 安裝都是開箱即用的。Python 也很棒,當它出現時。:-)

引用自:https://serverfault.com/questions/1084285