Mirror folder from a remote server in pure PHP

I want the folder on one machine to be synchronized with the folder on the other. This is for the WordPress deployment plugin, so I can't rely on rsync or other commands present on any machine. PHP and the web server will be available on both machines, and ideally it will work via HTTP.

My current thinking is that the requesting machine sends a list of local files with the latest modified dates to a script on another computer. Another machine compares with its files and responds with the modified files - either a list of files to be selected individually, or with modified files embedded in the response.

I would rather use an existing solution if one exists. Any ideas?

+6
source share
3 answers

I created a simple set of classes to implement this: https://github.com/outlandishideas/sync

On the server, for example. example.com/remote.php:

const SECRET = '5ecR3t'; //make this long and complicated const PATH = '/path/to/source'; //sync all files and folders below this path $server = new \Outlandish\Sync\Server(SECRET, PATH); $server->run(); //process the request 

On the client:

 const SECRET = '5ecR3t'; //this must match the secret key on the server const PATH = '/path/to/destination'; //target for files synced from server $client = new \Outlandish\Sync\Client(SECRET, PATH); $client->run('http://example.com/remote.php'); //connect to server and start sync 
+8
source

In PHP, I would not recommend it for a number of reasons.

I have exactly what you need as a python application.

This application is built to run as a service, you just start it and forget about it :)

Application: https://gist.github.com/8f62786582c6933395eb

Shell: https://gist.github.com/e08a99937c6f5deac4ab

Note: the shell file should be called fsyncd not fsyncd.sh :)

PHP version above:

https://gist.github.com/3963cbc58793ff7e9773

Note. You need to make it work on both sites and configure each to connect to the other, and configure them to run using clones. Preferably not WP carmines.

I have a directory path that will sync here:

 define("PATH_DATA", PATH_ROOT . "data" . DIRECTORY_SEPARATOR); 

In my case, the data folder is in the script folder. You just need to set the absolute path or use the WP kernel to get the WP downloads file.

Principle:

  • find a way so that both servers can talk to each other. I used the socket server / client approach. You can do HTTP _POST processor (server) and HTTP _POST maker (client).

  • Keep a record of the last synchronization time.

  • At certain intervals, read the folder and write down any files that have changed since the last time synchronization.

  • Send a list of files that will be updated with the changed time stamp to another server.

  • He should compare your list with his entries and tell you which of the files he does not have.

  • Send these files.

  • The receiver will record files and set the changed date to another on another server. (this is important to avoid endless loops)

Good luck.

0
source

It is best to check when the script is last run and then load the folder using ftp_* functions.

 <?php $username = 'root'; // and this $password = 'password'; // this also $host = 'my-remote-server.com'; // and this $remote_backup = 'backups/'; // folder on remote server to upload to $backup_folder = 'to_backup/'; // folder to backup $temp_folder = 'temp_files/'; // a folder on the local server i can write to $last_run = file_get_contents("{$temp_folder}last_run.txt"); // You'll probably want to get this from a database instead if($last_run <= strtotime('-1 day')) { file_put_contents("{$temp_folder}last_run.txt", time()); // Update the last time this was ran $file = time() . '_backup.zip'; // what the file will be called both remotely and locally $ftp = ftp_connect($host); // connect to the ftp server ftp_login($ftp, $username, $password); // login to the ftp server $zip = new ZipArchive; // create a new instance of ZipArchive $zip->open($temp_folder . $file, ZIPARCHIVE::CREATE); // Create a new archive foreach(glob($backup_folder . '*') as $file) // Loop through all files in the local backup directory { $zip->addFile($file); // add that file } ftp_chdir($ftp, $remote_backup); // cd into the remote backup folder $upload = ftp_nb_put($ftp, $remote_backup . $file, $temp_folder . $file); // non-blocking put, uploads the local backup onto the remote server while($upload === FTP_MOREDATA) { // do something else while we're waiting for the non-blocking upload to finish } ftp_close($ftp); // closes the connection } 

It should be non-blocking (good - upload to a remote server), so if you do not have a large number of files for zip, it will normally be included in the index page , for example. There is no error handling, so you might want to add this. It also does not delete the local backup, you can process it.

0
source

Source: https://habr.com/ru/post/944521/


All Articles