用PHP传输文件

时间:2014-04-25 10:10:47

标签: php linux rsync scp

我必须将创建的sql文件从服务器a传输到服务器b。目前我尝试过没有成功:

// Some SQL executions to fill and update the tables
( .. )

// Filename pattern
$fileName = "dump_".date("Y-m-d-H_i_s").".sql";

// Dump the tables and saves the output in dump directory 
$dump = "mysqldump -u$USER -p$PASS $DB ".$this->dumpTables." > " . $this->path."dumps/". $fileName;

// Transfer the lastest sql file to the origin
$command = "scp " . $this->path."dbbackup/" . $fileName . " user@xxx.xxx.xxx.xxx:/PATH/dumps" . $fileName;

system($command, $retvalOrigin);
if ($retvalOrigin != 0) {
        fwrite($this->logFileInstance, $this->date() . "Something went wrong! \r\n");
        fwrite($this->logFileInstance, $this->date() . "$command! \r\n");
} 

问题是什么?

如果我手动执行命令,一切都很顺利。所以没有权限问题。我的错误日志中没有意外的日志条目。如果我复制命令没有任何vars并把它放入一个新的PHP文件,并调用它,再没有错误.. 起初我认为scp讨厌我,这就是为什么我用“rsync -ave”替换“scp”。这没有给我任何错误,但不会将文件复制到源服务器(再次没有错误日志!)。

scp - > $ retvalOrigin = 1,原始服务器上没有文件,没有错误日志

rsync - > $ retvalOrigin = 0,原始服务器上没有文件,没有错误日志

那有什么意义呢?

编辑:

路径是绝对的, 用于执行用户的SSH KEY和www-data - 不再需要登录, $ retVal仅包含1/0, 和NO不会用于那个cron

1 个答案:

答案 0 :(得分:1)

确保使用scp命令的用户(很可能是www-data)具有使用scp而不使用身份验证的权限,而不仅仅是您的系统用户。

还要确保使用绝对路径。

为了更好地报告错误,请使用' exec'用于存储错误消息。 '系统'将直接输出错误,并且当您在cron中使用它时最有可能丢失。

exec($command, $commandOutput, $retvalOrigin);

此外,如果命令需要有效的shell,则可以尝试使用shell_exec。