赞
踩
rsync(remote synchronize)是 Liunx/Unix 下的一个远程数据同步工具,它可通过 LAN/WAN 快速同步多台主机间的文件和目录。rsync 通过比较两边文件是否相同,不相同才进行更新。
但是rsync无法知道“什么时候同步”,因为rsync只有执行同步命令的时候,才会去扫描文件判断哪些文件被修改了,只能建立一个定时任务,每隔一定的时间(比如5分钟,10分钟等等)去执行一次同步,这样虽然能同步,但却“不实时”。
lsyncd实际上是lua语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify 触发机制,然后通过rsync去差异同步,达到实时的效果。它完美解决了 inotify + rsync海量文件同步带来的文件频繁发送文件列表的问题 -通过时间延迟或累计触发事件次数实现。
源服务器(需要备份数据的服务器)——只需要部署rsync(daemon模式)
目标服务器(接收数据的服务器)——需要部署rsync和lsyncd
可支持多个源服务器往一个目标同步
也支持双向同步,就是把源端也当做一个目标端,按照目标端进行配置;然后目标也是一个源端,需要按照源端配置lsyncd
方式一(在线)后续步骤以此为基础:
yum -y install rsync
方式二(离线):
- #先下载安装包https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz
- #将安装包放到你喜欢的文件夹
- #进入该文件夹并解压安装
- tar -zxvf rsync-3.1.2.tar.gz
- cd rsync-3.1.2
- ./configure --prefix=/usr/local/rsync
- make
- make install
-
- #启动
- /usr/local/bin/rsync --daemon
- #验证是否启动
- ps -aux |grep rsync
- root 59120 0.0 0.2 1460 972 ?? Ss 5:20PM 0:00.00 /usr/local/rsync/bin/rsync –daemon
- #查看监听端口状态
- netstat -an |grep 873
- tcp4 0 0 *.873 *.* LISTEN
- #查看日志
- cat /var/log/rsyncd.log
-

- #没有该文件,需手动创建
- vi /etc/rsyncd.conf
- #各参数具体含义可自行百度
- log file = /var/log/rsyncd.log #日志路径
- pidfile = /run/rsyncd.pid
- lock file = /run/rsync.lock
- secrets file = /etc/rsync.password #认证文件,连接的账号密码
- [backup] #模块名称,有多个源服务端时就在下面复制整个模块
- path = /data/influxbackup/ #存储接收数据的路径
- 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=root #认证用户,同认证文件里的
- hosts allow = 10.11.1.62 #允许链接的ip
- #hosts deny = x.x.x.x #不允许的ip

mkdir /data/influxbackup/
- echo 'root:123456' > etc/rsync.password
- #查看配置是否成功
- cat etc/rsync.password
- root:123456
- chmod 600 /etc/rsync*
- ll /etc/rsync*
- -rw-------. 1 root root 880 Aug 13 14:54 /etc/rsyncd.conf
- -rw-------. 1 root root 10 Aug 13 14:55 /etc/rsync.password
- systemctl start rsyncd #启动服务
- systemctl status rsyncd #查看服务状态
- systemctl enable rsyncd #设置开机启动
- Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
- #关闭防火墙
- systemctl stop firewalld
- #关闭selinux
- sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
- #刷新配置,立即生效
- setenforce 0
到此,目标服务器配置完成。
- #指定密码,密码注意和上面的密码一致,用户可在命令中设置
- echo "123456" > /etc/rsyncd.password
- chmod 600 /etc/rsyncd.password #设置文件权限
- ll /etc/rsyncd.password
- #同步命令
- rsync -avH --port 873 --progress --delete /root/soft/backuptest/ root@10.11.1.60::backup --password-file=/etc/rsyncd.password
#打印的信息
sending incremental file list
deleting 123 #删除文件也会目标服务器的删除sent 70 bytes received 27 bytes 194.00 bytes/sec
total size is 0 speedup is 0.00
出现以上结果代表rsync安装配置成功。
yum -y install lsyncd
如出现无法安装成功,则先安装epel-release
yum install -y epel-release
服务器没有外网可下载上面的离线安装包
vim /etc/lsyncd.conf
- #具体参数含义可自行百度
- settings {
- logfile = "/var/log/lsyncd/lsyncd.log",
- statusFile = "/var/log/lsyncd/lsyncd.status",
- inotifyMode = "CloseWrite",
- maxProcesses = 8,
- maxDelays = 5, #累计到多少所监控的事件激活一次同步,即使后面的delay延迟时间还未到。
- nodaemon = false,
- }
- sync { #如果有多台服务器需要同步,依次往下配置多个sync即可
- default.rsync,
- source = "/var/lib/influxdb/engine/", #监控的需要备份的目录
- target = "root@10.11.1.60::backup",
- delete = true, #删除信息也同步
- delay = 15, #延迟15秒同步
- init = false, #false代表启动服务前的内容不会同步,如想设置为true,删掉即可,不要配置为true
- rsync = {
- binary = "/usr/bin/rsync", #rsync服务路径
- password_file = "/etc/rsyncd.password", #认证文件
- archive = true,
- compress = false,
- verbose = false,
- init=true
- _extra = {"--bwlimit=200", "--omit-link-times"}
- }
- }

- mkdir /var/log/lsyncd/
- touch /var/log/lsyncd/lsyncd.log
- touch /var/log/lsyncd/lsyncd.status
- #设置权限
- chmod 600 /var/log/lsyncd/lsyncd.log
- chmod 600 /var/log/lsyncd/lsyncd.status
- systemctl start lsyncd
- systemctl status lsyncd
- # systemctl restart rsyncd
- systemctl enable lsyncd #开机启动
Wed Sep 20 17:00:50 2023 Normal: --- Startup ---
Wed Sep 20 17:00:50 2023 Normal: recursive startup rsync: /var/lib/influxdb/engine/ -> root@10.11.1.60::backup/
Wed Sep 20 17:00:56 2023 Normal: Startup of /var/lib/influxdb/engine/ -> root@10.11.1.60::backup/ finished.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。