测试服务器信息
IP | 主机名 | 部署软件 | 操作系统 | 说明 |
10.33.24.42 | slave-rsync | rsync-3.1.2-6.el7_6.1.x86_64 | Centos 7.6 x64 | 服务端 |
10.33.0.15 | master-rsync | rsync-3.1.2-6.el7_6.1.x86_64 inotify-tools-3.13.tar.gz | Centos 7.6 x64 | 客户端 |
其他的原理就不说了,只是简单记录下操作过程。
实时同步的目录:/usr/local/sourcecode
脚本和密码文件存放的目录:/usr/local/shellfile
我们想测试的目的是:当master-rsync主机的/usr/local/sourcecode/目录下的文件或者文件夹发生变化的时候,slave-rsync的/usr/local/sourcecode/目录实时发生变化。
在master-rsync主机安装inofity-tool工具
[root@master-rsync software]#tar xzvf inotify-tools-3.13.tar.gz
[root@master-rsync software]#cd inofity-tools-3.13
[root@master-rsync software]#./configure --prefix=/usr/local/inofity-tool
[root@master-rsync software]#make && make install
上面四步就把inofity-tool安装到了/usr/local/inofity-tool目录下面了。
配置inofity-tool到环境变量
[root@master-rsync software]#vi /etc/profile
在这个文件的最底部输入以下内容
export INOTIFY_HOME=/usr/local/inofity-tool export PATH=$INOTIFY_HOME/bin:$PATH
保存退出.
[root@master-rsync software]#source /etc/profile
输入上面命令变量实时生效。
在master-rsync主机安装rsync
[root@master-rsync software]#yum -y install rsync
在/usr/local/shellfile创建以下脚本
[root@master-rsync shellfile]#vi rsync_do.sh
#!/bin/bash src=/usr/local/sourcecode/ #需要实时同步的文件夹。注意后面必须以"/"结尾。 des1=web # 同步的模块名称。这个名称是要和slave-rsync的主机rsync定义的模块名称对应的。特别重要。 #des2=web_log # web2上需要同步的目录 host1=10.33.24.42 #rsync服务节点,就是slave-rsync的主机IP #host2=192.168.1.104 #rsync服务节点2 user1=www #连接rsync使用的用户名web1上。就是2台主机连接的用户名。 #user2=backuser #连接rsync使用的用户名web2上 inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -emodify,delete,create,attrib $src | while read file do /bin/rsync -vzrtopg --delete --bwlimit=2000 --progress $src $user1@$host1::$des1 --password-file=/usr/local/shellfile/rsync.passwd ##上面的rsync.passwd文件也放在/usr/local/shellfile目录下。内容等会看看。 --bwlimit=2000 限速参数 。 #/bin/rsync -vzrtopg --delete --progress $src $user2@$host2::$des2 --password-file=/etc/rsync.password echo "${files} was rsynced" >> /var/log/rsync.log 2>&1 done
看看rsync.passwd的内容
[root@master-rsync shellfile]#cat rsync.passwd
123456
上面的这个123456就是连接到slave-rsync主机的密码。用户在上面的rsync_do.sh脚本里面定义了user1=www.
给rsync_do.sh脚本增加可执行权限。
[root@master-rsync shellfile]#chmod +x rsync_do.sh
master-rsync就安装完成了。
下面我们开始安装slave-rsync。
安装非常的简单。
[root@slave-rsync shellfile]#yum -y install rsync
这样就好了。
如果slave-rsync有外网,直接上面这样安装就好了,yum源使用阿里云或者163的都可以。
如果没有外网,可以创建本地的yum源进行安装或者下载tar.gz的源码进行编译安装。
安装如果有什么问题,可以交流下,详细的就不介绍了。就是上面的一个yum安装就好了。
关键是配置要注意一下。
具体的配置文件如下:
[root@slave-rsync shellfile]#vi /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: uid = root gid = root use chroot = no max connections = 20 pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.log # exclude = lost+found/ # transfer logging = yes timeout = 900 ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 [web] ##这个模块很重要,master-rsync 主机上面的脚本配置的dest1就是这个。 path=/usr/local/sourcecode/ ##本地的同步目录。注意一定要以"/"结尾。 #ignore error read only = false # writeonly = false list = false hosts allow = * #slave-rsync作为服务端,允许哪些主机连接,这里输入*表示所有的主机。也可以指定一个网络段:10.33.0.0./24 ,也可以直接指定一个固定的IP。 hosts deny = 0.0.0.0/32 #不允许访问的地址 auth users = www #定义连接的用户名 secrets file = /usr/local/shellfile/rsync.passwd #定义用户名和密码
看看slave-rsync端的rsync.passwd内容
[root@slave-rsync shellfile]# cat rsync.passwd
www:123456
在 slave-rsync主机启动rsync
[root@slave-rsync shellfile]# /bin/rsync --daemon
注意,在通过kill杀死rsync进程的时候,还需要处理掉/var/run/rsync.pid文件。
在master-rsync主机上,启动rsync_do.sh脚本
[root@master-rsync shellfile]# ./rsync_do.sh &
测试一下
在master-rsync的/usr/local/sourcecode/目录下增加、删除、更新文件,同时查看slave-rsync的/usr/local/sourcecode/目录下的文件变化。
[root@master-server sourcecode]# echo "this is my test">>test.txt
[root@master-server sourcecode]# sending incremental file list
./
test.txt
16 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=2/8)
sent 329 bytes received 47 bytes 752.00 bytes/sec
total size is 15,010,629 speedup is 39,921.89
sending incremental file list
sent 267 bytes received 21 bytes 576.00 bytes/sec
total size is 15,010,629 speedup is 52,120.24
在salve-rsync主机上查看文件变化
[root@slave-rsync sourcecode]# ll
总用量 14676
-rw-r--r-- 1 root root 202 7月 10 2019 1.txt
-rw-r--r-- 1 root root 11 7月 10 2019 2.tx
-rw-r--r-- 1 root root 16 7月 10 2019 2.txt
drwxr-xr-x 2 root root 18 7月 10 2019 freeflowtroll
-rw-r--r-- 1 root root 15010375 7月 10 2019 hsserver-3.1.4.49-1.el6.x86_64.rpm
-rw-r--r-- 1 root root 16 7月 10 2019 test.txt
[root@slave-rsync sourcecode]# cat test.txt
this is my test