rsync+inotify实现文件实时同步(二)

1,652次阅读
没有评论

rsync+inotify实现文件实时同步(二)

环境说明

主机名 IP 说明
server 172.16.10.10 服务端
client 172.16.10.100 客户端
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">[root@server ~]<span class="comment"># uname -a</span></span>
<span class="line">Linux server 2.6.32-573.22.1.el6.x86_64 <span class="comment">#1 SMP Wed Mar 23 03:35:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux</span></span>
<span class="line">[root@server ~]<span class="comment"># cat /etc/redhat-release </span></span>
<span class="line">CentOS release 6.7 (Final)</span>

配置rsync服务

rsync服务端配置在172.16.10.10服务器上面。

安装rsync服务

<span class="line">1</span>
<span class="line">[root@server ~]<span class="comment"># yum -y install rsync</span></span>

添加rsync服务的用户

<span class="line">1</span>
2
3
<span class="line">[root@server ~]<span class="comment"># useradd -M -s /sbin/nologin rsync</span></span>
<span class="line">[root@server ~]<span class="comment"># id rsync</span></span>
<span class="line">uid=501(rsync) gid=501(rsync) groups=501(rsync)</span>

创建/etc/rsyncd.conf配置文件

<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<span class="line">[root@server ~]<span class="comment"># vim /etc/rsyncd.conf</span></span>
<span class="line"><span class="comment">#rsync_config_______________start</span></span>
<span class="line">uid = rsync                             <span class="comment">#指定用户</span></span>
<span class="line">gid = rsync                             <span class="comment">#指定组</span></span>
<span class="line">use chroot = no</span>
<span class="line">max connections = 200                   <span class="comment">#最大链接数</span></span>
<span class="line">timeout = 300                           <span class="comment">#超时时间</span></span>
<span class="line">pid file = /var/run/rsyncd.pid          <span class="comment">#PID存放路径</span></span>
<span class="line">lock file = /var/run/rsync.lock         <span class="comment">#锁文件</span></span>
<span class="line"><span class="built_in">log</span> file = /var/<span class="built_in">log</span>/rsyncd.log          <span class="comment">#日志文件存放路径</span></span>
<span class="line">[ansheng]                               <span class="comment">#模块,链接的标签</span></span>
<span class="line">path = /ansheng/                        <span class="comment">#共享的路径</span></span>
<span class="line">ignore errors                           <span class="comment">#忽略错误</span></span>
<span class="line"><span class="built_in">read</span> only = <span class="literal">false</span>                       <span class="comment">#只读为假,可写</span></span>
<span class="line">list = <span class="literal">false</span>                            <span class="comment">#不允许远程列表,不能看文件内容</span></span>
<span class="line">hosts allow = 172.16.10.0/24            <span class="comment">#允许</span></span>
<span class="line">hosts deny = 0.0.0.0/32                 <span class="comment">#拒绝</span></span>
<span class="line">auth users = rsync_backup               <span class="comment">#虚拟用户</span></span>
<span class="line">secrets file = /etc/rsync.password      <span class="comment">#密码文件存放路径 user:password</span></span>
<span class="line"><span class="comment">#rsync_config_______________end</span></span>

生成密码文件

<span class="line">1</span>
2
3
<span class="line">[root@server ~]<span class="comment"># echo "rsync_backup:ansheng" > /etc/rsync.password</span></span>
<span class="line">[root@server ~]<span class="comment"># cat /etc/rsync.password</span></span>
<span class="line">rsync_backup:ansheng</span>

为密码文件设置权限

<span class="line">1</span>
<span class="line">[root@server ~]<span class="comment"># chmod 600 /etc/rsync.password</span></span>

创建共享的目录并授权rsync管理

<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">[root@server ~]<span class="comment"># mkdir /ansheng</span></span>
<span class="line">[root@server ~]<span class="comment"># chown -R rsync.rsync /ansheng/</span></span>
<span class="line">[root@server ~]<span class="comment"># ll -d /ansheng/</span></span>
<span class="line">drwxr-xr-x 2 rsync rsync 4096 Apr 26 14:19 /ansheng/</span>

启动rsync服务并检查

<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
5
6
7
8
9
10
<span class="line">[root@server ~]<span class="comment"># rsync --daemon</span></span>
<span class="line">[root@server ~]<span class="comment"># ps -ef | grep "rsync" | grep -v "grep"</span></span>
<span class="line">root       1155      1  0 14:20 ?        00:00:00 rsync --daemon</span>
<span class="line">[root@server ~]<span class="comment"># netstat -tulnp | grep 873</span></span>
<span class="line">tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      1155/rsync          </span>
<span class="line">tcp        0      0 :::873                      :::*                        LISTEN      1155/rsync</span>
<span class="line">[root@server ~]<span class="comment"># lsof -i :873</span></span>
<span class="line">COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME</span>
<span class="line">rsync   1155 root    4u  IPv4  10943      0t0  TCP *:rsync (LISTEN)</span>
<span class="line">rsync   1155 root    5u  IPv6  10944      0t0  TCP *:rsync (LISTEN)</span>

加入开机自启动

<span class="line">1</span>
2
3
<span class="line">[root@server ~]<span class="comment"># echo "/usr/bin/rsync --daemon" >>/etc/rc.local</span></span>
<span class="line">[root@server ~]<span class="comment"># tail -1 /etc/rc.local </span></span>
<span class="line">/usr/bin/rsync --daemon</span>

配置rsync客户端

客户端172.16.10.100配置rsync客户端实现推送功能。

