Rsync实现服务器的文件同步

环境部署:

  服务器1:192.168.5.208,作为客户端

  服务器2:192.168.5.185,作为服务端

  实现功能:每当192.168.5.208服务器中的文件发生改变时,就同步到192.168.5.185服务器中。

一、服务端配置

服务端配置(即192.168.5.185服务器的配置)

(1)软件安装

yum install rsync xinetd

(2)为 rsyncd 服务编辑配置文件,默认没有,需自己编辑

vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 5
timeout = 600
pid file = /var/run/rsyncd.pid
lockfile = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[web1]
path = /data/rsync/rsync_data/
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.5.208
hosts deny = *
list = yes
auth users = web
secrets file = /data/rsync/web.passwd

(3)创建文件同步的目录,上面配置里的path,如果有就不用创建了

mkdir /data/rsync/rsync_data/

(4)创建配置中的密码文件,并增加权限:

echo "web:123" > /data/rsync/web.passwd
chmod 600 /data/rsync/web.passwd

(5)启动

systemctl start  xinetd
systemctl status  xinetd

二、 客户端配置

客户端配置(即192.168.5.208服务器的配置)

(1)安装软件

yum -y install rsync

(2)创建web目录

mkdir /data/rsync/sftp/

(3)设置密码并设置权限

echo "123"> /tmp/rsync.password
chmod 600 /tmp/rsync.password

(4)关闭防火墙

systemctl stop  iptables.service

#在客户端测试(即192.168.5.208服务器)
rsync -avzP --delete --password-file=/tmp/rsync.password /data/rsync/sftp/ web@192.168.5.185::web1

问题一:rsync: failed to connect to 10.10.10.170: Connection refused (111)

原因:在服务端,rsync服务未启动

启动rsync服务:rsync --daemon --config=/etc/rsyncd.conf

[root@node02 ~]# netstat -tnlp | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      8393/rsync
tcp6       0      0 :::873                  :::*                    LISTEN      8393/rsync
[root@node02 ~]# ps -ef | grep rsync
root      8393     1  0 16:26 ?        00:00:00 rsync --daemon --config=/etc/rsyncd.conf
root     11381 11112  0 16:28 pts/0    00:00:00 grep --color=auto rsync

三、数据实时同步

环境:Rsync + Inotify-tools

以下操作在客户端配置

#下载安装
wget https://agebt.oss-cn-beijing.aliyuncs.com/my-resource/inotify-tools-3.13.tar.gz
tar -zxvf inotify-tools-3.13.tar.gz
mkdir /usr/local/inotify
cd inotify-tools-3.13
./configure --prefix=/usr/local/inotify/
make && make install

设置环境变量

vim /etc/profile
#在末尾增加一行:
export PATH=$PATH:/usr/local/inotify/bin
#使配置生效
source /etc/profile

echo '/usr/local/inotify/lib' >> /etc/ld.so.conf #加载库文件
ldconfig
ln -s /usr/local/inotify/include /usr/include/inotify

测试脚本vim /data/test/test.sh:

#!/bin/bash
src=/data/rsync/sftp/
user=web
host1=192.168.5.185
dst1=web1
passpath=/tmp/rsync.password

/usr/local/inotify/bin/inotifywait \
-mrq --timefmt '%d/%m/%y' \
--format '%T %w%f%e' \
-e modify,delete,create,attrib \
/data/rsync/sftp/ | while read files
do
    rsync  -vzrtopg --delete --progress --password-file=$passpath  $src $user@$host1::$dst1
    echo "${files} was rsyncd" >>/tmp/rsync.log 2>&1
done

#设置自动运行
chmod 755 /data/test/test.sh
bash /data/test/test.sh &
echo '/data/test/test.sh &' >> /etc/rc.local #设置开机自启

参考文档:https://www.cnblogs.com/gyfluck/p/10497578.html

posted @   wyanl  阅读(97)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示