简体   繁体   English

如何使用MySQL与PHP将数据库从联机服务器备份到脱机服务器

[英]How to get database backup from online server to offline server using PHP with MySQL

//Assume Server 1
$conn = mysql_connect("127.0.0.1","root","");

//Assume Server 2
$conn1 = mysql_connect("127.0.0.1","root","");

//Server 1 database
mysql_select_db("db1",$conn);

//Server 2 database
mysql_select_db("db2",$conn1);

//Count number of rows from server 1 -> database -> table (tbl1)
$cnt_rw=mysql_query("select count(*) from db1.tbl1");
$cnt_n=mysql_fetch_array($cnt_rw);  

//Fetch and update row one by one
for($i=0;$i<($cnt_n['count(*)']);$i++)
{
    $one_row=mysql_query("select * from db1.tbl1 limit $i,1");
    while($one_val=mysql_fetch_array($one_row))
    {
        $one=$one_val['one'];
        $two=$one_val['two'];
        $three=$one_val['three'];
    }

    //Already exist means update else insert so am using replace query
    mysql_query("REPLACE INTO db2.tbl2(one,two,three)values('".$one."','".$two."','".$three."')");
    $one=$two=$three='';
}   

This PHP code is working properly but take a long loading time. 此PHP代码正常运行,但加载时间较长。 So I want a simple query or PHP/MySQL code or any idea to get backups from the online server to the offline server. 因此,我想要一个简单的查询或PHP / MySQL代码或任何想法将备份从在线服务器备份到离线服务器。

Online and offline database fields are same. 联机和脱机数据库字段相同。

Since using PHP might not be the best idea if you have large amount of data you could also use command line tools like mysqldump to get a copy of the data and put it on your local machine with the mysql cli command. 因为如果您有大量数据,使用PHP可能不是最好的主意,所以您还可以使用mysqldump之类的命令行工具来获取数据的副本,并使用mysql cli命令将其放在本地计算机上。

Another option might be special tools like sqlyog which help you migrating data from one server to another. 另一个选择可能是特殊的工具,例如sqlyog ,可帮助您将数据从一台服务器迁移到另一台服务器。

常见的管理解决方案是使用cron脚本,该脚本整个db 转储到文件(理想情况下压缩到bz2),然后通过ftp,sftp,rsync将其上传到备份服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM