rsync+inotify实时备份

rsync简介

rsync有四种应用模式 1、shell本地模式 2、远程shellmoshi 3、查询模式 4、服务器模式

1、本地模式用于复制目录到另一个目录 rsync -av aa /bb

2、远程shell模式将本地目录复制到另外一个系统当中 rsync -av aa 192.168.0.10:bb

3、查询(列表)模式查看远程系统中目录的内容 rsync -a 192.168.0.10:bb

4、服务器模式基于C/S模式 服务器启用一个后台守护进程 用于接收或者发送文件

服务器地址

server     192.168.0.101 /web/webdata

web1       192.168.0.103 /web1/webdata

web2       192.168.0.104 /web2/webdata

web3       192.168.0.106 /web3/webdata


1、下载相关软件包

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

wget https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

在server上配置rsync

touch /etc/rsyncd.conf

uid = nobody

gid = nobody

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


 [web]

path = /web/webdata/

comment = ixdba file

ignore errors

read only = no

write only = no

hosts allow = *

list = false

uid = root

gid = root

auth users = webuser

secrets file = /etc/server.pass

各个参数含义

uid    守护进程具有的用户ID

gid    守护进程具有的组ID

max connections    最大并发连接量 默认0 表示无限制

strict modes    检查文件的权限 yes为检查口令文件权限 密码文件的权限必须为root用户权限

pid file    pid文件路径

lock file 支持max connections的锁文件

log file 日志文件路径

[webuser]    模块名称

path    备份的文件或者目录路径

ignore errors    表示忽略I/O错误

read only 设置为no用户可以上传文件 YES表示只读

write only 设置为no用户可以下载文件 yes表示不能下载

hosts allow    允许的主机

hosts deny    拒绝连接的主机

list    当客户请求可使用的模块列表时,该模块是否被列出 默认为true 若要隐藏咋设置为false

auth users    可以连接该模块的用户名

secerts file 用户:密码 文件的路径


创建密码文件

echo "backup" >> /etc/server.pass

注: 发布服务器上的密码文件只设置密码 节点服务器上的密码文件需要设置账户:密码。


在server系统上启动rsync守护进程

/usr/local/bin/rsync --daemon

echo "/usr/local/bin/rsync --daemon" >>/etc/rc.local 将rsync服务添加到自启动文件中

在web123台服务器上分别安装rsync服务



安装inotify-tools在发布服务器上(节点服务器无须安装)

检测内核是否支持 低于2.6.13则需要重新编译内核 加入对inotify的支持

uname -r

[root@localhost ~]# ll /proc/sys/fs/inotify

总用量 0

-rw-r--r-- 1 root root 0 2月  27 10:25 max_queued_events

-rw-r--r-- 1 root root 0 2月  27 10:25 max_user_instances

-rw-r--r-- 1 root root 0 2月  27 10:25 max_user_watches

如果有以上三项输出 就表示系统支持inotify 下面进行安装

tar -zxvf inotify-tools-3.13.tar.gz

cd inotify-tools-3.13

./configure

make

make install

ll /usr/local/bin/inotifywa*

-rwxr-xr-x 1 root root 32030 2月  27 10:39 /usr/local/bin/inotifywait

-rwxr-xr-x 1 root root 33979 2月  27 10:39 /usr/local/bin/inotifywatch

安装完成后会生成inotifywait和inotifywatch 两个文件,inotifywait用于等待文件或者文件集上的一个特定时间,可以监控文件和目录设置 inotifywatch用于收集被监控的文件系统统计数据

在三个节点服务器上配置rsync

web1

uid = nobody

gid = nobody

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


 [web1]

path = /web1/webdata

comment = web1 file

ignore errors

read only = no

write only = no

hosts allow = *

list = false

uid = root

gid = root

auth users = web1user

secrets file = /etc/server.pass

web2

uid = nobody

gid = nobody

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


 [web2]

path = /web2/webdata

comment = ixdba file

ignore errors

read only = no

write only = no

hosts allow = *

list = false

uid = root

gid = root

auth users = web2user

secrets file = /etc/server.pass


web3


uid = nobody

gid = nobody

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


 [web3]

path = /web3/webdata

comment = ixdba file

ignore errors

read only = no

write only = no

hosts allow = *

list = false

uid = root

gid = root

auth users = web3user

secrets file = /etc/server.pass

以上配置完三台节点服务器的rsyncd.conf后 并分别创建密码文件。后依次启动rsync守护进程,并将服务加到自启动文件

echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local


配置服务器内容发布节点

通过编写shell脚本来实现内容的发布

#!/bin/bash

host1=192.168.0.103

host2=192.168.0.104

host3=192.168.0.106

src=/web/webdata/

dst1=web1

dst2=web2

dst3=web3

user1=web1user

user2=web2user

user3=web3user

/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/local/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1

/usr/local/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user2@$host2::$dst2

/usr/local/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user3@$host3::$dst3

echo "${files} was rsynced" >> /tmp/rsynclog 2>&1

done

将此脚本放到后台运行,并加入到自启动文件

sh /root/rsync.sh &

echo "/root/rsync.sh &">>/etc/rc.local

脚本中相关参数如下 

--timefmt 指定时间的输出格式

--format 指定变化文件的详细信息

有时候会遇到这样的情况,发布服务器的目录中写入一个较大的文件,当写入这个大文件需要一段时间时,inotify会持续不断的输出该文件被更新的信息 这样就会不断出发rsync执行同步操作,占用大量系统资源,针对这种情况,最理想的罪罚是等待写完后在触发rsync同步,这种情况下,可以修改inotify的监控事件 即-e close_write,delete,create,attrib    


测试rsync+inotify实时同步

在server服务器的/web/webdata/目录中添加或者删除文件或者目录,查看节点服务器是否更新。                  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章