file sync using rsync and inotify - downgoon/hello-world GitHub Wiki

rsync 基于块的文件同步

Rsync(remote synchronize) 是一个远程数据同步工具,可以使用“Rsync算法”同步本地和远程主机之间的文件。

Rsync的好处是只同步两个文件不同的部分,相同的部分不在传递。类似于增量备份,这使的在服务器传递备份文件或者同步文件,比起scp工具要省好多时间。rsync 流行到许多Linux系统都默认安装了。

安装启动

  • 编译安装

比如在 centos6 下安装:

$ wget https://download.samba.org/pub/rsync/rsync-3.1.2.tar.gz
$ tar zxvf rsync-3.1.2.tar.gz && cd rsync-3.1.2
$ ./configure
$ make && make install

安装后,可执行文件位于: /usr/local/bin/rsync

  • 启动
$ /usr/local/bin/rsync --daemon --config=/etc/rsyncd.conf
  • 停止
$ pkill rsync

服务端配置

服务端配置文件默认: /etc/rsyncd.conf

port = 10073
uid = root
gid = root
use chroot = no
max connections = 10
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log

[web]
path = /opt/yunpan
ignore errors
incoming chmod = Du=rwx,Dog=rx,Fu=rwx,Fgo=rx
read only = false
list = true
hosts allow = *
#hosts deny = 0.0.0.0/32

#auth users = root
#secrets file = /etc/rsyncd.secrets

讲解

  • 节点[web]: 是rsync的服务,类似HTTP的 /some/path/ ,它表示rsyncd上有个地址叫web的服务,以便客户端通过它来寻址。

  • secrets file: 密码文件被注释了。

  • 权限配置:incoming chmod = Du=rwx,Dog=rx,Fu=rwx,Fgo=rx 其中Du=rwx表示Directory user,目录对用户是rwx权限;Fgo=rx表示File group owner,文件对同组的用户是rw权限。

客户端同步

rsyncd 服务端启动后,客户端既可以向服务端Push文件,又可以从服务端Pull文件。

对于Push模式,我们希望当一个文件写入后,我们就能通过inotify获得OS的通知,然后把文件从master推送到slave(从本地推送到rsyncd端)。但是如果更新太频繁,我们或许希望以batch批量的形式提交,比如最多等1s或者5s的时间,批量推送一次。可以借助 lsyncd 开源软件。

Lysncd 实际上是 lua语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify 触发机制,然后通过rsync去差异同步,达到实时的效果。我认为它最令人称道的特性是,完美解决了 inotify + rsync海量文件同步带来的文件频繁发送文件列表的问题 —— 通过时间延迟或累计触发事件次数实现。另外,它的配置方式很简单,lua本身就是一种配置语言,可读性非常强。lsyncd也有多种工作模式可以选择,本地目录cp,本地目录rsync,远程目录rsyncssh。

安装 lsyncd

在 ubuntu 下,安装极为简单:

$ apt-get install lsyncd

注意

Lsyncd 2.2.1 requires rsync >= 3.1 on all source and target machines.

配置 lsyncd

编辑配置文件 /etc/lsyncd.conf

settings {
    logfile ="/var/log/lsyncd.log",
    statusFile ="/var/run/lsyncd.status",
    nodaemon = false,
    inotifyMode = "CloseWrite",
    maxProcesses = 4,

    --maxDelays = 1
    --statusInterval = 10
}

sync {
    default.rsync,
    source = "/usr/local/nginx/upload",
    target = "rsync://[email protected]:10073/web",
    --delete = true,
    exclude = { ".*" },
    delay = 0,
    --init = true,

    rsync = {
        binary = "/usr/local/bin/rsync",
        archive = true,
        compress = true,
        verbose   = true,
        -- password_file = "/etc/rsyncd.pwd",
        -- _extra    = {"--bwlimit=200"}
    }
}

如上,每需要同步一个,就增加一个 sync 节点,上面是监控本地目录 source = "/usr/local/nginx/upload",当文件写完后,inotifyMode = "CloseWrite",立即执行同步(delay = 0 延迟时间0,如果你想等5s后,集中同步一次,请设置为5),同步到target = "rsync://[email protected]:10073/web"远端。

注意:远端"rsync://[email protected]:10073/web",表示走rsync专有协议,而不是SSH协议。路径/web是前面在rsyncd服务端配置的。不需要密码,因为密码被注释了 -- password_file = "/etc/rsyncd.pwd" (以--开头表示注释)。

启动 lsyncd

/usr/bin/lsyncd -log all /etc/lsyncd.conf

也可把上述命令,放入 /etc/rc.local中,以便开机自启动。


附录-1: 其他平台安装

mac 下安装 rsync

无需安装,默认就带了。 rsync 后台启动:

$ rsync --daemon --config=/etc/rsyncd.conf

ubuntu 下安装 rsync

$ apt-get install rsync
$ cp /usr/share/doc/rsync/examples/rsyncd.conf /etc/rsyncd.conf
$ sudo /etc/init.d/rsync start  

安装后的文件散落在:

服务:    /etc/init.d/rsync
配置:   /etc/default/rsync

附录-2:应用案例“直播电视墙”

直播截图首先推送到“主节点”,为了HA,我们需要在“从节点”也备份。借助rsync做截图同步:主节点运行rsync的daemon服务端,从节点周期性(比如每分钟)执行rsync pull指令,从主节点上拉取图片到从节点。

  • 主从备份结构

主:10.10.83.144 /opt/rsync/liveimg 从:10.10.77.177 /opt/rsync/liveimg

  • 主节点:运行 rsyncd 服务
$ rsync --daemon --config=/etc/rsyncd.conf
#listening on 873
  • 从节点:pull 模式同步

主从复制:pull模式 (Bash运行后台每分钟同步一次) 在从机器上,运行脚本:/opt/scripts/sync_from_live_master.sh

while true
do
  echo "Iter ... at "$(date);
  /usr/bin/rsync -vrtup -R  [email protected]::live_thumb  /opt/rsync/liveimg/thumb/  > /dev/null 2>&1
  /usr/bin/rsync -vrtup -R  [email protected]::live_page  /opt/rsync/liveimg/live/  > /dev/null 2>&1
  sleep 60;
done
  • 对外HA:截图地址

对外暴露一个Virtual IP,Virtual IP 对应后端的主从节点。 http://tvimg.tv.itc.cn/thumb/200x/192.jpg

Virtual IP 的主从节点,必须位于同一个以太网内,否则Virtual IP 无法工作。


参考资料