你好,游客 登录 注册 搜索
背景:
阅读新闻

基于rsync+inotify实现文件实时同步

[日期:2016-06-01] 来源:Linux社区  作者:marvin89 [字体: ]

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: 显示如何执行压缩和传输

复制前先测试:

[root@martin ~]# ls
anaconda-ks.cfg  d1  d2  install.log  install.log.syslog
[root@martin ~]# rsync install.log d2 -n
[root@martin ~]# 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命令使用中,如果源参数的末尾有斜线,就会复制指定目录的内容,而不复制目录本身;没有斜线,则会复制目录本身;目标参数末尾的斜线没有作用
源数据后面没有/测试
[root@martin ~]# 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
[root@martin ~]# ls d2/
d1

源数据后面有/ 测试:
[root@martin ~]# 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
[root@martin ~]# ls d2
a  a.bin.sql  a.sql  all.sql  d1  kk

基于ssh远程推送:
[root@martin d2]# rsync -e "ssh -p6789" -r ../d1  root@sherry:~/  --progress
 
[root@sherry ~]# ls
a  a.sql  anaconda-ks.cfg  d1  install.log  install.log.syslog

基于ssh远程拉取:
[root@martin ~]#  rsync -e "ssh -p6789" -rv  root@sherry:~/d3  . --progress 
root@sherry'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
[root@martin ~]# ls
anaconda-ks.cfg  d1  d2  d3  install.log  install.log.syslog

安装:
服务器:sherry
客户端:martin
[root@sherry ~]# yum install xinetd rsync

配置文件:
配置文件为/etc/rsyncd.conf,获取帮助的方式:man rsyncd.conf
定义一个全局配置和多个rsync共享配置

[root@sherry ~]# 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;

[root@sherry 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

[root@sherry rsync-mysql]# pwd
/mydata/backup/rsync-mysql
[root@sherry backup]# chown sherry.sherry rsync-mysql/  #用setfacl  数据一致性时候会报错

添加账号密码:
[root@sherry rsync]# cat /etc/rsyncd.passwd 
tom:222222
[root@sherry rsync]# chmod 600 /etc/rsyncd.passwd

启动端口:
[root@sherry rsync]# chkconfig xinetd on
[root@sherry rsync]# service xinetd restart
[root@sherry rsync]# chkconfig rsync on
[root@sherry rsync]# ss -lntup|grep 873
tcp    LISTEN    0      64                    :::873                  :::*      users:(("xinetd",121111,5))

测试:
push 手动输入密码:
#client
[root@martin mysql]# touch a
[root@martin mysql]# ls
a
[root@martin mysql]# rsync  a  tom@sherry::mysql
Password: 
#server
[root@sherry rsync-mysql]# ll
total 0
-rw-r--r-- 1 sherry sherry 0 May 28 10:01 a

push 用文件代替密码:
#client
[root@martin ~]#  vim /etc/rsyncd.passwd
222222
[root@martin ~]# chmod 600 /etc/rsyncd.passwd 
[root@martin mysql]# touch b
[root@martin mysql]# rsync  --password-file=/etc/rsyncd.passwd  b tom@sherry::mysql
#server
[root@sherry 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

拉取:
[root@martin mysql]# rm -fr *
[root@martin mysql]# rsync  --password-file=/etc/rsyncd.passwd  tom@sherry::mysql/* .
[root@martin mysql]# ls
a  b

增量上传:(上传服务器端没有的文件)
#client
[root@martin mysql]# rm -f *
[root@martin mysql]# ls
[root@martin mysql]# touch a b
[root@martin mysql]# ls
a  b
[root@martin mysql]# rsync  --password-file=/etc/rsyncd.passwd -az    ./  tom@sherry::mysql 
#server
[root@sherry rsync-mysql]# rm -f *
[root@sherry rsync-mysql]# ls
a  b
 
#client
[root@martin mysql]# rm -f b
[root@martin mysql]# touch c
[root@martin mysql]# ls
a  c
[root@martin mysql]# rsync  --password-file=/etc/rsyncd.passwd -az    ./  tom@sherry::mysql 
#server
[root@sherry rsync-mysql]# ls
a  b  c

同步上传:(数据一致性)
12345678910111213 #client
[root@martin mysql]# touch d
[root@martin mysql]# ls
a  c  d
#server
[root@sherry rsync-mysql]# ls
a  b  c
 
#client
[root@martin mysql]# rsync  --password-file=/etc/rsyncd.passwd -az  --delete  ./  tom@sherry::mysql 
#server
[root@sherry rsync-mysql]# ls
a  c  d

同步下载:(数据一致性)
#server
[root@sherry rsync-mysql]# rm -f c
[root@sherry rsync-mysql]# touch g
[root@sherry rsync-mysql]# ls
a  d  g
#client
[root@martin mysql]# ls
a  c  d
[root@martin mysql]# rsync  --password-file=/etc/rsyncd.passwd -az  --delete    tom@sherry::mysql  ./
[root@martin 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

安装:
[root@martin inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
[root@martin inotify-tools-3.14]# make && make install
[root@martin local]# ln -sv /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools

[root@martin bin]# cd /usr/local/inotify-tools/bin
[root@martin 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)
[root@martin bin]# ./inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /backup/mysql/ 
#另起窗口 在/backup/mysql/ 下创建文件
[root@martin mysql]# touch c
[root@martin 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
[root@martin mysql]# rm -f a
[root@martin 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

[root@martin 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 tom@sherry::mysql \
  --password-file=/etc/rsyncd.passwd  >/dev/null
done

[root@martin scripts]# chmod +x inotify.sh 
[root@martin scripts]# ./inotify.sh  &
[root@martin scripts]# jobs
[1]+  Running                ./inotify.sh &
[root@martin scripts]# kill %1
[root@martin scripts]# jobs
[1]+  已终止              ./inotify.sh

#server
[root@martin mysql]# /scripts/inotify.sh  &
[root@martin mysql]# pwd
/backup/mysql
[root@martin mysql]# ls
c  d  g  kk  ll
#client
[root@sherry rsync-mysql]# ls
c  d  g  kk  ll
#server服务器创建文件
[root@martin mysql]# touch pp
[root@martin mysql]# ls
c  d  g  kk  ll  pp
#client
[root@sherry rsync-mysql]# ls
c  d  g  kk  ll  pp  #上传完成
 
#server
[root@martin mysql]# rm -f c d
#cilent
[root@martin mysql]# ls
g  kk  ll  pp    #可删除

加入开机启动
echo '/bin/sh  /scripts/inotify.sh  &' >> /etc/rc.local

[root@martin 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实现文件备份同步详解   http://www.linuxidc.com/Linux/2014-09/106967.htm

利用inotifywait监控主机文件和目录 http://www.linuxidc.com/Linux/2013-03/81075.htm

利用inotify+rsync实现Linux文件批量更新 http://www.linuxidc.com/Linux/2012-01/52132.htm

inotify-tools+rsync实时同步文件安装和配置 http://www.linuxidc.com/Linux/2012-06/63624.htm

rsync同步完整配置 http://www.linuxidc.com/Linux/2013-06/85781.htm

CentOS 6.5下Rsync远程同步 http://www.linuxidc.com/Linux/2014-05/101084.htm

Linux文件夹对比并提取的差分文件技巧-rsync的妙用 http://www.linuxidc.com/Linux/2016-02/128307.htm

Rsync 的详细介绍请点这里
Rsync 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-06/131942.htm

linux
相关资讯       rsync+inotify 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款