php - 如何每天从另一台服务器自动加载数据

标签 php mysql linux ssh

<分区>

您好,我需要创建一些自动连接到使用 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 = 'testemail@yahoo.co.uk';
$subject = 'the subject';
$message = 'hello';
$headers = "From: The Sender Name <senderEmail@yahoo.co.uk>\r\n";
$headers .= "Reply-To: senderEmail@yahoo.coom\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';
     }

最佳答案

绝对不会使用 PHP。绝对不是自制的。有一个内置的机制。它叫做replication经过超过 15 年的尝试和测试,并被用于数以千计的安装。

Replication enables data from one MySQL database server (the master) to be copied to one or more MySQL database servers (the slaves). Replication is asynchronous by default; slaves do not need to be connected permanently to receive updates from the master. Depending on the configuration, you can replicate all databases, selected databases, or even selected tables within a database.

在 PHP 中执行此操作意味着每天或每小时转储整个数据库,这意味着在进行转储期间站点没有响应。然后,您必须通过 HTTP 传输整个数据库。

最后但同样重要的是,您的 PHP 方法不允许连续归档。如果您每天归档一次,如果系统在最后一次备份后 23:50 小时出现故障,会发生什么情况?

关于php - 如何每天从另一台服务器自动加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38655929/

相关文章:

php - 如何防止PHP变量是数组或对象?

mysql - 该操作系统不支持指定的区域设置。 [LCID=-1]

MySql查询以查找具有条件的同一列之间的差异

mysql - 在连接表上删除时设置为空

linux - 使用 PuTTY 连接后无法使用 Perl 脚本连接到 Linux Oracle 数据库

linux - 在 shell 脚本中找不到 Flutter 命令

linux - 使用 netfilter 队列时包头的格式是什么?

php - 大量轻量级连接的 Apache2 优化技巧

php - SESSION中应该保存什么,不应该保存什么?

php - 在 PHP 中包含 XML 编码