lsyncd:基于rsync的lsyncd自动同步配置

目录

需求

环境

解决办法

1 目标机配置

1.1 安装软件包

1.2 设置认证文件

1.3 生成客户端配置文件

1.4 设置自启并启动服务

2 源机

2.1 安装软件包

2.2 安装lsyncd 2.2.3版本

2.3 设置认证文件

2.4 生成服务端配置文件

2.5 设置自启动并启动服务

2.6 有可能目标机会报错Permission denied (13),是权限的问题

总结

参考文献


需求

需要将192.168.85.102(源机)的数据同步至192.168.85.103(目标机);

环境

两台CentOS服务器,版本CentOS Linux release 7.8.2003 (Core)

解决办法

基于rsync的lsyncd自动同步配置

1 目标机配置

1.1 安装软件包

yum install rsync -y

1.2 设置认证文件

mkdir /etc/rsyncd 
echo "test:test" > /etc/rsyncd/rsyncd.secret 
chmod 600 /etc/rsyncd/rsyncd.secret

1.3 生成客户端配置文件

cat > /etc/rsyncd.conf <<EOF 
uid = root 
gid = root 
use chroot = yes 
read only = no 
write only = no 
#hosts allow = 192.168.85.101
#hosts deny = * 
max connections = 200 
#motd file = /etc/rsync/rsyncd.motd 
pid file = /var/run/rsyncd.pid 
transfer logging = yes 
log format = %t %a %m %f %b 
log file = /var/log/rsync.log 
exclude = lost+found/ 
timeout = 900 
ignore nonreadable = yes 
reverse lookup = no 
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 
 
[www] 
path = /opt/test
list = no 
ignore errors = yes 
comment = www 
auth users = test
secrets file = /etc/rsyncd/rsyncd.secret 
exclude = lost+found/ 
EOF

其中[www]可以配置多个,名称可以自定义,代表了不同的同步文件夹;不同的配置之间需要配置

1.4 设置自启并启动服务

systemctl enable rsyncd.socket 
systemctl start rsyncd.socket

2 源机

2.1 安装软件包

yum install lsyncd rsync -y

2.2 安装lsyncd 2.2.3版本

##安装lua 5.3
yum install -y lsyncd readline-devel cmake rsync
wget http://www.lua.org/ftp/lua-5.3.0.tar.gz
tar zxf lua-5.3.0.tar.gz
cd lua-5.3.0/
make linux
make install
##查看版本
lua -v

##安装lsyncd 2.2.3
curl -OJ -# https://codeload.github.com/axkibe/lsyncd/tar.gz/refs/heads/master
tar zxf lsyncd-master.tar.gz
cd lsyncd-master/
mkdir build
cd build
cmake ../
sed -i '1s/$/ -ldl/' CMakeFiles/lsyncd.dir/link.txt
make
make install
##查看版本
lsyncd --version

2.3 设置认证文件

mkdir /etc/rsyncd 
echo "test" > /etc/rsyncd/rsyncd.passwd 
chmod 600 /etc/rsyncd/rsyncd.passwd
touch /etc/rsyncd/lsyncd.include

2.4 生成服务端配置文件

cat > /etc/lsyncd.conf <<EOF 
settings { 
    logfile ="/var/log/lsyncd/lsyncd.log", 
    statusFile  ="/var/log/lsyncd/lsyncd.status", 
    insist = true, 
    statusInterval = 10, 
    inotifyMode  = "CloseWrite", 
    maxProcesses = 3, 
    -- nodaemon =true, 
} 
sync { 
    default.rsync, 
    source    = "/opt/test", 
    target    = "test@192.168.85.103::www", 
    delay     = 1, 
    -- filterFrom = "/etc/rsyncd/lsyncd.include",
    exclude   = { ".*", "*.tmp", "*.bak" },
    delete = true,
    rsync     = { 
        binary   = "/usr/bin/rsync", 
        archive  = true, 
        compress = false, 
        password_file   = "/etc/rsyncd/rsyncd.passwd" 
    } 
} 
EOF

exclude与filterFrom配置是等价的,可以将过滤规则写进filterFrom指定的文件中,该文件中"-" or "+" 代表 exclude/include.该文件规则如下:

"*"         means everything 
"dir1"      transfers empty directory [dir1] 
"dir*"      transfers empty directories like:"dir1","dir2","dir3", etc... 
"file*"     transfers files whose names start with [file] 
"dir**"     transfers every path that starts with [dir] like"dir1/file.txt","dir2/bar/ffaa.html", etc... 
"dir***"    same as above 
"dir1/*"    does nothing 
"dir1/**"   does nothing 
"dir1/***"  transfers [dir1] directory and all its contents like"dir1/file.txt","dir1/fooo.sh","dir1/fold/baar.py", etc... 
 

实际使用中,规则书写如下:

- *

2.5 设置自启动并启动服务

##设置重启方法
sed -e 's/\/usr\/bin/\' -e '/ExecStart/aExecReload=/usr/bin/kill -HUP \$MAINPID' -i /usr/lib/systemd/system/lsyncd.service
systemctl daemon-reload
systemctl enable lsyncd 
systemctl start lsyncd

启动后,日志如下:

Mon May 17 10:55:37 2021 Normal: --- Startup ---
Mon May 17 10:55:37 2021 Normal: recursive startup rsync: /opt/test/ -> test@192.168.85.103::www/ excluding
.*
*.tmp
*.bak
Mon May 17 10:55:37 2021 Normal: Startup of /opt/test/  -> test@192.168.85.103::www/ finished.

2.6 有可能目标机会报错Permission denied (13),是权限的问题

临时更改为setenforce 0,永久更改为修改/etc/sysconfig/selinux,将SELINUX=enforcing修改为SELINUX=diabled或者SELINUX=permissive;

总结

内部详细的配置,需要多次实验,目前先介绍搭建起来;

参考文献

https://www.cnblogs.com/wshenjin/p/7084961.html

https://www.cnblogs.com/lemon-le/p/8395438.html

https://www.kancloud.cn/hiyang/linux/390195

https://blog.csdn.net/qq_38993101/article/details/85283856

配置文件研究地址:

https://www.cnblogs.com/ilanni/p/4225897.html

https://axkibe.github.io/lsyncd/manual/config/layer4/

https://rsync.samba.org/ftp/rsync/rsyncd.conf.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值