生成链接服务器需要的密码文件

<span class="line">1</span>
<span class="line">[root@client ~]<span class="comment"># echo "ansheng" > /etc/rsync.password</span></span>

设置密码文件权限

<span class="line">1</span>
<span class="line">[root@client ~]<span class="comment"># chmod 600 /etc/rsync.password</span></span>

文件同步测试

拉取

<span class="line">1</span>
<span class="line">[root@client ~]<span class="comment"># rsync -avz rsync_backup@172.16.10.10::ansheng /rsync_client/ --password-file=/etc/rsync.password</span></span>

推送

<span class="line">1</span>
<span class="line">[root@client ~]<span class="comment"># rsync -avz /tmp/ rsync://rsync_backup@172.16.10.10/ansheng/ --password-file=/etc/rsync.password</span></span>

推送的时候排单个或多个文件

排除单个文件

<span class="line">1</span>
<span class="line">[root@client ~]<span class="comment"># rsync -avz --exclude=rsync_test.txt /tmp/ rsync_backup@172.16.10.10::ansheng --password-file=/etc/rsync.password</span></span>

排除多个文件

<span class="line">1</span>
<span class="line">[root@client ~]<span class="comment"># rsync -avz --exclude={rsync_exclu.txt,rsync_test.txt} /tmp/ rsync_backup@172.16.10.10::ansheng  --password-file=/etc/rsync.password</span></span>

完全同步

<span class="line">1</span>
<span class="line">[root@client ~]<span class="comment"># rsync -avz --delete /rsync_client/ rsync_backup@172.16.10.10::ansheng --password-file=/etc/rsync.password</span></span>

完全同步会把服务端和本地端的文件保持一致

通道模式

一般配合ssh key使用

<span class="line">1</span>
<span class="line">[root@client ~]<span class="comment"># rsync -avzP -e 'ssh -p 22' /etc root@172.16.10.10:/tmp</span></span>

rsync客户端配置inotify

inotify监控rsync客户端上面的文件,一旦有变化就上传到rsync服务端。

查看当前系统是否支持inotify

在开始安装inotify-tools前请先确认你的Linux内核是否啊到了2.6.13,并且在编译时开启CONFIG_INOTIFY选项,也可以通过以下命令检测。

<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
5
6
7
<span class="line">[root@client ~]<span class="comment"># uname -r</span></span>
<span class="line">2.6.32-573.22.1.el6.x86_64</span>
<span class="line">[root@client ~]<span class="comment"># ls -l /proc/sys/fs/inotify/</span></span>
<span class="line">total 0</span>
<span class="line">-rw-r--r-- 1 root root 0 Apr 26 14:42 max_queued_events</span>
<span class="line">-rw-r--r-- 1 root root 0 Apr 26 14:42 max_user_instances</span>
<span class="line">-rw-r--r-- 1 root root 0 Apr 26 14:42 max_user_watches</span>

下载inotify源码包

<span class="line">1</span>
2
<span class="line">[root@client ~]<span class="comment"># cd /usr/local/src/</span></span>
<span class="line">[root@client src]<span class="comment"># wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz</span></span>

编译安装inotify

<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
5
6
<span class="line">[root@client src]<span class="comment"># tar xf inotify-tools-3.14.tar.gz </span></span>
<span class="line">[root@client src]<span class="comment"># cd inotify-tools-3.14</span></span>
<span class="line">[root@client inotify-tools-3.14]<span class="comment"># ./configure --prefix=/usr/local/inotify-tools-3.14</span></span>
<span class="line">[root@client inotify-tools-3.14]<span class="comment"># make && make install</span></span>
<span class="line">[root@client inotify-tools-3.14]<span class="comment"># cd ../</span></span>
<span class="line">[root@client src]<span class="comment"># ln -s /usr/local/inotify-tools-3.14 /usr/local/inotify</span></span>
<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
5
6
7
<span class="line">[root@client src]<span class="comment"># cd /usr/local/inotify</span></span>
<span class="line">[root@client inotify]<span class="comment"># ll</span></span>
<span class="line">total 16</span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 26 14:45 bin			<span class="comment">#inotify执行命令(二进制)</span></span>
<span class="line">drwxr-xr-x 3 root root 4096 Apr 26 14:45 include		<span class="comment">#inotify程序所需用的头文件</span></span>
<span class="line">drwxr-xr-x 2 root root 4096 Apr 26 14:45 lib			<span class="comment">#动态链接的库文件</span></span>
<span class="line">drwxr-xr-x 4 root root 4096 Apr 26 14:45 share			<span class="comment">#帮助文档</span></span>

脚本自动监控

当在/rsync_client目录下创建文件的时候会自动推送到rsync服务端

<span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
5
6
7
8
<span class="line">[root@client ~]<span class="comment"># cat inotify.sh </span></span>
<span class="line"><span class="meta">#!/bin/bash</span></span>
<span class="line">/usr/<span class="built_in">local</span>/inotify/bin/inotifywait -mrq  --format <span class="string">'%w%f'</span> <span class="_">-e</span> create,close_write,delete /rsync_client \</span>
<span class="line">|<span class="keyword">while</span> <span class="built_in">read</span> file</span>
<span class="line">  <span class="keyword">do</span></span>
<span class="line">    <span class="built_in">cd</span> //rsync_client &&\</span>
<span class="line">    rsync -az ./ --delete rsync_backup@172.16.10.10::ansheng --password-file=/etc/rsync.password</span>
<span class="line">  don</span>
admin
版权声明:本站原创文章,由admin2016-07-11发表,共计4219字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)