您的位置:首页 > 其它

rsync+inotify实现数据的实时备份

2012-04-25 15:22 525 查看
rsync 即remote sync 是Linux/UNIX系统下的文件同步和数据传输工具 采用rsync算法 使一个客户机和远程文件服务器中间的文件同步通过rsync可以将同一个服务器的数据从一个分区备份到另一个分区 也可以将本地系统的数据通过网络传输方式备份到任何一个远程主机上rsync 可以在终端之后恢复传输 rsync 只传输源文件和目标文件之间不一致的部分 即可以只执行完整备份或增量备份功能特性:可以镜像保存整个目录树和文件系统可以增量同步数据 文件传输效率高 因而同步时间很短可以保持原有文件的权限 时间等属性加密传输数据 保证了数据的安全性可以使用rcp ssh等方式来传输文件 当然也可以直接通过Socket连接传输文件支持匿名传输安装方式:目标主机是接收别的主机发送来的文件的服务器,因此,其rsync需要以守护进程的方式工作。rsync服务通常基于超级守护进程xinetd管理的方式来实现,因此需要事先安装rysnc和xinetd
yum install rsync xinetd
/usr/bin/rsyncd --daemon
chkconfig xinetd on
chkconfig rsyncd on
service xinetd start
应用模式:1.shell本地应用模式:
[root@localhost ~]# rsync -av ab/ /tmp/
building file list ... done
./
a
b
q
sent 209 bytes  received 92 bytes  602.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ll /tmp/
total 12
-rw-r--r-- 1 root root 0 Apr 15 21:21 a
-rw-r--r-- 1 root root 0 Apr 15 21:21 b
-rw-r--r-- 1 root root 0 Apr 15 21:21 q
[root@localhost ~]# rsync -av ab /tmp/
building file list ... done
ab/
ab/a
ab/b
ab/q
sent 214 bytes  received 92 bytes  612.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ll /tmp/
total 20
-rw-r--r-- 1 root root    0 Apr 15 21:21 a
drwxr-xr-x 2 root root 4096 Apr 15 21:21 ab
-a即--active归档模式 以递归方式传输文件并保持所有文件属性-v即--verbose 输出详细模式信息如果源目录名后加了/则只会同步目录中的文件,目录不会同步过去如果源目录名后不加/ 则连同目录一并同步过去2.远程模式
[root@localhost ~]# rsync -av ab/ 172.16.21.1:/tmp
The authenticity of host '172.16.21.1 (172.16.21.1)' can't be established.
RSA key fingerprint is 80:ca:92:a5:74:42:f8:f8:2c:5c:00:22:a7:44:43:a1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.21.1' (RSA) to the list of known hosts.
root@172.16.21.1's password:
building file list ... done
./
a
b
q
sent 209 bytes  received 92 bytes  5.52 bytes/sec
total size is 0  speedup is 0.00
3、rsync 列表模式
rsync -a 172.16.21.1:/tmp4、服务器模式 这种模式是基于C/S模式的 在这种模式下 rsync在后台启用了一个守护进程 这个守护进程在rsync服务器端永久运行 用于接受文件传输请求 因此客户端既可以把文件发送给守护进程也可以向守护进程请求文件 rsync 的服务器模式分成适合作为异地的中心备份服务器或数据异地存储库来使用

目标主机A(172.16.21.11)
编辑配置文件 /etc/rsyncd.conf rsyncd默认配置文件名称 软件在安装后不会生成 用户需自行添加
[root@frank /www/web1/web1]#cat /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
port = 55055

[web1]
path = /www/web1
ignore errors
read only = no
write only = no
host allow = 192.168.10.0/24
list = no
auth users = web1
secrets file = /etc/web.pwd


这里定义了模式web1 允许用户web1通过此模式来同步/www/web1中的内容

向web.pwd中添加用户名密码 操作如下:
echo "web1:web1" >/etc/web.pwd chmod 600 /etc/web.pwd
如果是Redhat5.4系统自带的rsync软件 则源主机的文件中不需要加用户名 只添加backup的密码就行
目标主机



源主机




开启rsync服务
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf




