我有一些批量编写的代码。该脚本应该使用数据库的SQLDump,然后使用7zip将其压缩,然后将压缩文件存档到历史文件夹中。 下面是我的整个脚本,提供了上下文,并了解了整体情况,但我的问题是第二个FOR循环,这就是这里:
FOR /F "usebackq" %%A IN ('%encryptedFile%') DO set encryptedFileSize=%%~zA
:: Checks the zipped back up is greater than 0 (No failure on zipping)
IF %encryptedFileSize% GTR %maxbytesize% (
%date:~6,4%%date:~3,2%%date:~0,2%-%time:~0,2%-%time:~3,2% %FileName% Encrypted Successfully >> Backup.txt
::Move the zip file
MOVE %FileName%.7z BackupHistory
::DEL %FileName% - do i need this?
::Deletes error log if the backup is successful; Error log will still be there if backup failed
DEL %errorLog%
) ELSE (
::Logs that zip & encryption failed
@ECHO %date:~6,4%%date:~3,2%%date:~0,2%-%time:~0,2%-%time:~3,2% %FileName% Encryption Failed 2> ZIPFailure.txt
)
整个脚本将运行OK(例如,使用转储,压缩它),但是当它到达下一位时,检查压缩文件大于50KB,然后移动文件并删除原始备份就失败了。它在屏幕上返回的错误是(从CMD提示中复制和粘贴,如下所示):
"设置encryptedFileSize = 50此时是意外的。"
收到错误讯息:
D:\BackupSchedule\BackupScripts\TestScripts>FOR /F "usebackq" %A IN ('"DailyBack
up.20161230-15-42.7z"') DO set encryptedFileSize=%~zA
D:\BackupSchedule\BackupScripts\TestScripts>set encryptedFileSize=
50 was unexpected at this time.
D:\BackupSchedule\BackupScripts\TestScripts> IF GTR 50 (
D:\BackupSchedule\BackupScripts\TestScripts>
完整脚本:
@ECHO onD:
::Set variables for the backup
SET WorkingDirectory=D:\BackupSchedule\BackupScripts\TestScripts
SET BackupFile="DailyBackup.%date:~6,4%%date:~3,2%%date:~0,2%-%time:~0,2%-%time:~3,2%.bak"
SET encryptedFile="DailyBackup.%date:~6,4%%date:~3,2%%date:~0,2%-%time:~0,2%-%time:~3,2%.7z"
SET errorLog="D:\BackupSchedule\BackupScripts\TestScripts\Backup_error_%date:~6,4%%date:~3,2%%date:~0,2%.txt"
SET maxbytesize=50
:: Just writing a line in the error log to break up the days
@ECHO --------------------------------------------------------- >> Backup.txt
cd %WorkingDirectory%
::Create Backup History Folder
IF NOT EXIST %WorkingDirectory%\BackupHistory\ (
MKDIR %WorkingDirectory%\BackupHistory\
)
::Take backup
mysqldump.exe --user=S-BackUp --password=SEXOr2 --host=HostIP --port=ChosenPort --result-file=%BackupFile% --default-character-set=utf8 --single-transaction=TRUE --databases "queue_depth" 2> Backup_error_%date:~6,4%%date:~3,2%%date:~0,2%.txt
FOR /F "usebackq" %%A IN ('%BackupFile%') DO set size=%%~zA
:: Checks the Backup isn't 0KB
IF %size% GTR %maxbytesize% (
:: Prints the size of backup to the log
@ECHO %date:~6,4%%date:~3,2%%date:~0,2%-%time:~0,2%-%time:~3,2% Backup is ^> %maxbytesize% bytes >> Backup.txt
@ECHO %date:~6,4%%date:~3,2%%date:~0,2%-%time:~0,2%-%time:~3,2% BackUp %BackupFile% created successfully >> Backup.txt
::Zip & Encrypt the backup
7za.exe a %BackupFile%.7z %BackupFile% -PmyPassword 2>> Backup_error_%date:~6,4%%date:~3,2%%date:~0,2%.txt
) ELSE (
:: Script will stop here if Backup failed
:: Prints the size of backup to the log
@ECHO %date:~6,4%%date:~3,2%%date:~0,2%-%time:~0,2%-%time:~3,2% Backup is ^< %maxbytesize% bytes >> Backup.txt
@ECHO %date:~6,4%%date:~3,2%%date:~0,2%-%time:~0,2%-%time:~3,2% BackUp %BackupFile% failed >> Backup.txt
)
FOR /F "usebackq" %%A IN ('%encryptedFile%') DO set encryptedFileSize=%%~zA
:: Checks the zipped back up is greater than 0 (No failure on zipping)
IF %encryptedFileSize% GTR %maxbytesize% (
%date:~6,4%%date:~3,2%%date:~0,2%-%time:~0,2%-%time:~3,2% %FileName% Encrypted Successfully >> Backup.txt
::Move the zip file
MOVE %FileName%.7z BackupHistory
::DEL %FileName% - do i need this?
::Deletes error log if the backup is successful; Error log will still be there if backup failed
DEL %errorLog%
) ELSE (
::Logs that zip & encryption failed
@ECHO %date:~6,4%%date:~3,2%%date:~0,2%-%time:~0,2%-%time:~3,2% %FileName% Encryption Failed 2> ZIPFailure.txt
)
如果您需要更多有关此
的详细信息,请与我们联系