CentOS 7.2 部署Rsync + Lsyncd服务实现文件实时同步/备份

接收端配置:


1.安装rsync

yum -y install rsync

2.配置同步模块

  1. 编辑同步配置文件

vi /etc/rsyncd.conf

   2. 同步模块配置参数

复制代码

# any name you like
[backup]
# destination directory for copy
path = /usr/blues
# hosts you allow to access
hosts allow = 192.168.16.143
hosts deny = *
list = true
uid = root
gid = root
read only = false

复制代码

   3. 创建同步文件夹

mkdir /usr/blues

  4. 启动同步服务

systemctl start rsyncd

  5. 设置开机启动

systemctl enable rsyncd

发送端配置:

一、配置密钥

1. 主/从服务器之间启用基于密钥的身份验证。登录发送端服务器并用 " ssh-keygen " 命令生成公共或私有的密钥。

2. 使用 " ssh-copy-id " 复制密钥文件到接收端服务器。

ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

二、安装rsync + lsyncd

1. 安装rsync

yum -y install rsync

2. 安装Lsyncd

  1. 安装lsyncd依赖包

yum install lua lua-devel pkgconfig gcc asciidoc

  2. 安装lsyncd rpm资源,并安装lsyncd

rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
yum install lsyncd

三、配置lsyncd

1. 复制lsyncd配置文件

cp /usr/share/doc/lsyncd-2.1.5/examples/lrsync.lua /etc/lsyncd.conf

 2. 编辑lsyncd配置文件

vi /etc/lsyncd.conf

复制代码

----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--

settings {
    logfile ="/var/log/lsyncd.log",
    statusFile = "/tmp/lsyncd.stat",
    statusInterval = 1,
}
sync {
    default.rsync,
    source="/usr/blues/",
    target="192.168.16.144::backup",
    rsync = {
     rsh ="/usr/bin/ssh -l root -i /root/.ssh/id_rsa",
     binary = "/usr/bin/rsync",
     archive = true,
     compress = true,
     verbose = true
   }
}

复制代码

四、启动服务,并设置开机启动

1. 启动lsyncd服务

systemctl start lsyncd

2. 启动完成查看lsyncd状态,确保lsync启动成功

systemctl status lsyncd

3. 设置开机启动

systemctl enable lsyncd

配置过程中遇到的错误与查看日志

以下错误是在服务正常开启的情况下发生的,请先查看服务是否正常启动。

一、错误

1. rsync: failed to set times on "." (in backup): Permission denied (13)

更新.文件的时间失败:原因是权限不够。

此处为selinux权限限制,临时更改为setenforce 0,永久更改为修改/etc/sysconfig/selinux, 将 SELINUX=enforcing 修改为 SELINUX=diabled 或者 SELINUX=permissive

PS:修改系统配置需要重启系统, getenforce 命令查看当前配置值


2. rsync: opendir "/kexue" (in dtsChannel) failed: Permission denied (13)

文件夹权限问题

查看同步的目录权限是否为755

3. time out

可能因为客户端或者服务端的防火墙开启 导致无法通信,可以设置规则放行 rsync(873端口) 或者直接关闭防火墙。 

二、日志

查看日志信息

tail -10 /var/log/lsyncd.log

如返回: Normal: Finished a list after exitcode: 0   表示同步正常

猜你喜欢

转载自blog.csdn.net/rungong123/article/details/88242946