1、A服务器安装lsyncd
#前提是有配置EPEL源
#依赖
yum install -y gcc gcc-c++ lua lua-devel cmake libxml2 libxml2-devel
#lsyncd安装
yum -y install lsyncd
2、A服务器配置到B服务器的免密登录
ssh-keygen -t rsa
ssh-copy-id -p 22 root@B
ssh -p 22 root@B
3、配置文件解释
vim /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd.log", --日志路径
statusFile = "/var/log/lsyncd.status", --状态文件
pidfile = "/var/run/lsyncd.pid", --pid文件路径
statusInterval = 1, --状态文件写入最短时间
nodaemon = false, --daemon运行
maxProcesses = 1, --最大进程
maxDelays = 1, --最大延迟
}
sync {
default.rsyncssh, --默认rsync+ssh,rsync版本需要升级3以上版本
source = "/apps/www/renwoleblog/", --源目录
delete = true, --保持完全同步
delete = false, --只同步A服务器的添加文件不同步删除操作
host = "root@B服务器地址",
targetdir = "/apps/www/renwoleblog/bak/", --目标目录
exclude={
".txt" --需排除的文件
},
rsync = {
binary = "/usr/bin/rsync", --需先安装好rsync
archive = true, --归档
compress = false, --压缩
owner = true, --属主
perms = true, --权限
whole_file = false
},
ssh = {
port = 22
}
4、本地目录同步配置方案
sync {
default.rsync,
source = "/apps/www/renwoleblog/", --源文件
target = "/apps/www/renwoleblog/bak/", --同步到的位置
}
5、远程同步rsync-daemon模式配置方案
sync {
default.rsync,
source = "/apps/www/renwoleblog/",
target = "root@B服务器IP::renwolecom",
delete="true", --保持完全同步
exclude = { ".bak*" }, --排除文件
delay = 30,
init = false,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
perms = true,
password_file = "/etc/rsync.password",
_extra = {"--bwlimit=200"}
}
}
6、重点参数解释
-- # 注释符
settings # 是全局配置
sync # 定义同步参数
rsync # 定义同步文件参数
ssh # 定义服务器远程端口
7、启动lsyncd并加入自启
#检测配置文件是否正确
lsyncd -nodaemon /etc/lsyncd.conf
#启动
systemctl start lsyncd
systemctl enable lsyncd
8、测试远程同步案例配置
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/tmp/lsyncd.status",
insist = true,
inotifyMode = "Modify",
statusInterval = 1
}
sync {
default.rsyncssh,
source="/root/lsy_node",
host="B服务ip地址",
targetdir="/root/lsy_test",
delete="false",
maxDelays = 5,
delay = 0,
rsync = {
archive = true,
compress = false,
-- verbose = true
},
ssh = {
port = 22
}
}
#A服务器创建 a b c 三个文件
[root@aly lsy_node]# touch {a,b,c}
[root@aly lsy_node]# ls
a b c
#B服务器查看同步效果
[root@k8s-master lsy_test]# ls
1 2 a b c