我制作了一个简单的脚本来备份我的数据库,现在我遇到了问题,我将每个服务器的变量存储在.txt
上,并且我试图将它们加载到脚本多次。
现在使用此配置它会覆盖配置,而不是为每个文件运行它。
有没有办法让脚本以文件xxx
执行,然后文件yyy
等...
for /F "tokens=*" %%a in (servers\xxx.txt servers\yyy.txt etc) do (
%%a
)
:: Backup folder
set backupfldr="C:\Backup\files\"
:: MySQL EXE Path
set mysqldumpexe="C:\Backup\mysqldump-5_6.exe"
:: zip
set zipper="C:\Backup\zip\7za.exe"
:: date
set datetimef=%date:~-4%%date:~3,2%%date:~0,2%
:: final
set bkupfilename=%datetimef%-%dbname%.sql
:: start
echo Backing up to file: %bkupfilename%
%mysqldumpexe% --set-gtid-purged=OFF -h %server% -u %dbuser% -p%dbpass%
%dbname% > %backupfldr%"%bkupfilename%"
.txt文件就像这样
set server=100.100.100.100
set dbname=nameofdatabase
set dbuser=userofdatabase
set dbpass=passofdatabase
答案 0 :(得分:0)
我没有验证您的逻辑,但是对于字符串列表(此处为文件名),请使用普通for
(不含/f
):
REM for every of those files call a subroutine:
for %%a in ("servers\xxx.txt" "servers\yyy.txt" etc) do call :process %%a
goto :eof
:process
copy "%~1" "%~dp0\temp.bat"
call "%~dp0\temp.bat"
:: Backup folder
set backupfldr="C:\Backup\files\"
:: MySQL EXE Path
set mysqldumpexe="C:\Backup\mysqldump-5_6.exe"
:: zip
set zipper="C:\Backup\zip\7za.exe"
:: date
set datetimef=%date:~-4%%date:~3,2%%date:~0,2%
:: final
set bkupfilename=%datetimef%-%dbname%.sql
:: start
echo Backing up to file: %bkupfilename%
%mysqldumpexe% --set-gtid-purged=OFF -h %server% -u %dbuser% -p%dbpass%
%dbname% > %backupfldr%"%bkupfilename%"
(给定,你的.txt文件看起来像:
set server=100.100.100.100
set dbname=nameofdatabase
set dbuser=userofdatabase
set dbpass=passofdatabase
)