2016-07-29 10 views
0

こんにちは私は、PHPライブラリを使用して別のサーバーに接続し、mysqlデータベースにデータをロードする必要がある自動作成する必要があります毎日アップロードされます。確かに毎日別のサーバーからデータを自動的にロードするには

<?php 
include 'core/init.php'; 
include 'includes/overall/header.php'; 

//connection to linux server 
$conn = ssh2_connect('xxx.xxx.xx.xxx', 22); 
$destinationPath = '/path/to/destination/path/'; 
$localPath = 'C:\path\to\local\path\'; 

//checks if the connection is successful or not 
if(ssh2_auth_password($conn, 'username', 'password')){ 
echo '<script type="text/javascript">alert("Authentication was successful"); </script>'; //javascript pop up when successful 
    }else{ 
    die("Authentication failed"); 
} 

if(ssh2_scp_recv($conn, $destinationPath, $localPath)){ 
echo '<h2>Todays file recieved</h2>'; //if file was recieved from server to local echo todays file recieved, putting the file in localpath 
}else{ //if the file was not uploaded send an email for radar file too be uploaded 

$to = '[email protected]'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = "From: The Sender Name <[email protected]>\r\n"; 
$headers .= "Reply-To: [email protected]\r\n"; 
$headers .= "Content-type: text/html\r\n"; 
mail($to, $subject, $message, $headers); 

}

$string = file_get_contents('http://localhost/Prototype/core/edit.txt', 'r');//get contents of file from web used to read the file 
$myFile = 'C:wampwwwPrototypecoreedit.txt';//file directory 
$fh = fopen($myFile, 'w') or die("Could not open: " .mysql_error());//open the file 
fwrite($fh, $string); 
fclose($fh); 
$result = mysql_query("LOAD DATA LOCAL INFILE '$myFile'". "INTO TABLE `restartdata` FIELDS TERMINATED BY ',' "); 
    if (!$result) { 
    die("Could not load." . mysql_error()); 
    }else{ 
echo 'data loaded in the database'; 
    } 
+0

質問は何ですか? – Epodax

+0

基本的にファイルは毎日Linuxサーバーにアップロードされ、毎日フェッチしてデータベースに格納する必要があります – Ben

+1

** deprecatedを使用して停止し、PHP7で削除された** mysql_ *関数を使用しないでください。 PDOに移行し、Prepared、Parameterized Queriesを使用し始めます。 –

答えて

2

ないPHPを使用して:ここでの問題はほとんどが、私は私がデータベースにファイルを毎日アップロードし続けるんですか

はコードです。そして確かに家で作られたものは何もない。そのためのメカニズムが組み込まれています。 replicationと呼ばれ、15年以上にわたってテストされており、数千のインストールで使用されています。

レプリケーションでは、1台のMySQLデータベースサーバー(マスター) のデータを1つ以上のMySQLデータベースサーバー(スレーブ)にコピーできます。 レプリケーションはデフォルトでは非同期です。スレーブはマスターから更新を受け取るために永久に接続されている である必要はありません。 構成に応じて、すべてのデータベース、選択された データベース、またはデータベース内の選択されたテーブルを複製することができます。

PHPでこれを行うと、データベース全体が毎日または毎時ダンプされ、その間にダンプが行われている間にサイトが応答しなくなることを意味します。次に、HTTP経由でデータベース全体を転送する必要があります。

PHPのアプローチでは、継続的なアーカイブはできません。 1日に1回アーカイブする場合、最後のバックアップが作成されてから23:50時間が経過してもシステムに障害が発生した場合はどうなりますか?

+0

あまりにも遅すぎるので、Webアプリケーションなので、PHPであれば最高ですし、これだけの技術的なエラーのほとんどをやってしまいました。 – Ben

+0

私はこれをタイプすると何千ものWebアプリケーションが複製されていると言いました。 – e4c5

+0

よく私は卒業生なので経験はありませんが、あなたはPHPでそれを行うことができます – Ben

関連する問題