2013-10-14 154 views
1

我正在创建一个应用程序来同步我的本地mysql数据库到远程mysql数据库,所以我生成一个转储文件,通过sFTP发送它,然后在服务器。ssh或cURL发送数据到远程服务器

但是,我知道还有其他可用的方法,如cURL。我喜欢将数据发送到远程服务器,并在服务器接受时(TRUE)在服务器上执行它,但我对这方面使用cURL的相关安全问题知之甚少。任何人都可以提供关于cURL解决方案的建议,否则建议使用其他方法?

+0

也无论我们的答案的帮助: 你会使用ssh2_auth_pubkey_file认证不是更好吗? **您是否找到解决方案?** – Jimbo

回答

3

首先,而不是生成的转储文件,你应该使用file_get_contents()从文件中读取数据,fread()

商店的这一个变量的结果,然后发送原始数据通过管道(通过cURL,如果你愿意的话),并且让服务器端的代码生成转储文件。

使用cURL,您可以指定一个专用证书文件进行身份验证 - 因此其安全性与通过ssh使用证书相同 - 这不是您需要担心的事情。

您可以通过以下卷曲选项示例将PEM文件:

curl_setopt($ch, CURLOPT_SSLCERT, $pemfile); 
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); 
curl_setopt($ch, CURLOPT_SSLKEY, $keyfile); 

有对上述所有在互联网上的教程。使用私钥身份验证,并对安全问题进行排序。只要确保你没有设置CURLOPT_SSL_VERIFYPEERfalse(你现在不想要任何MiTM(中间男人)攻击,你是不是)。

1

我使用Curl来做到这一点。

我有一个导出脚本生成json或xml(取决于什么样的心情我比什么都重要)然后我发布这个文件到远程服务器和远程服务器使用ignore_user_abort,这样即使处理可以继续父内部系统脚本超时/完成任何操作。

像本地Web服务器和远程Web服务器之间的同步更改为2.6gb表的魅力。

+0

您如何看待安全性?我们需要一些额外的东西吗? – underscore

+0

它只是一个标准的http post,但如果你把https放在远程服务器上,那么你会得到端到端的加密。你也可以实现一个你自己的简单加密,这样在2个页面之间发布加密数据,而不仅仅是json/xml数据。但是实际上,即使是大文件也只有几百K大小,因为它的唯一纯文本就是实际发布时间在开放时间与正常的网页负载时间非常相似,除非有人在任何一端主动嗅探您的服务器网络,否则您没有安全问题如果网络正在被嗅探,那么您遇到了更大的问题 – Dave

0

我使用phpseclib,一个纯粹的PHP SFTP实现来做这样的事情。例如。

<?php 
include('Net/SFTP.php'); 

$sftp = new Net_SFTP('www.domain.tld'); 
if (!$sftp->login('username', 'password')) { 
    exit('Login Failed'); 
} 

$sftp->put('filename.remote', 'filename.local', NET_SFTP_LOCAL_FILE); 
?> 

它有超过libssh2许多优点,包括速度和可移植性:

http://phpseclib.sourceforge.net/ssh/compare.html

0

这是使用SSH2/libssh碱性溶液。

此代码假定您已经有一种创建数据库转储的方法,并且只会在当前服务器上读取它,目的是将其加载到远程服务器上。

它通过SSH连接到远程主机,将您的sql_dump写入远程服务器上的文件,然后执行命令将其加载到数据库中。

我不建议存储用于连接的用户名/密码,这只是测试代码的简单方法。 http://php.net/manual/en/function.ssh2-auth-pubkey-file.php

// remote host authentication details. hostname/ip, user, pass 
$host = 'REMOTE_HOST'; 
$user = 'REMOTE_USER'; 
$pass = 'REMOTE_PASS'; 

// check if we have ssh2 installed first 
if (function_exists("ssh2_connect")) { 

    //connect to remote host 
    $connection = ssh2_connect($host, 22); 

    // if connection successful, proceed 
    if ($connection) { 

     // authenticate on remote connection 
     $auth = ssh2_auth_password($connection, $user, $pass); 

     // if we have authenticated, proceed with remote commands 
     if ($auth) { 

      // load our dump file to a string 
      $sql_str = file_get_contents('dump_file.sql'); 
      // bash command to cat our dump string to a file 
      $write_remote_file_command = "cat <<'EOF' > /home/tmp_file.sql \n$sql_str \nEOF"; 
      // call our execute ssh function to execute above command 
      executeSSHCommand($connection, $write_remote_file_command); 

      // command to load our temp dump file into the database 
      // - you may need to add additional commands to drop the existing db, etc 
      $remote_load_command = "mysql -Uroot -p -h localhost database_name < /home/tmp_file.sql"; 
      // remotely execute load commands 
      executeSSHCommand($connection, $remote_load_command); 
     } 
    } 
} 

// basic function to execute remote shell commands via our authenticated $connection 
function executeSSHCommand($connection, $command) { 

    $output = array(); 
    $ssh_data = ""; 

    $stream = ssh2_exec($connection, $command); 

    if ($stream) { 

     stream_set_blocking($stream, true); 

     while ($buffer = fread($stream, 65536)) { 

      $ssh_data .= $buffer; 
     } 

     fclose($stream); 

     $output = explode(PHP_EOL, $ssh_data); 

    } 

    return $output; 
} 
相关问题