rsync

本文详细介绍了如何在Linux环境下安装rsync、设置rsyncd.conf、创建用户认证、启用inotify工具以实现实时文件变更同步,并编写脚本以自动监控并同步源服务器的指定目录至目标服务器。
摘要由CSDN通过智能技术生成

rsync+inotify

环境

角色IP系统版本备注
dest192.168.100.4uos8目标服务器(备份)
src192.168.100.3uos8源服务器

目标服务器

安装

[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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值