rsync + inotify实时同步项目

rsync + inotify实时同步项目

同步脚本,对本地目录下所有内容处理。

在rsync服务器上daemon上配置:(192.168.4.5)

[root@localhost 桌面]# /etc/init.d/sshd status

openssh-daemon (pid  1970) 正在运行..

[root@localhost 桌面]# mkdir /share

[root@localhost 桌面]# useradd -s /sbin/nologin rsync

[root@localhost 桌面]# chown -R rsync.rsync /share/

[root@localhost 桌面]# ls -ld /share/

drwxr-xr-x. 2 rsync rsync 4096 5月  24 08:23 /share/

[root@localhost 桌面]# vim /etc/rsyncd.conf

uid=rsync

gid=rsync

max connections=200

timeout=300

pid file=/var/run/rsyncd.pid

lock file=/var/run/rsyncd.lock

log file=/var/log/rsyncd.log

[share]

comment this is student homework

path=/share

read only=no

auth users=shi

secrets file=/etc/password.txt

list=false

hosts allow=192.168.4.0/24

hosts deny=10.10.10.0/24

wq

[root@localhost 桌面]# echo "shi:789" > /etc/password.txt

[root@localhost 桌面]# cat /etc/password.txt

shi:789

[root@localhost 桌面]# chmod -R 600 /etc/password.txt

[root@localhost 桌面]# ll /etc/password.txt

-rw-------. 1 root root 8 5月  24 08:38 /etc/password.txt

[root@localhost 桌面]# rsync --daemon

[root@localhost 桌面]# netstat -anptu |grep 873

tcp        0      0 0.0.0.0:873             0.0.0.0:*              LISTEN      26794/rsync         

tcp        0      0 :::873                  :::*                   LISTEN      26794/rsync         

[root@localhost 桌面]# ls /share/

空                                                //客户端上传前为空

[root@localhost 桌面]# ls /share/

test.txt                                          //客户端上传后有内容

inotify的客户端(192.168.4.205)     //用同步脚本,在本地/backup目录写数据同步到服务器

[root@localhost 桌面]# echo "789" > /etc/pass.txt

[root@localhost 桌面]# cat /etc/pass.txt

789

[root@localhost 桌面]# chmod 600 /etc/pass.txt

[root@localhost 桌面]# ll /etc/pass.txt

-rw-------. 1 root root 4 5月  24 08:43 /etc/pass.txt

[root@localhost 桌面]# mkdir /test

[root@localhost 桌面]# echo "test" > /test/test.txt

[root@localhost 桌面]# rsync -av /test/ shi@192.168.4.5::share --password-file=/etc/pass.txt //测试推送

sending incremental file list

./

test.txt

sent 90 bytes  received 30 bytes  240.00 bytes/sec

total size is 5  speedup is 0.04

[root@localhost 桌面]# yum -y install gcc gcc-c++

[root@localhost 桌面]# ls

inotify-tools-3.14.tar.gz

[root@localhost 桌面]# tar -zxf inotify-tools-3.14.tar.gz

[root@localhost 桌面]# ls

inotify-tools-3.14  inotify-tools-3.14.tar.gz                             //把inotify源码包传上来

[root@localhost 桌面]# cd inotify-tools-3.14

[root@localhost inotify-tools-3.14]# ls

aclocal.m4    config.h.in   COPYING     libinotifytools  man      src

AUTHORS       config.sub    depcomp     ltmain.sh        missing

ChangeLog     configure     INSTALL     Makefile.am      NEWS

config.guess  configure.ac  install-sh  Makefile.in      README

[root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify

[root@localhost inotify-tools-3.14]# make && make install

[root@localhost inotify-tools-3.14]# cd -

/root/桌面

[root@localhost 桌面]# ls /usr/local/inotify/

bin  include  lib  share

[root@localhost 桌面]# /usr/local/inotify/bin/inotifywait --help  可以查看帮助文档命令,常用选项如下:

-r              //递归查询目录

-q              //打印很少的信息,仅仅打印监控事件的信息

-m              //永久保持事件监听状态

--excludei      //排除文件或目录时,不区分大小写,别掉了后面的i

--timefmt       //指定事件输出的格式

--format        //打印使用指定的输出类似格式字符串

-e              //通过此参数可以指定要监控的事件,事件如下:

常见-e下面的事件:

access             //文件或目录被读取

modify             //文件或目录被修改

attrib             //文件或目录属性被改变

close              //文件或目录封闭,无论读/写模式

open               //文件或目录被打开

moved_to           //文件或目录被移动至另外一个目录

moved_from         //文件或目录从另一个目录移动到当前目录

move               //文件或目录被移动另一个目录或从另一个目录移动到当前目录

create             //文件或目录被创建在当前目录

delete             //文件或目录被删除   

unmount            //文件系统被卸载

root@localhost 桌面]# mkdir /backup                      //创建要监听的目录

[root@localhost 桌面]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete /backup/    //在一个终端上,监听/backup目录(仅监听在下面创建或删除文件时)

空。                                                 //未在/backup目录创建文件或删除文件时候,现在为空

[root@localhost 桌面]# cd /backup/                   //在另一个终端下,在/backup目录下创建或删除文件

[root@localhost backup]# ls

[root@localhost backup]# mkdir a

[root@localhost backup]# touch a.txt

[root@localhost backup]# cat a.txt

[root@localhost backup]# rm -f a.txt

[root@localhost 桌面]# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete /backup/                  //此命令仅监听在/backup目录创建或删除文件

24/05/16 11:19 /backup/a

24/05/16 11:19 /backup/a.txt

24/05/16 11:20 /backup/a.txt               

[root@localhost 桌面]# /usr/local/inotify/bin/inotifywait -mrq  --format '%w%f' -e create,delete /backup/

空                                                    //在一个终端简化测试

[root@localhost 桌面]# cd /backup/                    //在另一个终端下,在/backup目录下创建或删除文件

[root@localhost backup]# ls

[root@localhost backup]# mkdir a

[root@localhost backup]# touch a.txt

[root@localhost backup]# rm -f a.txt

[root@localhost 桌面]# /usr/local/inotify/bin/inotifywait -mrq  --format '%w%f' -e create,delete /backup/

/backup/a                                           /此命令仅监听在/backup目录创建或删除文件

/backup/a.txt

/backup/a.txt

[root@localhost 桌面]# vim /shell/inotify.sh  //写实时同步脚本(让/backup目录和服务器share同步)

#!/bin/bash

host=192.168.4.5

src=/backup

dst=share

user=shi

rsync_passfile=/etc/pass.txt

inotify_home=/usr/local/inotify

${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \

| while read line                      //注意别忘了最前面的管道‘|’,把上面输出的文件交个while循环                                        

do

cd $src && rsync -av -R --delete ./ --timeout=100 $user@$host::$dst --password-file=${rsync_passfile}

done

exit 0

wq

[root@localhost 桌面]# sh /shell/inotify.sh  &

[root@localhost 桌面]# cd /backup/

[root@localhost backup]# ls

[root@localhost backup]# touch {a..z}

[root@localhost backup]# ls

a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

rsync服务器上daemon上)查看(192.168.4.5)   //服务器跟着增加文件

[root@localhost 桌面]# ls /share/

a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

inotify的客户端(192.168.4.205) //用同步脚本删除本地/backup内容,并同步到服务器

[root@localhost 桌面]# sh /shell/inotify.sh  &

[root@localhost backup]# rm -f *

[root@localhost backup]# ls

rsync服务器上daemon上)查看(192.168.4.5)   //服务器跟着删除文件

[root@localhost 桌面]# ls /share/

注意:系统默认的inotify参数较小,工作中为了提高效率应把参数调大一点

系统默认的inotify参数如下:(工作中应该调大)

[root@localhost 桌面]# cat /proc/sys/fs/inotify/max_user_watches

8192

[root@localhost 桌面]# cat /proc/sys/fs/inotify/max_queued_events

16384

[root@localhost 桌面]# cat /proc/sys/fs/inotify/max_user_instances

128

[root@localhost 桌面]# cd /proc/sys/fs/inotify/

[root@localhost inotify]# cat *

16384

128

8192

工作中使用的inotify参数如下:(提高效率)

[root@localhost 桌面]# cat /proc/sys/fs/inotify/max_user_watches

50000000

[root@localhost 桌面]# cat /proc/sys/fs/inotify/max_queued_events

327679

inotify支持用户创建文件和支持并发数范围:

文件大小为10-100k,并发200左右,最大不超过200(10-100k的文件)

inotify的优缺点:

优点:实时同步

缺点:

  1. 并发不能大于200个文件(10-100k的文件)
  2. 我们写的脚本,每次都是全部推送一次。

如果对运维课程感兴趣,可以在b站上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维实战课程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值