rsync服务

本文介绍如何使用rsync结合inotify在Linux环境下实现高效、实时的数据同步。rsync是一款强大的数据镜像备份工具,支持增量备份,能有效减少数据传输量。inotify则提供了一种细粒度的文件系统事件监控机制,用于实时监测文件变化。通过二者结合,可实现数据的实时同步,提高数据备份的效率和可靠性。

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

1. rsync简介

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。

2. rsync特性

rsync支持很多特性:

  • 可以镜像保存整个目录树和文件系统
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等
  • 无须特殊权限即可安装
  • 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽
  • 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接
  • 支持匿名传输,以方便进行网站镜象
  • 不能传输大型文件

3. rsync的ssh认证协议

rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:

  • ssh协议
  • rsync协议

rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件
rsync server端因为不用启动daemon进程,所以也不用配置文件/etc/rsyncd.conf

ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用ssh-keygen -t rsa打通通道

这种方式默认是省略了 -e ssh 的,与下面等价:
rsync -avz /SRC -e ssh root@192.168.125.128:/de
-a //文件宿主变化,时间戳不变
-z //压缩数据传输

//当遇到要修改端口的时候,我们可以:
rsync -avz /SRC -e “ssh -p222” root@192.168.125.129:/de
//修改了ssh 协议的端口,默认是22

4. rsync命令

  • Rsync的命令格式常用的有以下三种:
  1. rsync [OPTION]… SRC DEST
[root@xj ~]# rsync -avz ii /opt
sending incremental file list
ii

sent 63 bytes  received 31 bytes  188.00 bytes/sec
total size is 0  speedup is 0.00
[root@xj ~]# cd /opt/
[root@xj opt]# ls
ii
  1. rsync [OPTION]… SRC [USER@]HOST:DEST
[root@xj ~]# rsync -avz ii root@192.168.125.129:/root
root@192.168.125.129's password: 
sending incremental file list
ii

sent 63 bytes  received 31 bytes  37.60 bytes/sec
total size is 0  speedup is 0.00
[root@xxx ~]# ls
anaconda-ks.cfg  ii
  1. rsync [OPTION]… [USER@]HOST:SRC DEST
[root@xj ~]# ls
anaconda-ks.cfg
[root@xj ~]# rsync -avz root@192.168.125.129:ii /root
root@192.168.125.129's password: 
receiving incremental file list
ii

sent 30 bytes  received 68 bytes  65.33 bytes/sec
total size is 0  speedup is 0.00
[root@xj ~]# ls
anaconda-ks.cfg  ii

1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:

[root@xj ~]# ls
anaconda-ks.cfg  
[root@xj ~]# rsync -a anaconda-ks.cfg aa.cfg
[root@xj ~]# ll
总用量 60
-rw-------. 1 root root   810 2月  18 19:12 aa.cfg
-rw-------. 1 root root   810 2月  18 19:12 anaconda-ks.cfg

2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包
含单个冒号":"分隔符时启动该模式。如:

[root@xj ~]# rsync -azv aa.cfg root@192.168.125.129:/root/gg.cfg
root@192.168.125.129's password: 
sending incremental file list
aa.cfg

sent 588 bytes  received 31 bytes  137.56 bytes/sec
total size is 810  speedup is 1.31
[root@xxx ~]# ll   --大小和源服务器一样
总用量 60
-rw-------. 1 root root   810 2月  18 14:18 anaconda-ks.cfg
-rw-------. 1 root root   810 2月  18 2019 gg.cfg

3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径
包含单个冒号":"分隔符时启动该模式。如:

[root@xxx ~]# ls
anaconda-ks.cfg  gg.cfg test
[root@xxx ~]# 
[root@xj ~]# rsync -azv root@192.168.125.129:/root/test /root
root@192.168.125.129's password: 
receiving incremental file list
test/

sent 15 bytes  received 45 bytes  24.00 bytes/sec
total size is 0  speedup is 0.00
[root@xj ~]# ll
总用量 60
-rw-------. 1 root root   810 2月  18 19:12 aa.cfg
-rw-------. 1 root root   810 2月  18 19:12 anaconda-ks.cfg
drwxr-xr-x. 2 root root     6 2月  18 14:37 test
  • rsync常用选项:
    1. -a, --archive //归档
    2. -v, --verbose //啰嗦模式
    3. -q, --quiet //静默模式
    4. -r, --recursive //递归
    5. -p, --perms //保持原有的权限属性
    6. -z, --compress //在传输时压缩,节省带宽,加快传输速度
    7. –delete //在源服务器上做的删除操作也会在目标服务器上同步

5. rsyc+inotify

rsync与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。

部署rsync+inotify

服务器类型IP地址
源服务器192.168.125.128
目标服务器192.168.125.129
  • 把源服务器上/etc目录实时同步到目标服务器的/xj/下
    目标服务器
