简体   繁体   中英

syncronize two databases with php

I am struggling with backup my data base using PHP.I have tried this code but it's only generate an empty .sql file..

function backup_tables($host,$user,$pass,$name,$tables = '*')
{

    $link = mysql_connect($host,$user,$pass);
    mysql_select_db($name,$link);

    //get all of the tables
    if($tables == '*')
    {
        $tables = array();
        $result = mysql_query('SHOW TABLES');
        while($row = mysql_fetch_row($result))
        {
            $tables[] = $row[0];
        }
    }
    else
    {
        $tables = is_array($tables) ? $tables : explode(',',$tables);
    }

Having more code so we can see what exactly you're trying to do with it would be helpful.

However if your goal is just to backup or fully replicate a database doing it at the OS/mySQL level may be more reliable. Do you lack access at this level?

For non-realtime backups using mysqldump with a cronjob will allow you to output to .sql and import again. For realtime synchronization you can setup a replication server that will update both databases at the same time.

Keep in mind you could also trigger mysqldump from within php and avoid having to program this yourself.

A quick google gives me these starting articles:

backup with mysqldump: http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/

replication: https://www.digitalocean.com/community/articles/how-to-set-up-master-slave-replication-in-mysql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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