我尝试通过调用 wsh jscript 文件来转储 mysql 数据库,但它不起作用。
我有这段代码,用 git shell 调用,它工作得很好:
# If something fails, exit with status other than 0
set -e
# select dump directory
cd "$(git rev-parse --show-toplevel)"
# first, remove our original schema
rm -f "WebShop\DataBase\backup.sql"
# generate a new schema
exec "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" --skip-comments -u root --password=root webshopdb |sed 's$),($),\n($g' > "WebShop\DataBase\backup.sql"
我在 WSH 中尝试了几乎相同的代码,但它仅返回转储文件的 header ,并且不创建文件。我不知道出了什么问题,或者如何调试代码......:S
var shellObj = WScript.CreateObject('WScript.Shell');
var exec = shellObj.Exec(
'"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump.exe"'+
" --skip-comments -u root --password=root webshopdb |sed 's$),($),\\n($g' > " +
'"D:\\creation\\software developer\\projects\\webshop-refactoring-project\\document root\\WebShop\\DataBase\\backup.sql"'
);
WScript.Echo(exec.StdOut.ReadAll());
我也尝试过使用bat文件和cmd文件,但它们无法处理路径中的空间。
有人可以帮忙吗?
(对我来说,从 git 代码中创建一个可执行文件就足够了,或者让 wsh 工作......完美的解决方案是如果我可以从 netbeans 调用转储,但在生活中没有什么比这更理想的了。 ..:D)
最佳答案
我是通过文件关联做到的。
我创建了一个 git.bat 文件:
if not exist %1 exit
set bash=C:\Program Files (x86)\Git\bin\bash.exe
"%bash%" --login -i -c "exec "%1""
并将其关联到 .hook 文件。
之后我创建了一个测试 dump.hook 文件:
#!/bin/sh
cd "D:/creation/software developer/projects/webshop-refactoring-project/document root/WebShop";
cd "$(git rev-parse --show-toplevel)"
rm -f "WebShop/DataBase/backup.sql"
exec "C:/Program Files/MySQL/MySQL Server 5.5/bin/mysqldump.exe" --skip-comments -u root --password=root webshopdb |sed 's$),($),\n($g' > "WebShop/DataBase/backup.sql"
exit
而且效果非常好。
三天后我收到了!呜呼! :D
注意:*Windows命令提示符通常会出现路径名中空格和特殊字符的问题,因此使用git的模拟linux比尝试修复它要容易得多。也可以将 .hook 文件源到预提交 git hook 中,这样它就可以在每次提交时自动转储数据库模式...(也许 git add 不适用于这些文件,我还没有找到自动转储并提交解决方案: git pre-commit + mysqldump: cannot find path, not existing command ) *
关于windows - Windows 7 上可执行文件的 MySQLDump,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9254186/