[root@xxx ~]# systemctl stop firewalld   --关闭防火墙
[root@xxx ~]# setenforce 0  --关闭selinux
[root@xxx ~]# yum -y install rsync  --安装服务
....
[root@xxx ~]# 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

[xj_client]
        path = /xj/
        uid = root
        gid = root
        port = 873
        ignore errors
        use chroot = no
        read only = no
        list = no
        timeout = 600
        auth users = xj
[root@xxx ~]# vim /etc/rsyncd.pass  --配置用户认证文件
[root@xxx ~]# cat /etc/rsyncd.pass 
xj:1
[root@xxx ~]# chmod 600 /etc/rsyncd.*   --设置文件权限
[root@xxx ~]# ll /etc/rsyncd.*
-rw-------. 1 root root 736 2月  18 15:28 /etc/rsyncd.conf
-rw-------. 1 root root   5 2月  18 15:26 /etc/rsync.pass
[root@xxx ~]# systemctl start rsyncd  
[root@xxx ~]# systemctl enable rsyncd --重启服务和设置开机自动启动
ln -s '/usr/lib/systemd/system/rsyncd.service' '/etc/systemd/system/multi-user.target.wants/rsyncd.service' 

源目标服务器

[root@xj ~]# systemctl stop firewalld --关闭防火墙
[root@xj ~]# setenforce 0  --关闭selinux
[root@xj ~]# yum -y install rsync  --安装服务,不用开启服务
...
[root@xj ~]# vim /etc/rsync.pass  创建认证密码文件
[root@xj ~]# cat /etc/rsync.pass
1
[root@xj ~]# chmod 600 /etc/rsync.pass  ---设置文件权限
[root@xj ~]# ll /etc/rsyncd.pass
-rw-------. 1 root root 2 2月  18 20:02 /etc/rsync.pass
[root@xj ~]# mkdir -pv /root/etc/test  创建测试目录test
mkdir: 已创建目录 "/root/etc/test"
[root@xj ~]# ls
anaconda-ks.cfg  etc  
[root@xj ~]# rsync -avH --port 873 --progress --delete /root/etc/ xj@192.168.125.129::xj_client --password-file=/etc/rsync.pass
sending incremental file list  ---手动同步
./
deleting etc/jj
deleting etc/
test/

sent 49 bytes  received 15 bytes  128.00 bytes/sec
total size is 0  speedup is 0.00
[root@xxx xj]# ls
test
[root@xj ~]# ll /proc/sys/fs/inotify/  ---查看内是否支持inotify
总用量 0
-rw-r--r--. 1 root root 0 2月  18 20:14 max_queued_events
-rw-r--r--. 1 root root 0 2月  18 20:14 max_user_instances
-rw-r--r--. 1 root root 0 2月  18 20:14 max_user_watches
[root@xj ~]# yum -y install inotify  -安装工具
....
[root@xj ~]# mkdir /scripts
[root@xj ~]# touch /scripts/inotify.sh
[root@xj ~]# chmod 755 /scripts/inotify.sh
[root@xj ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 2月  18 20:16 /scripts/inotify.sh
[root@xj ~]# vim /scripts/inotify.sh
host=192.168.125.129    //目标服务器的ip(备份服务器)
src=/root/etc        //在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=xj_client     //自定义的模块名,需要与目标服务器上定义的同步名称一致
password=/etc/rsync.pass        //执行数据同步的密码文件
user=xj          //执行数据同步的用户名
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@xj ~]# nohup bash /scripts/inotify.sh &   启动脚本并放入后台运行
[1] 11570
[root@xj ~]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@xj ~]# cd /root/etc/
[root@xj etc]# cd test/
[root@xj test]# mkdir tt
[root@xj test]# ls
tt
[root@xj test]# tail /tmp/rsync.log 
20190218 20:21 /root/etc/test/jjCREATE was rsynced
20190218 20:21 /root/etc/test/jjATTRIB was rsynced
20190218 20:24 /root/etc/test/ttCREATE,ISDIR was rsynced
[root@xj test]# chmod +x /etc/rc.d/rc.local    设置权限
[root@xj test]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 477 4月   2 2014 /etc/rc.d/rc.local
[root@xj test]# vim /etc/rc.d/rc.local   --设置开机自动运行脚本
[root@xj test]# cat /etc/rc.d/rc.local
#!/bin/bash
touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh   --添加
[root@xj etc]# touch tttt  --重启后验证是否开机自动启动
[root@xj etc]# ps -ef |grep inotify
root       1532   1525  0 02:14 ?        00:00:00 /bin/bash /scripts/inotify.sh
root       1536   1532  0 02:14 ?        00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/etc
root       1537   1532  0 02:14 ?        00:00:00 /bin/bash /scripts/inotify.sh
root       2581   2556  0 02:15 pts/1    00:00:00 grep --color=auto inotify
[root@xxx etc]# ls
test  tttt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值