基于rsync+inotify实现文件实时同步
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于rsync+inotify实现文件实时同步相关的知识,希望对你有一定的参考价值。
rsync英文名remote synchronization,可使本地和远程两台主机之间的数据快速复制同步镜像 远程备份的功能,rsync在拷贝时候会先比较源数据跟目标数据,不一致才复制,实现增量拷贝
监听于873/tcp
rsync有许多选项: -n: 在不确定命令是否能按意愿执行时,务必要事先测试;-n可以完成此功能; -v: --verbose,详细输出模式 -q: --quiet,静默模式 尽可能输出少 -c: --checksum,开启校验功能,强制对文件传输进行校验 -r: --recursive,递归复制; -a: --archives,归档,保留文件的原有属性 -p: --perms 保留文件的权限 -t: --times 保留文件的时间戳 -l: --links 保留文件的符号链接 -g: --group 保留文件的属组 -o: --owner 保留文件的属主 -D: --devices 保留设备文件 -e ssh: 表示使用ssh协议作承载 -z: 对文件压缩后传输 --progress:显示进度条 --stats: 显示如何执行压缩和传输
复制前先测试:
[[email protected] ~]# ls anaconda-ks.cfg d1 d2 install.log install.log.syslog [[email protected] ~]# rsync install.log d2 -n [[email protected] ~]# rsync install.log.a d2 -n rsync: link_stat "/root/install.log.a" failed: No such file or directory (2) rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
注意:rsync命令使用中,如果源参数的末尾有斜线,就会复制指定目录的内容,而不复制目录本身;没有斜线,则会复制目录本身;目标参数末尾的斜线没有作用
源数据后面没有/测试
[[email protected] ~]# rsync -rv d1 d2 sending incremental file list d1/a d1/a.bin.sql d1/a.sql d1/all.sql d1/kk/a.sql sent 658784 bytes received 109 bytes 1317786.00 bytes/sec total size is 658357 speedup is 1.00 [[email protected] ~]# ls d2/ d1
源数据后面有/ 测试:
[[email protected] ~]# rsync -rv d1/ d2 sending incremental file list a a.bin.sql a.sql all.sql kk/ kk/a.sql sent 658777 bytes received 111 bytes 1317776.00 bytes/sec total size is 658357 speedup is 1.00 [[email protected] ~]# ls d2 a a.bin.sql a.sql all.sql d1 kk
基于ssh远程推送:
[[email protected] d2]# rsync -e "ssh -p6789" -r ../d1 [email protected]:~/ --progress [[email protected] ~]# ls a a.sql anaconda-ks.cfg d1 install.log install.log.syslog
基于ssh远程拉取:
[[email protected] ~]# rsync -e "ssh -p6789" -rv [email protected]:~/d3 . --progress [email protected]‘s password: receiving incremental file list d3/ d3/a.sql 1868 100% 1.78MB/s 0:00:00 (xfer#1, to-check=0/2) sent 34 bytes received 1972 bytes 802.40 bytes/sec total size is 1868 speedup is 0.93 [[email protected] ~]# ls anaconda-ks.cfg d1 d2 d3 install.log install.log.syslog
安装:
服务器:sherry
客户端:martin
[[email protected] ~]# yum install xinetd rsync
配置文件:
配置文件为/etc/rsyncd.conf,获取帮助的方式:man rsyncd.conf 定义一个全局配置和多个rsync共享配置
[[email protected] ~]# vim /etc/rsyncd.conf # Global Settings uid = nobody gid = nobody use chroot = no max connections = 10 strict modes = yes pid file = /var/run/rsyncd.pid log file = /var/log/rsyncd.log # Directory to be synced [synced_name] path = /path/to/some_dir #共享目录 ignore errors = yes #同步时是否忽略错误 read only = no #yes不能push 只能下载 write only = no #yes不能pull 只能上传 hosts allow = white_list_ip/net #白名单 hosts deny = * #黑名单 说明: 1、二者都不出现时,默认为允许访问; 2、只出现hosts allow: 定义白名单;但没有被匹配到的主机由默认规则处理,即为允许; 3、只出现hosts deny: 定义黑名单;出现在名单中的都被拒绝; 4、二者同时出现:先检查hosts allow,如果匹配就allow,否则,检查hosts deny,如果匹配则拒绝;如二者均无匹配,则由默认规则处理,即为允许; list = false #列出目录 uid = root #不用root gid = root auth users = username #用户认证 secrets file = /etc/rsyncd.passwd #文件格式(明文):username:password文件权限要设置为600;
[[email protected] rsync]# cat /etc/rsyncd.conf # Global Settings uid = nobody gid = nobody use chroot = no max connections = 10 strict modes = yes pid file = /var/run/rsyncd.pid log file = /var/log/rsyncd.log [mysql] path =/mydata/backup/rsync-mysql ignore errors = yes read only = no write only = no hosts allow = 192.168.1.0/24 hosts deny = * list = false uid = sherry gid = sherry auth users = tom secrets file = /etc/rsyncd.passwd
[[email protected] rsync-mysql]# pwd /mydata/backup/rsync-mysql [[email protected] backup]# chown sherry.sherry rsync-mysql/ #用setfacl 数据一致性时候会报错
添加账号密码:
[[email protected] rsync]# cat /etc/rsyncd.passwd tom:222222 [[email protected] rsync]# chmod 600 /etc/rsyncd.passwd
启动端口:
[[email protected] rsync]# chkconfig xinetd on [[email protected] rsync]# service xinetd restart [[email protected] rsync]# chkconfig rsync on [[email protected] rsync]# ss -lntup|grep 873 tcp LISTEN 0 64 :::873 :::* users:(("xinetd",121111,5))
测试:
push 手动输入密码:
#client [[email protected] mysql]# touch a [[email protected] mysql]# ls a [[email protected] mysql]# rsync a [email protected]::mysql Password: #server [[email protected] rsync-mysql]# ll total 0 -rw-r--r-- 1 sherry sherry 0 May 28 10:01 a
push 用文件代替密码:
#client [[email protected] ~]# vim /etc/rsyncd.passwd 222222 [[email protected] ~]# chmod 600 /etc/rsyncd.passwd [[email protected] mysql]# touch b [[email protected] mysql]# rsync --password-file=/etc/rsyncd.passwd b [email protected]::mysql #server [[email protected] rsync-mysql]# ll total 0 -rw-r--r-- 1 sherry sherry 0 May 28 10:01 a -rw-r--r-- 1 sherry sherry 0 May 28 10:03 b
拉取:
[[email protected] mysql]# rm -fr * [[email protected] mysql]# rsync --password-file=/etc/rsyncd.passwd [email protected]::mysql/* . [[email protected] mysql]# ls a b
增量上传:(上传服务器端没有的文件)
#client [[email protected] mysql]# rm -f * [[email protected] mysql]# ls [[email protected] mysql]# touch a b [[email protected] mysql]# ls a b [[email protected] mysql]# rsync --password-file=/etc/rsyncd.passwd -az ./ [email protected]::mysql #server [[email protected] rsync-mysql]# rm -f * [[email protected] rsync-mysql]# ls a b #client [[email protected] mysql]# rm -f b [[email protected] mysql]# touch c [[email protected] mysql]# ls a c [[email protected] mysql]# rsync --password-file=/etc/rsyncd.passwd -az ./ [email protected]::mysql #server [[email protected] rsync-mysql]# ls a b c
同步上传:(数据一致性)
#client [[email protected] mysql]# touch d [[email protected] mysql]# ls a c d #server [[email protected] rsync-mysql]# ls a b c #client [[email protected] mysql]# rsync --password-file=/etc/rsyncd.passwd -az --delete ./ [email protected]::mysql #server [[email protected] rsync-mysql]# ls a c d
同步下载:(数据一致性)
#server [[email protected] rsync-mysql]# rm -f c [[email protected] rsync-mysql]# touch g [[email protected] rsync-mysql]# ls a d g #client [[email protected] mysql]# ls a c d [[email protected] mysql]# rsync --password-file=/etc/rsyncd.passwd -az --delete [email protected]::mysql ./ [[email protected] mysql]# ls a d g
linux内核从2.6.13起开始支持inotify,通过inotify可以监控文件系统中添加 删除修改移动等各种事件。inotify是一种事件机制,它为应用程序文件系统提供实时响应事件的机制,相比cron的轮询机制相比,inotify资源消耗更低。
下载地址:
http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
安装:
[[email protected] inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14 [[email protected] inotify-tools-3.14]# make && make install [[email protected] local]# ln -sv /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools
[[email protected] bin]# cd /usr/local/inotify-tools/bin [[email protected] bin]# ls inotifywait inotifywatch #inotifywait 在被监控的文件或目录上等待特定文件系统事件(open,close,delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用 #inotifywatch 收集被监控的文件系统使用度统计数据,指文件系统事件发生的次数统计 inotifywait -r|--recursive 递归查询目录 q |--quiet 安静模式,尽量输出少 -m |--monitor 始终保持事件监听状态 --excludei 排除文件或者目录时,不区分大小写 --timefmt 指定时间输出的格式 --format 指定命令执行时的信息输出格式,%T为前面的时间格式 -e 事件
监控:(一般监控这三项即可 删除delete,创建create,修改close_write)
[[email protected] bin]# ./inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e delete,create,close_write /backup/mysql/ #另起窗口 在/backup/mysql/ 下创建文件 [[email protected] mysql]# touch c [[email protected] bin]# ./inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e delete,create,close_write /backup/mysql/ 28/05/16 12:04 /backup/mysql/c 28/05/16 12:04 /backup/mysql/c #触发两次 create close_write #delete [[email protected] mysql]# rm -f a [[email protected] bin]# ./inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e delete,create,close_write /backup/mysql/ 28/05/16 12:04 /backup/mysql/c 28/05/16 12:04 /backup/mysql/c 28/05/16 12:06 /backup/mysql/a
[[email protected] scripts]# vim inotify.sh #!/bin/bash inotify=/usr/local/inotify-tools/bin/inotifywait src=/backup/mysql/ $inotify -mrq --format ‘%w%f‘ -e create,close_write,delete $src |while read file do cd $src && rsync -az $src --delete [email protected]::mysql --password-file=/etc/rsyncd.passwd >/dev/null done
[[email protected] scripts]# chmod +x inotify.sh [[email protected] scripts]# ./inotify.sh & [[email protected] scripts]# jobs [1]+ Running ./inotify.sh & [[email protected] scripts]# kill %1 [[email protected] scripts]# jobs [1]+ 已终止 ./inotify.sh
#server [[email protected] mysql]# /scripts/inotify.sh & [[email protected] mysql]# pwd /backup/mysql [[email protected] mysql]# ls c d g kk ll #client [[email protected] rsync-mysql]# ls c d g kk ll #server服务器创建文件 [[email protected] mysql]# touch pp [[email protected] mysql]# ls c d g kk ll pp #client [[email protected] rsync-mysql]# ls c d g kk ll pp #上传完成 #server [[email protected] mysql]# rm -f c d #cilent [[email protected] mysql]# ls g kk ll pp #可删除
加入开机启动
echo ‘/bin/sh /scripts/inotify.sh &‘ >> /etc/rc.local
[[email protected] mysql]# ps -ef |grep inotify.sh root 126119 1 0 12:21 ? 00:00:00 /bin/bash /scripts/inotify.sh root 126121 126119 0 12:21 ? 00:00:00 /bin/bash /scripts/inotify.sh
以上是关于基于rsync+inotify实现文件实时同步的主要内容,如果未能解决你的问题,请参考以下文章