MySQL Database Synchronization between two servers

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

MySQL Database Synchronization between two servers

Postby Web Guru on July 21st, 2008, 4:02 pm

Do you ever need to keep update a database on a server from another website/server. The following script allows you to mirror the database from 2nd server to the first server. You can set this script as a crown job to update your database after a specific interval of time say every night, etc.

In the below code, just update the database connection information of both server and done :)

Also make sure you have added the server IP Address where this script will be ruuning on the remote server MySQL access list so that this script will be allowed to connect with that mysql database.
Code: Select all
<?php
    /////////////////////////////////////  Database Synchronization ////////////////////////////////////
   @ini_set("max_execution_time", 0);
   @ini_set("mysql.connect_timeout", -1);

   //  Remote Server Db Info
   $remote_host = "www.server.com";
   $remote_user = "server_db_user_name";
   $remote_pass = "server_db_password";
   $remote_db   = "server_db_name";

   //  Client Server Db Info
   $client_host = "localhost"; // mostly where this script will be running.
   $client_user = "db_user_name";
   $client_pass = "db_password";
   $client_db   = "db_name";

   $error = false;


   // creating client db connection
   $client_con = @mysql_connect($client_host, $client_user, $client_pass);

   if (!$client_con)
   {
      echo "Error: Unable to connect to the Client database Server.";
      exit( );
   }

   if (!@mysql_select_db($client_db, $client_con))
   {
      echo "Error: Unable to locate the Client Database.";
      exit( );
   }

   // creating remote db connection
   $remote_con = @mysql_connect($remote_host, $remote_user, $remote_pass);

   if (!$remote_con)
   {
      echo "Error: Unable to connect to the Remote database Server.";
      exit( );
   }

   if (!@mysql_select_db($remote_db, $remote_con))
   {
      echo "Error: Unable to locate the Remote Database.";
      exit( );
   }

   // starting transaction
   @mysql_query("BEGIN", $client_con);

   // getting remote server tables list
   $tables_sql = "SHOW TABLES FROM $remote_db";
   $tables_rs  = @mysql_query($tables_sql, $remote_con);

   if (!$tables_rs)
      $error = true;

   if ($error == false)
   {
      $tables_count = @mysql_num_rows($tables_rs);

      for ($i = 0; $i < $tables_count; $i ++)
      {
         $table_name = @mysql_result($tables_rs, $i);

         @mysql_select_db($remote_db, $remote_con);

         $table_struct_sql = "SHOW CREATE TABLE $table_name";
         $table_struct_rs  = @mysql_query($table_struct_sql, $remote_con);

         if (!$table_struct_rs)
         {
            print $table_struct_sql."<br><br>".@mysql_error( )."<br><br>";
            $error = true;
            break;
         }

         $table_struct = @mysql_result($table_struct_rs, 0, 1);

         @mysql_select_db($client_db, $client_con);

         // Droping Table if exists
            $table_sql = "DROP TABLE $table_name";
         @mysql_query($table_sql, $client_con);


         if (!@mysql_query($table_struct, $client_con))
         {
            print $table_struct."<br><br>".@mysql_error( )."<br><br>";
            $error = true;
            break;
         }

         @mysql_select_db($remote_db, $remote_con);

         // Table Data
            $records_sql = "SELECT * FROM $table_name";
         $records_rs  = @mysql_query($records_sql, $remote_con);

         if (!$records_rs)
         {
            print $records_sql."<br><br>".@mysql_error( )."<br><br>";
            $error = true;
            break;
         }

         $records_count = @mysql_num_rows($records_rs);
         $fields_count  = @mysql_num_fields($records_rs);

         for ($j = 0; $j < $records_count; $j ++)
         {
            $record_sql = "INSERT INTO $table_name VALUES (";

            // getting field values
            for ($k = 0; $k < $fields_count; $k ++)
            {
               $type  = @mysql_field_type($records_rs, $k);
               $value = @mysql_result($records_rs, $j, $k);

               if (!isset($value))
                  $record_sql .= 'NULL';

               else
                  $record_sql .= ("'".mysql_real_escape_string($value)."'");

               if ($k < ($fields_count - 1))
                  $record_sql .= ', ';
            }

            $record_sql .= ");\n";

            @mysql_select_db($client_db, $client_con);

            if (!@mysql_query($record_sql, $client_con))
            {
               print $record_sql."<br><br>".@mysql_error( )."<br><br>";
               $error = true;
               break;
            }
         }
      }
   }

   // commiting the transaction
   if ($error == false)
   {
      @mysql_query("COMMIT", $client_con);
      echo "<br>##### Database updated successfully.";
   }

   // discarding the transactions
   else
   {
      @mysql_query("ROLLBACK", $client_con);
      echo "<br>##### Database updating failed due to some error.";
   }

   @mysql_close($remote_con);
   @mysql_close($client_con);
?>
User avatar
Web Guru
 
Posts: 68
Joined: March 24th, 2008, 7:59 am
Location: Lahore, Pakistan

Return to PHP / MySQL / XML

Who is online

Users browsing this forum: No registered users and 0 guests

cron