Rsync

本文介绍了如何配置rsync服务端和客户端,通过rsync-daemon、rsyncd.conf配置、用户认证文件以及权限设置,实现实时文件同步。客户端利用inotify-tools监测文件变化,并自动触发rsync同步到服务端,确保数据一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.准备两台服务器,一个客户端一个服务端,关闭服务端上的防火墙与Selinux(rsync已安装跳过)
[root@server ~]# systemctl stop firewalld
[root@server ~]# systemctl disable firewalld
[root@server ~]# setenforce 0

2.在服务端安装rsync-deamon守护进程
[root@server ~]# yum -y install rsync-daemon
Installed:
  rsync-daemon-3.1.3-7.el8.noarch                                                                                                                                                              

Complete!

3.设置rsyncd.conf配置文件
[root@server ~]# vim /etc/rsyncd.conf 

# configuration example:
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
log file = /var/log/rsyncd.log   
pidfile = /var/run/rsyncd.pid    
lock file = /var/run/rsync.lock   
secrets file = /etc/rsync.pass   

[share]    
path = /tmp/          
comment = sync runtime 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        
hosts allow = 192.168.240.133  

4.创建用户认证文件
[root@server ~]# echo 'admin:123456' > /etc/rsync.pass
[root@server ~]# cat /etc/rsync.pass
admin:123456

5.设置文件权限为600
[root@server ~]# chmod 600 /etc/rsync*
[root@server ~]# ll /etc/rsync*
-rw-------. 1 root root 1731 Oct 18 23:32 /etc/rsyncd.conf
-rw-------. 1 root root   13 Oct 18 23:33 /etc/rsync.pass

6.启动rsync服务并设置开机自启动
[root@server ~]# systemctl start rsyncd
[root@server ~]# systemctl enable rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@server ~]# ss -antl
State                     Recv-Q                    Send-Q                                       Local Address:Port                                        Peer Address:Port                   
LISTEN                    0                         5                                                  0.0.0.0:873                                              0.0.0.0:*                      
LISTEN                    0                         50                                                 0.0.0.0:139                                              0.0.0.0:*                      
LISTEN                    0                         128                                                0.0.0.0:111                                              0.0.0.0:*        
7.客户端关闭防火墙和selinux
[root@client ~]# systemctl stop firewalld
[root@client ~]# systemctl disable firewalld
[root@client ~]# setenforce 0
setenforce: SELinux is disabled

8.客户端上创建认证密码文件,并设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@client ~]# echo '123456' > /etc/rsync.pass
[root@client ~]# cat /etc/rsync.pass
123456
[root@client ~]# chmod 600 /etc/rsync.pass
[root@client ~]# ll /etc/rsync.pass
-rw------- 1 root root 7 Oct 18 23:54 /etc/rsync.pass
9.在源服务器上创建runtime测试目录,然后在源服务器运行以下命令
[root@client ~]# mkdir /runtime
[root@client ~]# cd /runtime
[root@client runtime]# touch abc def
[root@client runtime]# ls
abc  def
[root@client runtime]# rsync -avH --port 873 --progress --delete /runtime admin@192.168.240.134::share --password-file=/etc/rsync.pass
@ERROR: auth failed on module share
rsync error: error starting client-server protocol (code 5) at main.c(1657) [sender=3.1.3]
[root@client runtime]# cd
[root@client ~]# rsync -avH --port 873 --progress --delete /runtime admin@192.168.240.134::share --password-file=/etc/rsync.pass
sending incremental file list
runtime/
runtime/abc
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=1/3)
runtime/def
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=0/3)

sent 182 bytes  received 66 bytes  496.00 bytes/sec
total size is 0  speedup is 0.00
[root@server ~]# ls /tmp/
runtime   
       [root@server ~]# cd /tmp/runtime/
[root@server runtime]# ls
abc  def
              
10.在客户端上安装inotify-tools工具,实时触发rsync进行同步
[root@client ~]# yum -y install inotify-tools
Installed:
  inotify-tools-3.14-19.el8.x86_64                                                                                                   

Complete!

11.写同步脚本,让脚本自动去检测我们制定的目录下文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[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 Oct 19 00:19 /scripts/inotify.sh
[root@client ~]# vim /scripts/inotify.sh
#!/bin/bash
host=192.168.240.134
src=/runtime
des=share
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


12.启动脚本
[root@client ~]# nohup bash /scripts/inotify.sh &
[1] 45163
[root@client ~]# nohup: ignoring input and appending output to 'nohup.out'

[1]+  Done                    nohup bash /scripts/inotify.sh
[root@client scripts]# ps -ef|ps -ef|grep inotify
root       45708   45531  0 01:10 pts/0    00:00:00 /bin/bash /scripts/inotify.sh
root       45709   45708  0 01: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 /runtime
root       45710   45708  0 01:10 pts/0    00:00:00 /bin/bash /scripts/inotify.sh
root       45716   45531  0 01:11 pts/0    00:00:00 grep --color=auto inotify


13.在客户端上测试新建、删除文件操作,让脚本自动运行同步到服务端上
[root@client runtime]# ls
abc  def
[root@client runtime]# touch 123 456
[root@client runtime]# ls
123  456  abc  def
[root@server ~]# ls /tmp/runtime/
abc  def
[root@server ~]# ls /tmp/runtime/
123  456  abc  def

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值