CentOS 7 配置rsync+lsyncd实现海量文件实时同步

/ 0评 / 0

rsync是一个非常好用的文件同步工具, 但是无法进行实时同步, 但通过lsyncd+rsync可以完美的解决海量文件实时备份, lsyncd其实可以理解为inotify + rsync的重新打包整合, 现在参考部分教程简单记录一下.

i44lHs.md.png

主要目的
从主服务器上实时增量备份文件到另外一台服务器上.

先在主服务器上创建无密码的ssh证书.

<span class="hljs-attribute">cd</span> /root/
ssh-keygen -t rsa

一直按回车就会得到两个ssh证书文件, 一个id_rsa秘钥, 一个id_rsa.pub公钥.

查看公钥证书

<span class="hljs-attribute">cat</span> /root/.ssh/id_rsa.pub

然后去备份服务器上

cd <span class="hljs-regexp">/root/</span>
mkdir <span class="hljs-regexp">/root/</span>.ssh
vi id_rsa.pub

然后把之前在主服务器上创建的公钥id_rsa.pub内容复制到这里

把公钥增量添加到~/.ssh/authorized_keys里面

cat /root/id_rsa.pub <span class="hljs-meta">>> </span>/root/.ssh/authorized_keys

再更改公钥文件权限

<span class="hljs-keyword">chmod</span> <span class="hljs-number">600</span> /root/.ssh/authorized_keys

更改sshd配置文件

<span class="hljs-attribute">vi</span> /etc/ssh/sshd_config

把#AuthorizedKeysFile .ssh/authorized_keys前面的#去掉, 让其保持为

<span class="hljs-attribute">AuthorizedKeysFile</span> .ssh/authorized_keys

然后重启备份服务器的ssh

<span class="hljs-attribute">service</span> sshd restart

设置完成后再会主服务器
先测试一下是否可以免密码登录备份服务器

<span class="hljs-attribute">ssh</span> root@备份服务器的ip

如果能正常登录就可以.

然后在备份服务器上操作

<span class="hljs-attribute">vi</span> /etc/rsyncd.conf

配置如下

[<span class="hljs-keyword">backup</span>]
# destination <span class="hljs-keyword">directory</span> <span class="hljs-keyword">for</span> copy
<span class="hljs-keyword">path</span> = /home/<span class="hljs-keyword">backup</span>
# <span class="hljs-keyword">hosts</span> you <span class="hljs-keyword">allow</span> <span class="hljs-keyword">to</span> <span class="hljs-keyword">access</span>
<span class="hljs-keyword">hosts</span> <span class="hljs-keyword">allow</span> = <span class="hljs-number">1.1</span><span class="hljs-number">.1</span><span class="hljs-number">.1</span>
<span class="hljs-keyword">hosts</span> deny = *
<span class="hljs-keyword">list</span> = <span class="hljs-literal">true</span>
uid = root
gid = root
<span class="hljs-keyword">read</span> <span class="hljs-keyword">only</span> = <span class="hljs-literal">false</span>

再设置rsync自动启动

<span class="hljs-selector-tag">systemctl</span> <span class="hljs-selector-tag">enable</span> <span class="hljs-selector-tag">rsyncd</span><span class="hljs-selector-class">.service</span>
<span class="hljs-selector-tag">systemctl</span> <span class="hljs-selector-tag">start</span> <span class="hljs-selector-tag">rsyncd</span><span class="hljs-selector-class">.service</span>

从再退出, 返回主服务器.

<span class="hljs-keyword">exit</span>

返回主服务器后
安装lua lua-devel

yum <span class="hljs-keyword">install</span> lua lua-devel -y
yum <span class="hljs-keyword">install</span> lsyncd -y

编辑配置lsyncd的配置文件

<span class="hljs-attribute">vi</span> /etc/lsyncd.conf

参考配置文件如下

