user1813962
user1813962

Reputation: 549

sync my localhost with my server by ftp

I want to sync my localhost (Windows) with my remote server in real time and automatically. So when I modify, create or delete a file this tool should update remote server automatically. This aplication must to keep both servers synchronized in real time. Please I really need your help. I tried FTPbox, but it doesn't update always, I need some better. I'm working on windows, but if exists some on linux is better.

Thanks

Upvotes: 2

Views: 1393

Answers (3)

doom
doom

Reputation: 3618

WinScp has a synchronization feature that does what you want.

For linux users, you can have a look here.

Upvotes: 1

Josiah
Josiah

Reputation: 148

I'm assuming that you want to syncronize the databases and files. This was my way out, I hope it will be of help to someone.

The first code is local, and the other one is remote.

//make sure you are connected to your local database

<?php

//function to check internet connection.

function is_connected() {
  if($connected = fsockopen("www.example.com", 80)){
  // website, port  (try 80 or 443)
  if ($connected){
    $is_conn = true; //action when connected
    fclose($connected);
  }

return $is_conn;

} else {
    $is_conn = false; //action in connection failure
  }
}

//if connected to internet, do the following...
if(is_connected()== true){

  echo "connected"; 

  ini_set('max_execution_time', 3000);//increase this incase of slow internet

$table_name = TableName::find_all();
//whatever way you find an array
//of all your entries on this particular 
//table that you want to sync with the remote table.

$file = 'to_upload_local.php'; //a local file to put table contents into
$current = serialize($table_name);//serialize the table contents (google).
file_put_contents($file, $current);//put the serialized contents to the file.

$remote_file = 'public_html/to_upload_remote.php';//this is the file that is on the remote server that you want to overwrite with the local file to upload. 

$ftp_server = "ftp.yourwebsite.org";// your ftp address

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, "yourFTPUsername", "yourFTPPassword");

// turn passive mode on
ftp_pasv($conn_id, true);

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)){
    echo "Upload Successful";
} else {

}

// close the connection
ftp_close($conn_id);

//this script called below is to update your remote database. Its in the next example

$call_script = file_get_contents('http://path_to_your_script');
} else { 
 //if not connected to internet,....
echo "offline";
}
?>

The online script that should do the work, (the one you called in the last line of the previous code) should look something like this:

//make sure you're connected to remote database
<?php 

//this function should compare num_rows of your two 
//databases values (local  remote) and return the 
//difference. It's used with array_udiff function.
function compare_objects($obj_a, $obj_b) {
  return $obj_a->id - $obj_b->id;
}
//this function should compare contents of your two 
//databases values (local  remote) and return the 
//difference. It's used with array_udiff function.
function comparison($obj_a, $obj_b){
   if ($obj_a==$obj_b){
     return 0;
   }else{
    return -1;
   }
 }


$file = '../to_upload_remote.php';//the uploaded file
$current = file_get_contents($file);//load the file
$array = unserialize($current);//unserialize to get the object array
$remote_table_name = remote_table_name::find_all();//get what you have in 
//remote database

//if a new value is added, create a new entry to database with new vals
if($try_new = array_udiff($array, $remote_table_name, 'compare_objects')){
  foreach($try_new as $entry){
  $remote_table_name = new remote_table_name();
  $remote_table_name->value = $entry->value;
  //depending on the number of your columns,
  //add values to remote table that were not there before.
  //you can use any other suitable method to do this.
      if($remote_table_name->save()){
          echo "the remote_table_name was saved successfully";
      }
  }
} else {
  echo "same number of rows";
}

//if some values are changed, update them with new vals
if($try_change = array_udiff($array, $remote_table_name, 'comparison')){
  foreach($try_change as $entry){
  $remote_table_name = remote_table_name::find_by_id($entry->id);
  $remote_table_name->value = $entry->value;
    //depending on the number of your columns,
    //update values to remote table.
    //you can use any other suitable method to do this.
    if($remote_table_name->save()){
        echo "the remote_table_name was saved successfully";
    }
  }
} else {
  echo "All values match";
}

?>

So, any time the first code is executed, it reads the local table, takes all the values and puts them in the local file, uploads the local file and replaces one in the remote folder, calls a remote script to check the unserialized local table and compares it with the online table, then does the necessary.

Upvotes: 0

Try Dropbox or Google Drive if you don't need to synchronize too much information.

Upvotes: 0

Related Questions