douyi1939 2014-04-25 10:10
浏览 78

用PHP传输文件

i have to transfer a created sql file from server a to server b. Currently i tried following without success:

// 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! 
");
        fwrite($this->logFileInstance, $this->date() . "$command! 
");
} 

Whats the problem?

If i execute the command by hand, everything goes fine. So there is no permission problem. No unexpected log entries in my error log. If i copy the command without any vars and put this into a new php file, and call this, there is no error again.. At first i thougt scp hates me, thats why i replaced the "scp" with "rsync -ave". That gives me no error, but won't copy the file to the origin server (No error log again!).

scp -> $retvalOrigin = 1, no file on origin server, no error log

rsync -> $retvalOrigin = 0, no file on origin server, no error log

So what's the point?

Edit:

Path are ABSOLUTE, SSH KEY for the executing user and www-data - no login required anymore, $retVal includes ONLY 1 / 0, and NO in won't use for that a cron

  • 写回答

1条回答 默认 最新

  • dongtang6775 2014-04-25 10:23
    关注

    Make sure that the user that uses the scp command (most likely www-data) has the permissions to use scp without authentification and not only your system user.

    Also make sure that you use absolute paths.

    For better error reporting use 'exec' for storing the error messages. 'system' will output the error directly and will get lost most likely when you use it in a cron.

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

    Furthermore you can try shell_exec if the command needs a valid shell.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题