Rsync与Lsync配置手册

snow chuai汇总、整理、撰写---2020/1/30


Rsync配置与实现
1)配置目标主机(需将文件同步到远程的服务器主机)
[root@node5 ~]# yum install rsync -y
[root@node5 ~]# vim /etc/rsyncd.conf
......
......
......
......
......
......
# 于最后追加如下内容 [cloud] # 指定从服务器端同步到本地指定目录 path = /tech/cloud hosts allow = node1.1000cc.net hosts deny = * list = true uid = root gid = root read only = false
[root@node5 ~]# mkdir -p /tech/cloud/cloud-test [root@node5 ~]# systemctl enable --now rsyncd
2) 源主机配置(本地主机) [root@node1 ~]# yum install rsync -y
# 设定所要排除的目录或文件 [root@node1 ~]# vim ~/rsync-exclude.txt cloud-test host-list.txt
[root@node1 ~]# mkdir -pv test/cloud-test [root@node1 ~]# touch test/host-list.txt test/a{1..5}.txt
# 将本地/root/test/目录下的文档同步至node5节点上 [root@node1 ~]# rsync -avz --delete --exclude-from=/root/rsync-exclude.txt /root/test/ node5.1000cc.net::cloud sending incremental file list deleting test/a.txt ./ a1.txt a2.txt a3.txt a4.txt a5.txt
sent 368 bytes received 129 bytes 994.00 bytes/sec total size is 0 speedup is 0.00
4) 客户端验证 [root@node5 ~]# tree /tech/cloud/ /tech/cloud/ ├── a1.txt ├── a2.txt ├── a3.txt ├── a4.txt ├── a5.txt
0 directory, 5 files
5) Rsync其他操作 # 手工指定排除的文件/目录 [root@node1 ~]# rsync -avz --delete --exclude=host-list.txt /root/test/ node1.1000cc.net::cloud
# 手工指定同步的文件 [root@node1 ~]# rsync -avz --delete --include=host-list.txt --exclude=/* /root/test/ node1.1000cc.net::cloud
# 使用文件指定同步的文件 [root@node1 ~]# vim rsync-include.txt host-list.txt
[root@node1 ~]# rsync -avz --delete --include-from=/root/rsync-include.txt /root/test/ --exclude=/* node1.1000cc.net::cloud
6) 从服务器同步至指定本地目录 # 本例演示将CentOS7的源同步至本地指定目录 [root@node5 ~]# rsync -avz --progress rsync://mirrors.163.com/centos/7/ /var/www/html/
Rsync+Lsync实现主动同步
1) 完成Rsync配置并实现同步机制
2) 安装Lsyncd [root@node1 ~]# yum --enablerepo=epel install lsyncd -y
3) 配置lsyncd [root@node1 ~]# vim /etc/lsyncd.conf # 注释第8行 -- sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}
...... ...... ...... ...... ...... ......
# 于文件最底部追加如下内容 settings{ statusFile = "/tmp/lsyncd.stat", statusInterval = 1, } sync{ default.rsync, # 监控所需要同步的目录 source="/root/test/", # 同步到指定的FQDN或IP target="node1.1000cc.net::cloud", # 指定同步时所需要排除的文件 excludeFrom="/root/rsync-exclude.txt", }
[root@node1 ~]# systemctl enable --now lsyncd [root@node1 ~]# touch test/a{6..8}.txt
4) 客户端测试 [root@node5 ~]# /tech/cloud/ ├── a1.txt ├── a2.txt ├── a3.txt ├── a4.txt ├── a5.txt ├── a6.txt ├── a7.txt └── a8.txt
0 directories, 8 files

 

如对您有帮助,请随缘打个赏。^-^

gold