亲测有效!服务器之前互相实时同步文件

使用场景

现有服务器A和服务器B,如果服务器A的指定目录(例如 /home/paul/rsync/ )中的内容发生变更(增删改和属性变更),实时将这些变更同步到服务器B的目标目录中(例如 /home/paul/rsync/ )。

数据镜像备份工具Rsync
Rsync是一个非常快速和灵活的文件复制工具。它支持本机或者是和远程服务器间的文件复制。Rsync使用了delta-transfer算法,它只需要传送源端和目标端的文件差异部分,大大减少了网络带宽的消耗和复制耗费的时间。Rsync多用于数据备份和镜像。

Rsync使用了快速检查算法,通过比较文件大小或最后修改时间的变化来判断文件是否需要同步。

Rsync连接远程主机有两种方式:使用ssh或rsync daemon。这里使用Rsync方式来实现远程文件备份。

Rsync的安装与操作

安装Rsync
分别在服务器A和服务器B的terminal执行:

sudo yum install rsync
安装完毕后,会发现rsync的配置文件位于 etc/rsyncd.conf 。该文件使用daemon方式同步时需要使用,此处暂不介绍。

配置服务器A和B之间的免密登录

服务器A执行:

[bash] view plaincopy
ssh-keygen
ssh-copy-id 服务器B的IP地址
创建源端目录和目标端目录
在服务器A中:

mkdir /home/paul/rsync
在服务器B中:

mkdir /home/paul/rsync
在服务器A创建测试文件
echo “Hello from Server A” >> /home/paul/rsync/demo.txt
执行文件传送命令
在服务器A运行:

[bash] view plaincopy

(1)

rsync -avPz --progress /home/paul/rsync 192.168.100.130:/home.paul/rsync

(2)

rsync -avPz --delete --progress /home/paul/rsync 192.168.100.130:/home.paul/rsync
会发现在demo.txt也出现在了服务器B中/home/paul/rsync目录。

命令解析:

(1) 将服务器A中的/home/paul/rsync目录内的文件复制到服务器B(192.168.100.130)的/home.paul/rsync中。

(2) 比较目标端和源端的文件,如果目标端的文件在源端不存在,在目标端删除该文件。

Rsync存在的问题
Rsync仅是一个文件复制工具,它无法对源端文件的增删改操作进行监听。在源端做出更改后,需要执行rsync命令才能将变更同步到目标端。
Rsync在每次同步前需要扫描整个目录。如果源目录内的文件数量比较多,扫描可能需要耗费较多的时间。
为了满足实时监听的要求,我们需要引入另一个工具:inotify。

文件系统事件监听工具inotify
inotify-tools为inotify提供一个简单接口。它是一个c语言编写的库,同时也包含命令行工具。

inotify-tools的详细介绍请点击: https://github.com/rvoicilas/inotify-tools/wiki

inotify-tools的安装
对于centos7系统,依次执行:

[bash] view plaincopy
yum install -y epel-release
yum --enablerepo=epel install inotify-tools
使用inotifywait命令进行事件监听
监听脚本如下(inotifywait-rsync.sh):

[bash] view plaincopy
inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M’ --format ‘%T %w%f’ -e modify,delete,create,attrib /home/paul/rsync/ | while read file
do
rsync -avPz --progress /home/paul/rsync/ 192.168.100.130:/home/paul/rsync/
rsync -avPz --delete /home/paul/rsync/ 192.168.100.130:/home/paul/rsync/
echo “${file} was synchronized”
done
参数解析

-m 保持持续监听状态,如果不写该参数,inotifywait会在监听到一次事件之后退出。
-r 递归方式监听目录。
-q 安静模式,打印输出较少的内容。
–timefmt 指定时间的输出格式。
–format 指定事件输出的格式。
-e 设置监听的事件类型。这里监听增删改和metadata的变更。
对于每次触发的监听时间,inotifywait会执行do和done之间的代码。在这里,我们调用之前所说的rsync命令进行文件同步。

Events: 事件
access file or directory contents were read 文件或目录被(访问)读取r
modify file or directory contents were written 文件或目录被写入w
attrib file or directory attributes changed 文件或目录属性变更【理由:chmod更改属性】
close_write file or directory closed, after being opened in writeable mode 文件或目录被写关闭【理由:文件内容被更改】
close_nowrite file or directory closed, after being opened in read-only mode 文件或目录以只读方式打开后关闭
close file or directory closed, regardless of read/write mode 文件或目录被用编辑器(不管是读或写)关闭
open file or directory opened 文件或目录被用编辑器打开
moved_to file or directory moved to watched directory 文件或目录被移动进来【理由:mv目录内重命名】
moved_from file or directory moved from watched directory 文件或目录被移动出去
move file or directory moved to or from watched directory 文件或目录不管是移出或移进
create file or directory created within watched directory 文件或目录被创建【理由:mkdir创建目录】
delete file or directory deleted within watched directory 文件或目录被删除【理由:rm删除】
delete_self file or directory was deleted 文件或目录自删除
unmount file system containing file or directory unmounted 文件系统取消挂载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值