settings {
    logfile = <span class="hljs-string">"/var/log/lsyncd.log"</span>,          <span class="hljs-comment">--日志路径</span>
    statusFile = <span class="hljs-string">"/var/log/lsyncd.status"</span>,    <span class="hljs-comment">--状态文件</span>
    pidfile = <span class="hljs-string">"/var/run/lsyncd.pid"</span>,          <span class="hljs-comment">--pid文件路径</span>
    statusInterval = <span class="hljs-number">1</span>,                       <span class="hljs-comment">--状态文件写入最短时间</span>
    nodaemon = <span class="hljs-literal">false</span>,                         <span class="hljs-comment">--daemon运行</span>
    maxProcesses = <span class="hljs-number">1</span>,                         <span class="hljs-comment">--最大进程</span>
    maxDelays = <span class="hljs-number">1</span>,                            <span class="hljs-comment">--最大延迟</span>
}
sync {
    <span class="hljs-keyword">default</span>.rsyncssh,      <span class="hljs-comment">--默认rsync+ssh,rsync版本需要升级<span class="hljs-number">3</span>以上版本</span>
    source = <span class="hljs-string">"/home/backup/"</span>,                 <span class="hljs-comment">--源目录</span>
    <span class="hljs-keyword">delete</span> = <span class="hljs-literal">true</span>,                            <span class="hljs-comment">--保持完全同步        </span>
    host = <span class="hljs-string">"root@11.22.33.44"</span>,                
    targetdir = <span class="hljs-string">"/home/backup/"</span>,              <span class="hljs-comment">--目标目录</span>
    exclude={                 
             <span class="hljs-string">".txt"</span>            <span class="hljs-comment">--需排除的文件</span>
    },
rsync = {
    binary = <span class="hljs-string">"/usr/bin/rsync"</span>, <span class="hljs-comment">--需先安装好rsync</span>
    archive = <span class="hljs-literal">true</span>,            <span class="hljs-comment">--归档</span>
    compress = <span class="hljs-literal">false</span>,          <span class="hljs-comment">--压缩</span>
    owner = <span class="hljs-literal">true</span>,              <span class="hljs-comment">--属主</span>
    perms = <span class="hljs-literal">true</span>,              <span class="hljs-comment">--权限</span>
    whole_file = <span class="hljs-literal">false</span>
    },
ssh = {
    port = <span class="hljs-number">22</span>
    }
}


设置开机启动并运行

<span class="hljs-selector-tag">systemctl</span> <span class="hljs-selector-tag">enable</span> <span class="hljs-selector-tag">lsyncd</span><span class="hljs-selector-class">.service</span>
<span class="hljs-selector-tag">systemctl</span> <span class="hljs-selector-tag">start</span> <span class="hljs-selector-tag">lsyncd</span><span class="hljs-selector-class">.service</span>

然后去原服务器的/home/backup目录下创建一个文件看看会不会自动同步到备份服务器的/home/backup目录下

i44FHA.md.png

如果无法运行, 查看状态:

<span class="hljs-attribute">systemctl</span> status lsyncd.service

提示

Error: Terminating since out of inotify watches Consider increasing
/proc/sys/fs/inotify/max_user_watches

i5iYPx.md.jpg

这个是超过实例可监听的最大上限, 编辑sysctl.conf文件

<span class="hljs-attribute">vi</span> /etc/sysctl.conf 

把监听值改大一点, 在下面添加

fs.inotify.max_user_watches = 999999999

再重启lsyncd服务

<span class="hljs-attribute">sysctl</span> -p
systemctl restart lsyncd.service

本教程参考以下链接
https://www.jianshu.com/p/0ecac4f6baf2
https://renwole.com/archives/1001
https://linux.cn/article-5849-1.html
https://github.com/axkibe/lsyncd
https://axkibe.github.io/lsyncd/
http://seanlook.com/2015/05/06/lsyncd-synchronize-realtime/

发表评论