如何每天从另一台服务器自动加载数据 [英] How do I load data automatically from another server everyday

查看:40
本文介绍了如何每天从另一台服务器自动加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一些自动连接到使用 php 库的另一台服务器的东西,然后我需要将数据加载到 mysql 数据库中,只有第一个文件上传每天都会上传一个新文件.问题是我如何继续每天将文件上传到数据库我几乎就在那里

Hi I need to create something automatic that connects to another server using a php library and then I need to load the data in a mysql database only the first file uploads a new file is uploaded everyday. The problem is how do I continue uploading a file everyday to the database I am almost there

代码如下:

<?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 年的尝试和测试,它是已在数千个安装中使用.

Definitely not using PHP. And definitely nothing that's home made. There is a built in mechanism for that. It's called replication tried and tested over more than 15 years and it's being used on thousands of installations.

复制启用来自一台 MySQL 数据库服务器(主服务器)的数据复制到一个或多个 MySQL 数据库服务器(从属).默认情况下复制是异步的;奴隶不需要永久连接以接收来自主站的更新.取决于配置,您可以复制所有数据库,选择数据库,甚至是数据库中选定的表.

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 传输整个数据库.

To do this in PHP would mean the entire database is dumped daily or hourly during which means the site is unresponsive during the time the dump is being made. Then you have to transfer the whole database over HTTP.

最后但并非最不重要的一点是,您的 PHP 方法不允许连续存档.如果您每天归档一次,如果系统在上次备份后 23:50 小时出现故障会怎样?

Last but not least your PHP approach does not allow continous archiving. If you are archiving once a day, what happens if the system fails 23:50 hours after the last backup was made?

这篇关于如何每天从另一台服务器自动加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