2013-10-14 66 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; 
} 
相關問題