6

我想让一台机器上的文件夹与另一台机器上的文件夹保持同步。这是一个 WordPress 部署插件,所以我不能依赖 rsync 或任何一台机器上存在的其他命令。两台机器上都可以使用 PHP 和 Web 服务器,理想情况下它可以通过 HTTP 运行。

我目前的想法是请求机器将带有最后修改日期的本地文件列表发布到另一台机器上的脚本中。另一台机器与它的文件进行比较,并用修改后的文件进行响应——要么是要单独获取的文件列表,要么是响应中内联的已更改文件。

不过,如果存在,我宁愿使用现有的解决方案。有任何想法吗?

4

3 回答 3

8

我创建了一组简单的类来实现这一点:https ://github.com/outlandishideas/sync

在服务器上,例如 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

在客户端:

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
于 2013-05-24T01:03:16.713 回答
0

在 PHP 中,我不推荐它,原因有很多。

我有你所需要的作为一个 python 应用程序。

这个应用程序被构建为作为服务运行,您只需启动它并忘记它:)

应用程序:https ://gist.github.com/8f62786582c6933395eb

外壳:https ://gist.github.com/e08a99937c6f5deac4ab

注意:shell 文件应该被称为 fsyncd 而不是 fsyncd.sh :)

以上的PHP版本:

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

注意:您需要让它在两个站点上运行,并将每个站点配置为连接到另一个站点,并将它们设置为由 crons 执行。最好不要通过 WP crons。

我在这里定义了将要同步的目录的路径:

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

在我的情况下,数据文件夹位于脚本文件夹中。您应该只设置一个绝对路径或使用 WP 核心来获取 WP 上传目录。

主要是:

  1. 找到一种方法让两台服务器能够相互通信。我使用了套接字服务器/客户端方法。你可以做一个 HTTP _POST 处理器(服务器)和一个 HTTP _POST 制造商(客户端)。

  2. 保留上次同步时间的记录。

  3. 在一定的时间间隔读取文件夹并记录从上次同步时间修改的任何文件。

  4. 将要更新的文件列表及其修改后的时间戳发送到另一台服务器。

  5. 它应该将您的清单与他的记录进行比较,并告诉您哪些文件他没有。

  6. 发送那些文件。

  7. 接收方将写入文件并将修改日期设置为另一台服务器上的文件。(这很重要,以避免无限循环)

祝你好运。

于 2013-05-20T18:17:37.303 回答
0

最好的办法是检查脚本上次运行的时间,然后上传带有ftp_*函数的文件夹。

<?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
    }

它应该是非阻塞的(嗯 - 上传到远程服务器),所以如果你没有很多文件要压缩,它会很好地包含在index page例如。没有任何错误处理,因此您可能想要添加它。它也不会删除本地备份,您可能想要处理它。

于 2013-05-20T20:38:01.310 回答