阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

144次阅读
没有评论

共计 8239 个字符,预计需要花费 21 分钟才能阅读完成。

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

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计8239字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中