rsync+inotify
环境
角色 | IP | 系统版本 | 备注 |
---|---|---|---|
dest | 192.168.100.4 | uos8 | 目标服务器(备份) |
src | 192.168.100.3 | uos8 | 源服务器 |
目标服务器
安装
[root@dest ~]# yum -y install rsync
[root@dest ~]# yum -y install rsync-daemon
设置rsyncd.conf配置文件
[root@dest ~]# vim /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
[etc_from_client]
path = /yinsiyuan/
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 = admin
创建用户认证
[root@dest ~]# echo 'admin:123456' > /etc/rsync.pass
[root@dest ~]# cat /etc/rsync.pass
admin:123456
[root@dest ~]#
设置文件权限
[root@dest ~]# chmod 600 /etc/rsync.pass
[root@dest ~]# ll /etc/rsync.pass
-rw------- 1 root root 13 12月 14 16:10 /etc/rsync.pass
[root@dest ~]#
设置开机自启
[root@dest ~]# systemctl enable rsyncd
[root@dest ~]# systemctl start rsyncd
[root@dest ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 5 0.0.0.0:873 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 5 [::]:873 [::]:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
[root@dest ~]#
源服务器
安装
[root@src ~]# yum -y install rsync
[root@src ~]# yum -y install rsync-daemon.noarch
创建认证密码文件
[root@src ~]# echo '123456' > /etc/rsync.pass
[root@src ~]# cat /etc/rsync.pass
123456
[root@src ~]#
设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@src ~]# chmod 600 /etc/rsync.pass
[root@src ~]# ll /etc/rsync.pass
-rw------- 1 root root 7 12月 14 17:01 /etc/rsync.pass
在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@src ~]# mkdir -pv /root/etc/test
mkdir: 已创建目录 '/root/etc'
mkdir: 已创建目录 '/root/etc/test'
[root@src ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.100.4::etc_from_client --password-file=/etc/rsync.pass
测试
[root@dest ~]# ls /yinsiyuan/
test
[root@dest ~]#
查看服务器内核是否支持inotify
[root@src ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 12月 14 17:17 max_queued_events
-rw-r--r-- 1 root root 0 12月 14 17:17 max_user_instances
-rw-r--r-- 1 root root 0 12月 14 17:17 max_user_watches
[root@src ~]#
安装inotify-tools工具,实时触发rsync进行同步
[root@src ~]# wget https://dl.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/i/inotify-tools-3.14-19.el8.x86_64.rpm
--2023-12-14 17:19:40-- https://dl.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/i/inotify-tools-3.14-19.el8.x86_64.rpm
正在解析主机 dl.fedoraproject.org (dl.fedoraproject.org)... 38.145.60.22, 38.145.60.24, 38.145.60.23
正在连接 dl.fedoraproject.org (dl.fedoraproject.org)|38.145.60.22|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:58668 (57K) [application/x-rpm]
正在保存至: “inotify-tools-3.14-19.el8.x86_64.rpm”
inotify-tools-3.14-19.el8.x86_64. 100%[=============================================================>] 57.29K 15.1KB/s 用时 3.8s
2023-12-14 17:19:51 (15.1 KB/s) - 已保存 “inotify-tools-3.14-19.el8.x86_64.rpm” [58668/58668])
[root@src ~]#
[root@src ~]# yum -y install inotify-tools-3.14-19.el8.x86_64.rpm
写同步脚本,
此步乃最最重要的一步,请慎之又慎。让脚本自动去检测我们制定的目录下
//文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[root@src ~]# mkdir /scripts
[root@src ~]# touch /scripts/inotify.sh
[root@src ~]# chmod 755 /scripts/inotify.sh
[root@src ~]# ll /scripts/inotify.sh
-rwxr-xr-x 1 root root 0 12月 14 17:24 /scripts/inotify.sh
[root@src ~]#
[root@src ~]# vim /scripts/inotify.sh
#!bin/bash
host=192.168.100.4 # 目标服务器的ip(备份服务器)
src=/etc # 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=etc_from_client # 自定义的模块名,需要与目标服务器上定义的同步名称一致
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@src ~]# nohup bash /scripts/inotify.sh &
[1] 61574
[root@src ~]# nohup: 忽略输入并把输出追加到'nohup.out'
[root@src ~]# ps -ef|grep inotify
root 61574 61462 0 17:28 pts/1 00:00:00 bash /scripts/inotify.sh
root 61575 61574 0 17:28 pts/1 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 61576 61574 0 17:28 pts/1 00:00:00 bash /scripts/inotify.sh
root 61578 61462 0 17:28 pts/1 00:00:00 grep --color=auto inotify
[root@src ~]#
在源服务器上生成一个新文件
[root@src ~]# echo 'hello world ' > /etc/abc
[root@src ~]# vim /scripts/inotify.sh
查看inotify生成的日志
[root@src etc]# tail /tmp/rsync.log
20231214 17:31 /etc/abc89=CREATE was rsynced
20231214 17:31 /etc/abc89=MODIFY was rsynced
20231214 17:33 /etc/abcCREATE was rsynced
20231214 17:33 /etc/abcMODIFY was rsynced
[root@src etc]#
到目标服务器上去查看是否把新生成的文件自动传上去了:
[root@dest ~]# cd /yinsiyuan/etc/
[root@dest etc]# cat abc
hello world
[root@dest etc]#
开机自启
[root@src ~]# chmod +x /etc/rc.d/rc.local
[root@src ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x 1 root root 506 12月 14 14:50 /etc/rc.d/rc.local
[root@src ~]#
[root@src ~]# vim /etc/rc.local
#!/bin/bash
nohup /bin/bash /scripts/inotify.sh
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES