rsync+inotify实时同步

rsync+inotify

效果

[root@node2 test]# ../inotifybck.sh
sending incremental file list
./
dfsdfsdfadg
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=3/5)

sent 198 bytes  received 46 bytes  488.00 bytes/sec
total size is 358,772  speedup is 1,470.38
sending incremental file list

sent 155 bytes  received 20 bytes  350.00 bytes/sec
total size is 358,772  speedup is 2,050.13
sending incremental file list

sent 155 bytes  received 20 bytes  350.00 bytes/sec
total size is 358,772  speedup is 2,050.13
sending incremental file list
./
dfsdfsdfadgasdf
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=3/6)

sent 218 bytes  received 46 bytes  528.00 bytes/sec
total size is 358,772  speedup is 1,358.98
sending incremental file list

sent 175 bytes  received 20 bytes  390.00 bytes/sec
total size is 358,772  speedup is 1,839.86
sending incremental file list

sent 175 bytes  received 20 bytes  390.00 bytes/sec
total size is 358,772  speedup is 1,839.86
sending incremental file list
./
t
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/7)

sent 234 bytes  received 46 bytes  560.00 bytes/sec
total size is 358,772  speedup is 1,281.33
sending incremental file list

sent 191 bytes  received 20 bytes  422.00 bytes/sec
total size is 358,772  speedup is 1,700.34
sending incremental file list

sent 191 bytes  received 20 bytes  422.00 bytes/sec
total size is 358,772  speedup is 1,700.34
sending incremental file list
./
dfsdfsdfadg
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=5/7)
dfsdfsdfadgasdf
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=4/7)
ff
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=3/7)
gg
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=2/7)
inotify-tools-3.14.tar.gz
        358,772 100%   25.91MB/s    0:00:00 (xfr#5, to-chk=1/7)
t
              0 100%    0.00kB/s    0:00:00 (xfr#6, to-chk=0/7)

sent 359,201 bytes  received 141 bytes  718,684.00 bytes/sec
total size is 358,772  speedup is 1.00
sending incremental file list

sent 191 bytes  received 20 bytes  422.00 bytes/sec
total size is 358,772  speedup is 1,700.34


server

安装rsyncd

# 安装rsyncd
[root@node1 test]# rpm -ql rsync
/etc/rsyncd.conf
/etc/sysconfig/rsyncd
/usr/bin/rsync
/usr/lib/systemd/system/rsyncd.service
/usr/lib/systemd/system/rsyncd.socket
/usr/lib/systemd/system/rsyncd@.service
/usr/share/doc/rsync-3.1.2
/usr/share/doc/rsync-3.1.2/COPYING
/usr/share/doc/rsync-3.1.2/NEWS
/usr/share/doc/rsync-3.1.2/OLDNEWS
/usr/share/doc/rsync-3.1.2/README
/usr/share/doc/rsync-3.1.2/support
/usr/share/doc/rsync-3.1.2/support/Makefile
/usr/share/doc/rsync-3.1.2/support/atomic-rsync
/usr/share/doc/rsync-3.1.2/support/cvs2includes
/usr/share/doc/rsync-3.1.2/support/deny-rsync
/usr/share/doc/rsync-3.1.2/support/file-attr-restore
/usr/share/doc/rsync-3.1.2/support/files-to-excludes
/usr/share/doc/rsync-3.1.2/support/git-set-file-times
/usr/share/doc/rsync-3.1.2/support/instant-rsyncd
/usr/share/doc/rsync-3.1.2/support/logfilter
/usr/share/doc/rsync-3.1.2/support/lsh
/usr/share/doc/rsync-3.1.2/support/lsh.sh
/usr/share/doc/rsync-3.1.2/support/mapfrom
/usr/share/doc/rsync-3.1.2/support/mapto
/usr/share/doc/rsync-3.1.2/support/mnt-excl
/usr/share/doc/rsync-3.1.2/support/munge-symlinks
/usr/share/doc/rsync-3.1.2/support/rrsync
/usr/share/doc/rsync-3.1.2/support/rsync-no-vanished
/usr/share/doc/rsync-3.1.2/support/rsync-slash-strip
/usr/share/doc/rsync-3.1.2/support/rsyncstats
/usr/share/doc/rsync-3.1.2/support/savetransfer.c
/usr/share/doc/rsync-3.1.2/tech_report.tex
/usr/share/man/man1/rsync.1.gz
/usr/share/man/man5/rsyncd.conf.5.gz
[root@node1 test]#
[root@node1 test]#

配置rsyncd

[root@node1 test]# cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
exclude = lost+found/
transfer logging = yes
timeout = 900
ignore nonreadable = yes
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# [ftp]
#        path = /home/ftp
#        comment = ftp export area

[test]
# 路径
        path = /home/test
        comment = test export area
# 用户        
        auth users = root
        read only = no
# 密码        
        secrets file = /etc/rsyncd.secrets
[root@node1 test]#
[root@node1 test]# cat /etc/rsyncd.secrets
root:123456
[root@node1 test]#
[root@node1 test]#
[root@node1 test]#
[root@node1 test]# ls -al /etc/rsyncd.secrets
-rw------- 1 root root 12 Mar  8 19:21 /etc/rsyncd.secrets
[root@node1 test]# 600 哦

client

安装inotify

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz --no-check-certificate
tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure 
make && make install

[root@node2 home]# ll /usr/local/bin/
total 116
-rwxr-xr-x 1 root root 60720 Mar  8 18:14 inotifywait
-rwxr-xr-x 1 root root 55088 Mar  8 18:14 inotifywatch
[root@node2 home]#
[root@node2 home]# cat /etc/rsyncd.secrets
123456
[root@node2 home]#
[root@node2 home]# cat inotifybck.sh
#!/bin/bash
src=/home/test/
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src | while read file
do
rsync -vzrtopg --delete --progress $src root@192.168.11.151::test --password-file=/etc/rsyncd.secrets
echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
done

server+client

server: systemctl start syncd
client: ./inotifybck.sh &
[root@node2 home]# cat /tmp/rsync.log
08/03/21 20:06 /home/test/dfsdfsdfadgCREATE was rsynced
08/03/21 20:06 /home/test/dfsdfsdfadgATTRIB was rsynced
08/03/21 20:06 /home/test/dfsdfsdfadgCLOSE_WRITE,CLOSE was rsynced
08/03/21 20:06 /home/test/dfsdfsdfadgasdfCREATE was rsynced
08/03/21 20:06 /home/test/dfsdfsdfadgasdfATTRIB was rsynced
08/03/21 20:06 /home/test/dfsdfsdfadgasdfCLOSE_WRITE,CLOSE was rsynced
08/03/21 20:07 /home/test/tCREATE was rsynced
08/03/21 20:07 /home/test/tATTRIB was rsynced
08/03/21 20:07 /home/test/tCLOSE_WRITE,CLOSE was rsynced
08/03/21 20:07 /home/test/tATTRIB was rsynced
08/03/21 20:07 /home/test/tCLOSE_WRITE,CLOSE was rsynced
[root@node2 home]#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值