源主机B(172.16.21.1)
源主机不需做任何配置 只需使用rsync命令来同步数据即可 命令如下
[root@md01 /opt]#rsync -vzrtopgr --progress --port 55055 --password-file=/etc/server.pass /www/web1 web1@192.168.10.16::web1
sending incremental file list
web1/
web1/04-cquser.sql
25130 100%    0.00kB/s    0:00:00 (xfer#1, to-check=37/39)
web1/2013-07-09.tgz
在源主机查看日志文件:
[root@frank /www/web1/web1]#tail -f /var/log/rsyncd.log
2013/07/23 15:46:25 [2589] sent 78 bytes  received 122 bytes  total size 0
2013/07/23 15:46:53 [2591] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors
2013/07/23 15:46:53 [2591] Unknown Parameter encountered: "host allow"
2013/07/23 15:46:53 [2591] IGNORING unknown parameter "host allow"
2013/07/23 15:46:53 [2591] name lookup failed for 192.168.10.75: Name or service not known
2013/07/23 15:46:53 [2591] connect from UNKNOWN (192.168.10.75)
2013/07/23 15:46:54 [2591] rsync to web1/ from web1@UNKNOWN (192.168.10.75)
2013/07/23 15:46:54 [2591] receiving file list
2013/07/23 15:46:54 [2591] web1/
2013/07/23 15:47:04 [2591] sent 746 bytes  received 55987156 bytes  total size 89367005
这种方式同步不是实时的,而是通过执行手动执行命令来更新,实时性不高,但随着系统规模的扩大 对数据的安全性和可靠性提出了更高的要求。 rsync同步数据是需要扫描所有文件后进行差量传输,如果文件数量达到了百万甚至千万量级别,扫描所有文件将是非常耗时的,而且发生变化的往往是其中很少的 一部分 因此rsync是非常低效的方式,这些缺点会导致服务器和客户端数据出现不一致 无法在出现应用故障时完全恢复数据。基于以上原因 rsync+inotify组合出现了: inotify是一种强大的、细粒度、异步文件系统系统事件监控机制。Linux内核从2.6.13版本起加入了对inotify的支持。通过inotify可以监控文件系统中添加、删除、移动、修改等动作。利用这个内核接口,第三方软件可以监控文件系统下文件的各种变化情况 inotify-tools就是这样的一个第三方软件
[root@md01 /opt]#ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Jul 23 16:06 max_queued_events
-rw-r--r-- 1 root root 0 Jul 23 16:06 max_user_instances
-rw-r--r-- 1 root root 0 Jul 23 16:06 max_user_watches
1、/proc/sys/fs/inotify/max_queue_events 应用程序调用inotify时需要初始化inotify实例,并时会为其设定一个事件队列,此文件中的值则是用于设定此队列长度的上限;超出此上限的事件将会被丢弃;2、/proc/sys/fs/inotify/max_user_instances此文件中的数值用于设定每个用户ID(以ID标识的用户)可以创建的inotify实例数目的上限;3、/proc/sys/fs/inotify/max_user_watches此文件中的数值用于设定每个用户ID可以监控的文件或目录数目上限;

安装innotify-tools
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz --no-check-certificate
tar xf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
ln -sv /usr/local/lib/libinotify* /usr/lib/
ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0
安装完成后会在/usr/local/bin/ 生成两个命令inotifywait inotifywatch inotifywait用于等待文件或文件集上的一个特定事件 可以监控任何文件和目录设置 并且可以递归地监控这个目录树 inotifywatch用于收集监控的文件系统统计数据 包括每个inotify事件发生多少次等信息 具体用法
打开shell1
inotifywait -r --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e create,delete,modify,close_write -m /www/web1/
对/www/web1这个目录进行监控
在打开shell2
[root@md01 /www/web1]#rm 04-cquser.sql
rm: remove regular file `04-cquser.sql'? y
在shell1中我们可以看到
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
23/07/13 16:09 /www/web1/ 04-cquser.sql DELETE
-r 递归显示整个目录的动作 -m 始终保持时间监听状态 -q 打印出监控时间 --timefmt 时间显示格式 --format 指定变化文件的详细信息 innotifywait是一个监控等待事件 可以配合shell脚本使用它这样就可以实现rsync+inotify功能
配置服务节点(192.168.10.16)
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
port = 55055

[web1]
path = /www/web1
ignore errors
read only = no
write only = no
host allow = 192.168.10.0/24
list = no
auth users = web1
secrets file = /etc/web.pwd
配置内容发布节点md01:
#!/bin/bash
host1=192.168.10.16
src=/www/web1
des1=web1
user1=web1

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f'  -e close_write,modify,delete,create,attrib ${src} | while read  file
do
rsync -vzrtopgr --delete --port 55055 --progress ${src} ${user1}@${host1}::${des1} --password-file=/etc/server.pass &
echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
echo "---------------------------------------------------------------------------"
done
测试:
执行脚本
[root@md01 /opt]#bash aa
另开一个终端,在md01上删除文件
[root@md01 /opt]#rm all_back.sql
rm: remove regular file `all_back.sql'? y
查看
sending incremental file list
web1/
deleting web1/all_back.sql
sent 761 bytes  received 13 bytes  1548.00 bytes/sec
total size is 75770879  speedup is 97895.19
你也可以到host1 上看这个文件是否被删除


将这个脚本命名为inotifyrsync.sh后放到/www/web1目录下 然后为其指定可执行权限放到后台运行 过程如下:
chmod 755 /www/web1/inotifyrsync.sh
/www/web1/inotifyrsync.sh &
echo "/www/web1/inotifyrsync.sh" >> /etc/rc.local
本文出自 “明日香” 博客,请务必保留此出处http://leezqang.blog.51cto.com/1525874/844180
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: