ted directory '/root/etc/test'
[root@135 ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.111.137::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting vmware-root_933-3988752732/
./
test/
sent 77 bytes received 58 bytes 270.00 bytes/sec
total size is 0 speedup is 0.00
#安装inotify-tools工具,实时触发rsync进行同步
[root@135 ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Sep 22 18:53 max_queued_events
-rw-r--r--. 1 root root 0 Sep 22 18:53 max_user_instances
-rw-r--r--. 1 root root 0 Sep 22 18:53 max_user_watches
如果有这三个max开头的文件则表示服务器内核支持inotify
#安装inotify-tools
[root@135 ~]# yum -y install make gcc gcc-c++ --allowerasing
[root@135 ~]# yum -y install inotify-tools
#编写脚本
[root@135 ~]# mkdir /scripts
[root@135 ~]# touch /scripts/inotify.sh
[root@135 ~]# chmod 755 /scripts/inotify.sh
[root@135 ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 Sep 22 19:03 /scripts/inotify.sh
[root@135 ~]# vim /scripts/inotify.sh
host=192.168.111.137
src=/etc
des=etc_from_clien
password=/etc/rsync.pass
user=admin
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@135 ~]# nohup bash /scripts/inotify.sh &
[1] 198988
[root@135 ~]# nohup: ignoring input and appending output to 'nohup.out'
[root@135 ~]# ps -ef|grep inotify
root 198988 24963 0 19:11 pts/0 00:00:00 bash /scripts/inotify.sh
root 198989 198988 0 19:11 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 198990 198988 0 19:11 pts/0 00:00:00 bash /scripts/inotify.sh
root 200024 199782 0 19:11 pts/2 00:00:00 grep --color=auto inotify
测试脚本
//在源服务器上生成一个新文件
[root@135 ~]# touch /etc/123
#查看inotify生成的日志
[root@135 ~]# tail /tmp/rsync.log
20220922 19:15 /etc/123CREATE was rsynced
20220922 19:15 /etc/123ATTRIB was rsynced
#在目标服务器中也可以看见创建的123文件
[root@137 ~]# ls
etc test
[root@137 ~]# ls etc/123
etc/123
设置脚本开机自动启动:
[root@135 ~]# chmod +x /etc/rc.d/rc.local
[root@135 ~]# vim /etc/rc.d/rc.local
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh & //添加此行