在两个服务器之间同步文件夹

Synchronizing folders between two servers

我必须为外部应用程序实现后备解决方案(身份验证系统)。因此,我必须使我的主身份验证服务器的身份验证文件夹与我的后备服务器保持同步。该文件夹包含多个 .php 文件、.bin 文件和一些其他文件。不幸的是,我不知道我应该如何实现(例如每小时)将这些文件夹同步到我的后备服务器。

所有服务器都使用 CPanel / WHM,也许有解决方案或者我怎样才能让它们保持同步?我想到了一个 .php 脚本,它通过 FTP 登录并同步它们。然后我会为这个 .php 脚本放置一个 cronjob。但我什至不知道这是否可能。如果主服务器离线,它当然不会以负面方式影响我的后备服务器。

我是如何should/can意识到这一点的?

我建议您使用 RSYNC,前提是您没有使用共享主机计划。

Rsync, which stands for "remote sync", is a remote and local file synchronization tool. It uses an algorithm that minimizes the amount of data copied by only moving the portions of files that have changed.

http://www.tecmint.com/rsync-local-remote-file-synchronization-commands/

为此,您需要能够访问服务器上的 SFTP 端口,当然还有 linux 终端!

Leonel Atencio 关于 rsync 的建议很棒。

这是我使用的 rsync shell 脚本。它位于我的项目中名为 /publish 的文件夹中。要点包含 shell 脚本提到的 rs_exclude.txt 文件。

rsync.sh

# reverse the comments on the next two lines to do a dry run
#dryrun=--dry-run
dryrun=

c=--compress
exclude=--exclude-from=rs_exclude.txt
pg="--no-p --no-g"

#delete is dangerous - use caution.  I deleted 15 years worth of digital photos using rsync with delete turned on.
# reverse the comments on the next two lines to enable deleting
#delete=--delete
delete=

rsync_options=-Pav
rsync_local_path=../
rsync_server_string=user@example.com
rsync_server_path="/home/www.example.com"

# choose one.
#rsync $rsync_options $dryrun $delete $exclude $c $pg $rsync_local_path $rsync_server_string:$rsync_server_path

#how to specify an alternate port
#rsync -e "ssh -p 2220" $dryrun $delete $exclude $c $pg $rsync_local_path $rsync_server_string:$rsync_server_path

https://gist.github.com/treehousetim/2a7871f87fa53007f17e

运行 通过 cron


Source

编辑您的 crontab。

# crontab -e

Crontab 条目每行一个。注释字符是井号 (#)。为您的 cron 条目使用以下语法。

这些示例假设您将 rsync.sh 脚本放在 ~/rsync 这些示例还将创建 rsync 输出的日志文件。

每分钟

* * * * * ~/rsync/rsync.sh > ~/rsync/rsync.log

每 5 分钟

*/5 * * * * ~/rsync/rsync.sh > ~/rsync/rsync.log

保存您的 crontab 并退出编辑器。您应该会看到一条消息,确认您已添加到 crontab。