**环境**
源服务器:
* IP地址:172.16.1.131;
* 应用:rsync,inotify-tools,脚本;
* 操作系统:kylin v10 sp1 server 0518
目标服务器:
* IP地址:172.16.1.200;
* 应用:rsync;
* 操作系统:kylin v10 sp1 server 0518
//关闭防火墙与selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//安装rsync服务端软件
[root@localhost ~]# yum -y install rsync
安装过程省略...
//设置rsyncd.conf配置文件
[root@localhost ~]# vim /etc/rsyncd.conf
[root@localhost ~]# cat /etc/rsyncd.conf
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
[share]
path = /yuchen/
comment = sync etc from client
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = Jacky
hosts allow = 172.16.1.131
hosts deny = 172.16.1.2
//配置用户认证文件
[root@localhost ~]# vim /etc/rsync.pass
[root@localhost~]# cat /etc/rsync.pass
Jacky:123456
//设置文件权限
[root@localhost ~]# chmod 600 /etc/rsync.pass
[root@localhost ~]# ll /etc/rsync.pass
-rw-------. 1 root root 13 2月 19 04:14 /etc/rsync.pass
//启动rsync服务并设置开机自启动
[root@localhost ~]# systemctl start rsyncd
[root@localhost ~]# systemctl enable rsyncd
在源服务器上做以下操作
//关闭防火墙和selinux
[root@client ~]# systemctl stop firewalld
[root@client ~]# setenforce 0
setenforce: SELinux is disabled
[root@client ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//只需安装rsync,不需要启动,不需要配置
[root@client ~]# yum -y install rsync
//创建认证密码文件,只需要密码,不需要用户
[root@client ~]# vim /etc/rsync.password
[root@client ~]# cat /etc/rsync.password
123456
//设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@client ~]# chmod 600 /etc/rsync.password
[root@client ~]# ll /etc/rsync.password
-rw------- 1 root root 7 2月 19 15:23 /etc/rsync.password
//在源服务器上创建测试目录/etc/test,然后在源服务器运行以下命令
[root@client ~]# rsync -avH --port 873 --delete /etc/ Jacky@172.16.1.200::share --password-file=/etc/rsync.password
sending incremental file list
./
test/
sent 49 bytes received 15 bytes 6.10 bytes/sec
total size is 0 speedup is 0.00
//在目标服务器上查看,如果在/yuchen目录下,有test目录,则说明同步成功
[root@localhost ~]# ll /yuchen
总用量 0
drwxr-xr-x. 2 root root 6 2月 19 04:25 test
//安装inotify-tools工具,实时触发rsync进行同步
//查看服务器内核是否支持inotify
[root@client ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 2月 19 15:46 max_queued_events
-rw-r--r-- 1 root root 0 2月 19 15:46 max_user_instances
-rw-r--r-- 1 root root 0 2月 19 15:46 max_user_watches
**如果有这个三个以max开头的文件,则表示服务器内核支持inotify**
//安装inotify-tools(如果本地源没有这个rpm,则需要搭建网络源下载)
[root@client ~]# rpm -ivh inotify-tools-3.14-8.el7.x86_64.rpm
警告:inotify-tools-3.14-8.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID 352c64e5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:inotify-tools-3.14-8.el7 ################################# [100%]
//写同步脚本,这是最最重要的一步,请慎之又慎。让脚本自动去检测我们制定的目录下
//文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
//创建一个脚本,并修改权限为755
[root@client ~]# mkdir /scripts
[root@client ~]# touch /scripts/inotify.sh
[root@client ~]# chmod 755 /scripts/inotify.sh
[root@client ~]# ll /scripts/inotify.sh
-rwxr-xr-x 1 root root 0 2月 19 15:54 /scripts/inotify.sh
填写inotify.sh脚本
[root@client ~]# vim /scripts/inotify.sh
[root@client ~]# cat /scripts/inotify.sh
host=172.16.1.200
src=/root/etc
des=share
password=/etc/rsync.password
user=Jacky
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib $src|while read files ; do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
//检查脚本语法是否有错,如果没错则先手动启动脚本
[root@client ~]# bash -s /scripts/inotify.sh
[root@client ~]# nohup bash /scripts/inotify.sh &
[1] 2710
//nohup: 忽略输入并把输出追加到"nohup.out"
[root@client ~]# ps -ef|grep inotify
root 2671 2169 0 15:59 pts/0 00:00:00 bash -s /scripts/inotify.sh
root 2699 2671 0 16:08 pts/0 00:00:00 bash -s /scripts/inotify.sh
root 2710 2699 0 16:10 pts/0 00:00:00 bash /scripts/inotify.sh
root 2711 2710 0 16:10 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root 2712 2710 0 16:10 pts/0 00:00:00 bash /scripts/inotify.sh
root 2714 2699 0 16:10 pts/0 00:00:00 grep --color=auto inotify
//在源服务器上生成一个文件或者目录,然后查看inotify生成的日志
[root@client ~]# echo "hello world">>/etc/httpd/test
[root@client ~]# tail /tmp/rsync.log
20190219 16:12 /etc/httpd/testCREATE was rsynced
[root@client ~]# cat /etc/httpd/test
hello world
//在目标服务器上进行验证
[root@localhost ~]# ls /yuchen
etc test
[root@localhost ~]# cat /yuchen/etc/httpd/test
hello world
设置脚本为开机自动启动
[root@client ~]# chmod +x /etc/rc.d/rc.local
[root@client ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 6月 27 2017 /etc/rc.d/rc.local
[root@client ~]# vim /etc/rc.d/rc.local
[root@client ~]# tail -1 /etc/rc.d/rc.local
nohup /bin/bash /scripts/inotify.sh &
在源服务器删除一个文件,在目标服务器查看是否也是同步
[root@client httpd]# ls /etc/httpd
extra httpd.conf magic mime.types original test
[root@client httpd]# rm -fr test
[root@client httpd24]# ls
extra httpd.conf magic mime.types original
[root@localhost ~]# ls /yuchen/etc/httpd
extra httpd.conf magic mime.types original