Link to home
Start Free TrialLog in
Avatar of Brad_nelson1
Brad_nelson1

asked on

How to keep remote files up to date with master server?

I have remote servers that have 2 files that i need to keep up to date with the 2  files on my master server.

The problem is my remote servers are both windows and linux.

Whats the best way I should do this? doesn't have to be PHP but that would be nice.


Thanks guys!
Avatar of Skonen
Skonen

It depends on what you consider "up to date". If you've got ssh or PHP console access, you can use the following small script which depending on the file sizes, could have some undesired bandwidth usage:

<?php

$strServer = "http://www.mysite.com";

$aFiles = Array(); $aDest = Array();

//These are the files you are grabbing from the server
$aFiles[] = "example1.txt";
$aFiles[] = "files/example2.txt";

//Where to put the corresponding files
$aDest[] = "example1.txt";
$aDest[] = "example2.txt";

$iMinutes = 15;
$iSleep = $iMinutes*60;

$sizeofFiles = sizeof($aFiles);

while (1) {

for ($i=0; $i < $sizeofFiles; $i++){

     if ($fp = fopen($strServer . '/' . $aFiles[$i], "rb")){
         $file_contents = "";
         while (!feof($fp)) {
              $file_contents .= fgets($fp, 1024);
         }
         if ($fp_dest = fopen($aDest[$i], "wb")){
         fwrite($fp, $file_contents, strlen($file_contents);
         @fclose($fp_dest);
         }

         @fclose($fp);
     }
}

sleep($iSleep);
}

?>
It will attempt to download the files and write them to the local server every 15 minutes. However, this does not check to see if the files have been updated, but being that you referred to it as a Master Server, it really shouldn't matter. You may also need to add the following line after the opening php tag (doubtful):

ini_set("max_execution_time", 0);
Avatar of Roonaan
Do you have access to cron or the windows taskmanager on your servers? You then could cronjob/task the following php file.

<?php
  //notice the url and dir difference with the two variables below!
  $master_server_url = 'http://www.myfirstdomain.com/';
  $second_server_dir = '/wwwhome/';

  //all files that needed to be checked
  $files = array('news/news.txt', 'conf/conf.xml');

  foreach($files as $file)
  {
      //Use some vars just for understandability
      $file1 = $master_server_url.$file;
      $file2 = $second_server_dir.$file;
     
      //retrieve the last modification time of the files.
      $time1 = @filemtime($file1);
      $time2 = @filemtime($file2);
     
      //If file1 is newer than file2, update.
      if($time2 < $time1)
         @copy($file1, $file2);
  }
?>

I added loads of '@' to suppress warnings. But the idea must be clear. warnings could occur when files aren't accessable, you haven't got your file2's chmodden to be writable, etc etc.

-r-
I didn't realize the core copy function properly handled remote files, so my example is much more bloated. Roonan is correct about Cron/TaskMon, it's preferred as it doesn't hog nearly as many resources. Next time I won't attempt to answer a question when I've got for more than 24 hours without sleep ;)
If you have root, you could schedule an rsync task to mirror the files:
http://www.danhendricks.com/articles/My%20Articles/Poor%20Man's%20Guide%20to%20Load%20Balancing/
Avatar of Brad_nelson1

ASKER

 //notice the url and dir difference with the two variables below!
  $master_server_url = 'http://www.myfirstdomain.com/';
  $second_server_dir = '/wwwhome/';

Im not understanding that top part of the code.

my files will be located at: http//domain.com/files/
my remote server would be: http://ipaddress/files/

so would that make the top:
  $master_server_url = 'http://www.domain.com/files/';
  $second_server_dir = 'http://ipadddress/files/';


I got it I had to make it:

$master_server_url = 'http://www.domain.com/files/';
 $second_server_dir = 'C:\\location\\of\\files\\';

I also had to change the < to a > in:

//If file1 is newer than file2, update.
      if($time2 > $time1)
         @copy($file1, $file2);

Thanks for the help guys!
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial