rsync + inotify 保证文件系统的同步

一、两台服务器IP地址分别为:git

源服务器:192.168.1.2github

目标服务器:192.168.1.3web

@todo:从源服务器(192.168.1.2)的/www/目录下的全部的文件实时同步到目标服务器(192.168.1.3)的/www_bak/目录下/test_bak/下shell

从源的/test/目录下的全部文件实时同步到目标服务器(192.168.1.3)的vim

源服务器下须要安装rsync和inotify,源服务器作为server端,实时的向目标服务器client端发送数据centos

二、安装 rsync安全

通常centos6.5下都已经安装了rsync,因此就没必要安装了,能够用如下命令检查一下是否已安装:bash

rpm -qa |grep rsync服务器

上图显示了个人机器上安装的是rsync-3.0.6-12。ui

若是没有安装请往下看,若是已安装,那就跳过下面的部分:

cd /usr/local/src

wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz

tar zxvf rsync-3.0.9.tar.gz

cd rsync-3.0.9

./configure --prefix=/usr/local/rsync

make

make install

rsync已安装完毕

三、建立同步文件所须要的密码文件,这样作是为了安全

touch /etc/rsyncd.secrets

echo 'newpassword' > /etc/rsyncd.secrets

注:这里的newpassword能够是任意字符

出于安全考虑要把此文件的权限改为600:

chmod 600 /etc/rsyncd.secrets

四、安装inotify

先查看服务器是否支持inotify

ll /proc/sys/fs/inotify

会有三个文件,这说明此服务器是支持 inotify的。

下面安装inotify:

wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 
tar zxvf inotify-tools-3.14.tar.gz 
cd inotify-tools-3.14 
./configure --prefix=/usr/local/inotify 
make
make install

五、建立rsync复制脚本,用户shell来实现,其功能就是:从源服务器(192.168.1.2)的/www/目录下的全部的文件不管是添加、修改、删除文件,可以经过inotify监控到,并经过rsync实时同步到目标服务器(192.168.1.3)的/www_bak/目录下

vim /usr/bin/rsync.sh

#!/bin/bash
host=192.168.1.3
src=/www/
des=web
user=webuser
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files
do
/usr/bin/rsync -zrtopg --delete --progress --password-file=/etc/rsyncd.secrets $src $user@$host::$des
echo "${files} was rsynced" > /var/log/rsyncd.log 2>&1
done

六、建立rsync复制脚本,用户shell来实现,其功能就是:从源服务器(192.168.1.2)的/test/目录下的全部的文件不管是添加、修改、删除文件,可以经过inotify监控到,并经过rsync实时同步到目标服务器(192.168.1.3)的/www_bak/目录下

vim /usr/bin/rsync-b.sh

#!/bin/bash
host=192.168.1.3
src=/test/
des=weba
user=webuser
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files
do
/usr/bin/rsync -zrtopg --delete --progress --password-file=/etc/rsyncd.secrets $src $user@$host::$des
echo "${files} was rsynced" > /var/log/rsyncd.log 2>&1
done

其中host是目标服务器的ip,src是源服务器要同步的目录,des是认证模块名,须要与目标服务器一致,user是创建密码文件里的认证用户。

修改rsync.sh的权限

chmod +x /usr/bin/rsync-b.sh

到此为止,源服务器的全部操做就完成了。下面配置目标服务器。

一、目标服务器也要安装 rsync,安装方式跟源服务器同样,这里就不在赘述了。

二、创建密码文件:

touch /etc/rsyncd.secrets

echo "webuser:newpassword" > /etc/rsyncd.secrets

一样要给此文件一个600的权限

chmod 600 /etc/rsyncd.secrets

注:在源服务器创建的密码文件,只有密码,没有用户名;而在目标服务器里创建的密码文件,用户名与密码都有。

三、写rsync的配置文件:

vim /etc/rsyncd.conf

一、目标服务器也要安装 rsync,安装方式跟源服务器同样,这里就不在赘述了。

二、创建密码文件:

touch /etc/rsyncd.secrets

echo "webuser:newpassword" > /etc/rsyncd.secrets

一样要给此文件一个600的权限

chmod 600 /etc/rsyncd.secrets

注:在源服务器创建的密码文件,只有密码,没有用户名;而在目标服务器里创建的密码文件,用户名与密码都有。

三、写rsync的配置文件:

vim /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
#log format = %t %a %m %f %b # 日志记录格式
[web]
path = /www_bak/
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 192.168.1.2
hosts deny = *
list = false
uid = root
gid = root
auth users = webuser
secrets file = /etc/rsyncd.secrets

[weba]
path = /test_bak/
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 192.168.1.2
hosts deny = *
list = false
uid = root
gid = root
auth users = webuser
secrets file = /etc/rsyncd.secrets

目标服务器启动 rsync

/usr/bin/rsync --daemon --config=/etc/rsyncd.conf

五、源服务器启动同步:

/usr/bin/rsync.sh &

/usr/bin/rsync-b.sh &

到这里,全部的都已完成。能够到源服务器下的/www目录下建一个文件,而后再看一下目标服务器下的/www_bak/下是否有?

相关文章
相关标签/搜索