备份Sql数据库时Windows批处理文件错误

时间:2015-12-25 21:02:52

标签: mysql batch-file

错误发生在

之后
del "C:\backupforalldbs\tpsdatabase\bk_%backuptime%.sql"

这是源代码:

CLS 

SET backuptime=%DATE:~10,4%-%DATE:~7,2%-%DATE:~4,2%-%TIME:~0,2%-%TIME:~3,2%

echo %backuptime%

echo Running dump ...

set 7zip_path=

"C:\xampp\mysql\bin\mysqldump.exe" --host="localhost" --port="3306" --user="jakedean" --password="jakedean" -Q --result-file="C:\backupforalldbs\tpsdatabase\bk_%backuptime%.sql" jakedean

echo Zipping ...

"C:\Program Files\7-Zip\7z.exe" a -tzip     "C:\backupforalldbs\tpsdatabase\bk_%backuptime%.zip"         "C:\backupforalldbs\tpsdatabase\bk_%backuptime%.sql"

echo Deleting the SQL file ...

del "C:\backupforalldbs\tpsdatabase\bk_%backuptime%.sql"

timeout /t 5

echo Done!

@pause

这是执行...后的cmd提示符的打印屏幕。

Screenshot

我已尝试过更改文件名,缩短目录,更改非上限,但没有任何效果......:/

2 个答案:

答案 0 :(得分:0)

你的问题是%backuptime%变量正在生成斜杠,你应该找到一种方法来删除它们,因为windows不允许带有斜杠名字的文件。这就是你的命令。

enter image description here

不支持的字符列表:

enter image description here

在尝试删除目录之前,请检查目录中的文件名。

答案 1 :(得分:0)

我通过替换

来解决这个问题

SET backuptime=%DATE:~10,4%-%DATE:~7,2%-%DATE:~4,2%-%TIME:~0,2%-%TIME:~3,2%

SET backuptime=%TIME:~0,2%-%TIME:~3,2%

日期是在文件名中添加/不允许导致错误。日期也没有设置正确的数据,它保持设置01-02 ...

整个.bat文件现在看起来像这样

@echo off

CLS 

SET backuptime=%TIME:~0,2%-%TIME:~3,2%

echo %backuptime%

echo Running dump ...

set 7zip_path=

"C:\xampp\mysql\bin\mysqldump.exe" --host="localhost" --port="3306" --user="jakedean" --password="jakedean" -Q --result-file="C:\backupforalldbs\tpsdatabase\bk_%backuptime%.sql" jakedean

echo Zipping ...

"C:\Program Files\7-Zip\7z.exe" a -tzip "C:\backupforalldbs\tpsdatabase\bk_%backuptime%.zip" "C:\backupforalldbs\tpsdatabase\bk_%backuptime%.sql"

echo Deleting the SQL file ...

del "C:\backupforalldbs\tpsdatabase\bk_%backuptime%.sql"

timeout /t 5

echo Done!