Mirror folder from remote server in pure PHP

Tamlyn picture Tamlyn · May 8, 2013 · Viewed 10.1k times · Source

I want to keep a folder on one machine in sync with a folder on another. This is for a WordPress deployment plugin so I can't rely on rsync or other commands being present on either machine. PHP and a web server will be available on both machines and ideally it would work over HTTP.

My current thinking is that the requesting machine posts the local file list with last modified dates to a script on the other machine. The other machine compares with its files and responds with the modified files - either a list of files to be fetched individually or with the changed files inlined in the response.

I'd rather use an existing solution if one exists, though. Any ideas?

Answer

Tamlyn picture Tamlyn · May 24, 2013

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

On the server, e.g. 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