使用lsyncd实时同步文件从一台服务器到另一台服务器
一 背景说明
我有3台nginx服务器,想实时同步vhost,html,ssl这三个目录,这样我只是改一台机器的东西,所有服务器上的配置文件也都会被更改,就找到了lsyncd的同步软件,是通过lsyncd结合ssh去实现实时同步的,我这里是想把196上面的3个目录,同步到198和197上.
系统:centos7.9 lsyncd版本:2.2.2 服务端ip:172.19.149.196,两个客户端ip172.19.149.198,172.19.149.197
二 安装lsyncd以及配置
2.1 安装
只是在196的机器上面安装了lsyncd,两个客户端什么都没有安装
yum install lsyncd -y
lsyncd -version #查看版本
Version: 2.2.2
2.2 配置文件
[root@forward vhost]# cat /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/tmp/lsyncd.status",
insist = ture,
statusInterval = 10
}
sync {
default.rsyncssh,
source="/usr/local/nginx/vhost", #源目录
host="172.19.149.198", #目标机器
targetdir="/usr/local/nginx/vhost", #目标目录
excludeFrom="/etc/lsyncd_exclude.lst", #排查那些文件不同步
maxDelays = 5,
delay = 0,
rsync = {
archive = true,
compress = false,
-- verbose = true
},
ssh = {
port = 22, #ssh端口号
}
}
sync {
default.rsyncssh,
source="/usr/local/nginx/html",
host="172.19.149.198",
targetdir="/usr/local/nginx/html",
excludeFrom="/etc/lsyncd_exclude.lst",
maxDelays = 5,
delay = 0,
rsync = {
archive = true,
compress = false,
-- verbose = true
},
ssh = {
port = 22,
}
}
sync {
default.rsyncssh,
source="/usr/local/nginx/ssl",
host="172.19.149.198",
targetdir="/usr/local/nginx/ssl",
excludeFrom="/etc/lsyncd_exclude.lst",
maxDelays = 5,
delay = 0,
rsync = {
archive = true,
compress = false,
-- verbose = true
},
ssh = {
port = 22,
}
}
sync {
default.rsyncssh,
source="/usr/local/nginx/vhost",
host="172.19.149.197",
targetdir="/usr/local/nginx/vhost",
excludeFrom="/etc/lsyncd_exclude.lst",
maxDelays = 5,
delay = 0,
rsync = {
archive = true,
compress = false,
-- verbose = true
},
ssh = {
port = 22,
}
}
sync {
default.rsyncssh,
source="/usr/local/nginx/ssl",
host="172.19.149.197",
targetdir="/usr/local/nginx/ssl",
excludeFrom="/etc/lsyncd_exclude.lst",
maxDelays = 5,
delay = 0,
rsync = {
archive = true,
compress = false,
-- verbose = true
},
ssh = {
port = 22,
}
}
sync {
default.rsyncssh,
source="/usr/local/nginx/html",
host="172.19.149.197",
targetdir="/usr/local/nginx/html",
excludeFrom="/etc/lsyncd_exclude.lst",
maxDelays = 5,
delay = 0,
rsync = {
archive = true,
compress = false,
-- verbose = true
},
ssh = {
port = 22,
}
}
指定不同步那些类型的文件
cat /etc/lsyncd_exclude.lst
*.log
*.jar
*.bak
nginx.conf
2.3 生成ssh证书-(在lsyncd安装的机器上面)
ssh-keygen -t rsa -b 4096
ssh-copy-id -p 22 root@172.19.149.198
ssh-copy-id -p 22 root@172.19.149.197
2.4 检测配置文件并启动
#检测
lsyncd -nodaemon /etc/lsyncd.conf
systemctl start lsyncd.service
至此配置全部完成了,在196的机器上更改配置文件,自动会同步到197和198了