系统环境
centos7.4
机器列表
A:172.17.24.139
B:172.17.54.7
C:172.17.24.145
实现目的
要求从A和B两台机器上,将静态文件同步到C机器上。
实现
1、在A、B、C三台机器上分别执行yum -y install rsync xinetd
2、在A、B、C三台机器上分别修改配置文件 /etc/xinetd.d/rsync
,没有则创建。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # default: off # description: The rsync server is a good addition to an ftp server, as it \ # allows crc checksumming etc. service rsync { disable = no #改为no flags = IPv6 socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon log_on_failure += USERID } ~ ~ |
---|
3、修改C机器,配置rsync的配置文件和创建认证密码
vim /etc/rsyncd.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | log file = /var/log/rsyncd.log pidfile = /var/run/rsyncd.pid lock file = /var/run/rsync.lock secrets file = /etc/rsyncd.secrets motd file = /etc/rsyncd.Motd [web] path = /data/web/book_img uid = root gid = root port = 873 use chroot = no read only = no max connections = 200 timeout = 600 hosts allow = A机器 secrets file = /etc/rsyncd.secrets [ad] path = /data/web/ad_img uid = root gid = root port = 873 use chroot = no read only = no max connections = 200 timeout = 600 hosts allow = B地址 secrets file = /etc/rsyncd.secrets [ts] path = /data/web/kf_img uid = root gid = root port = 873 use chroot = no read only = no max connections = 200 timeout = 600 hosts allow = C地址 secrets file = /etc/rsyncd.secrets |
---|
vim /etc/rsyncd.secrets
1 | xs:123,qwe. |
---|
4、重启C的xinetd服务
5、在AB两台机器上安装inotify服务
编译安装:./configure --prefix=/usr/local/inotify && make && make install
6、在A、B上编写同步脚本
1 2 3 4 5 6 7 8 9 10 | #!/bin/bash SRCDIR=/home/wwwroot/novel3/public/static/book_img #要同步的源路径 USER=xs #同步的用户名 IP=C机器 #同步的目标机器IP,也就是这里的C机器 DESTDIR=book #C机器上的book项,在C机器的rsync配置文件中可看到 /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $SRCDIR | while read file do /usr/bin/rsync -rptgoDvLH --port=873 --progress --delete-before $SRCDIR $USER@$IP::$DESTDIR --password-file=/etc/rsyncd.secrets echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1 done |
---|
7、测试
略