2

这是我的目标:我有一台包含所有源代码的 Windows XP PC 和一个开发数据库。我们称之为“pc.dev.XP”。我有一台运行 Linux 的目标计算机。我们称之为“pc.demo.Linux”。这是我在“pc.dev.XP”上所做的(只是为了让你了解上下文):

  • 安装了所有 cygwin 的东西
  • 创建了一个有效的 rsa 密钥并将其放在 dest 备份计算机上,这样 ssh 就不会要求输入密码
  • rsync 这样工作得很好

如果我尝试通过命令行在“pc.dev.XP”上执行此操作:

cd \cygwin\bin
ssh Fred@pc.demo.Linux "cd /var/www && ls -al"

无需询问密码即可完美运行现在这是我想在“pc.dev.XP”上执行的操作:

  • 启动一个提取开发的 php 脚本。数据库到一个sql文件
  • 压缩这个文件
  • 通过 ftp 将其传输到“pc.demo.Linux”
  • 登录到“pc.demo.Linux”并执行“unzip then mysql -e”source unzipped file”

如果我手动在“pc.dev.XP”上运行:

putty -load "myconf" -l Fred -pw XXX -m script.file.that.unzip.and.integrates.sql

这完美地工作。同样适用于:

cd \cygwin\bin
ssh Fred@dest "cd /var/www && ls -al"

如果我尝试在 php 中执行()(安装在“pc.dev.XP”上的 wamp),它们会挂起。我很确定这是因为用户是“SYSTEM”而不是“Fred”,并且 putty 或 ssh 要求输入密码,但也许我错了。

无论如何,我正在寻找一种方法来自动化我所描述的这 4 个任务,但我被卡住了,因为 exec() 挂起。safe_exec_mode 或 safe_exec_dir 指令没有问题,它们在开发机器上被禁用,因此如果我尝试一些基本的东西,比如 exec("dir") exec() 工作得很好

知道我能做什么/检查/纠正吗?

4

3 回答 3

1

我不确定这是否是您需要的,但我通常使用这样的结构来跨机器同步数据库:

php extractFromDb.php | ssh user@remote.com "mysql remoteDatabaseName"

这将在本地执行 PHP 脚本,并将脚本通过 SSH 直接打印出来的 SQL 命令通过管道传输到远程 mysql 进程中,该进程在远程数据库中执行它们。

如果您需要压缩,您可以使用 SSH 的 -C 开关,或者集成使用您选择的压缩程序,如下所示:

php extractFromDb.php | gzip -9 | ssh user@remote.com "gunzip | mysql remoteDatabaseName"
于 2009-10-30T14:24:58.507 回答
0

You want to do this from PHP running under apache, as in I go to http://myWebserver.com/crazyScript.php and all this happens? Or you just want to write your scripts in PHP and invoke them via cmd line?

If you want the first solution, try running your apache/iss under a different user that has credentials to perform all those tasks.

"if I run on the development PC manually this works perfectly.". Why not do it like that? When you run that script, I assume you're connecting to the local SSH server on the dev machine. When you do this, you are using the credentials Fred, so everything works. When you run the PHP script, you are right that it is probably running as SYSTEM.

Try either changing the user that apache is running as or use php to connect to the local ssh thereby using alternate credentials.

于 2009-10-30T13:16:42.763 回答
0

这就是我所做的:一个批处理文件:

  1. 通过“php.exe my_extract_then_compress_then_ftp.php”调用 php 文件
  2. 调用 rsync 同步源文件夹
  3. 调用 putty -l user -pw password -m file_with_ssh_commands_to_execute

它就像一个魅力。

于 2009-11-04T15:56:31.553 回答