zoukankan      html  css  js  c++  java
  • Centos7 rsync+inotify实现实时同步更新

    inotify slave部署  

       把master上指定文件下载到本地的主机指定目录

     yum install rsync –y
     [root@localhost ~]# useradd rsync -s /sbin/nologin -M
     [root@localhost ~]# mkdir -p /home/yxh/back
     [root@localhost ~]# chown rsync.rsync /home/yxh/back/
    
      echo rsync_backup:yxh >>/etc/rsync.password
       rsync_backup 为用户名
       yxh  为密码
    
    [root@localhost ~]# chmod 600 /etc/rsync.password
    [root@localhost ~]# rsync --daemon
    [root@localhost ~]# ss -tunlp | grep rsync
    tcp    LISTEN     0      5         *:873                   *:*                   users:(("rsync",pid=12133,fd=4))
    tcp    LISTEN     0      5        :::873                  :::*                   users:(("rsync",pid=12133,fd=5))
    
    重启rsync
    [root@localhost back]# ps -ef | grep rsync
    root      13472      1  0 20:10 ?        00:00:00 rsync --daemon
    root      14185   9059  0 20:19 pts/1    00:00:00 grep --color=auto rsync
    [root@localhost back]# kill -9 13472
    [root@localhost back]# ps -ef | grep rsync
    root      14253   9059  0 20:19 pts/1    00:00:00 grep --color=auto rsync
    [root@localhost back]# rsync --daemon
    failed to create pid file /var/run/rsyncd.pid: File exists
    [root@localhost back]# rm -fr /var/run/rsyncd.pid
    [root@localhost back]# ls
    [root@localhost back]# rsync --daemon
    安装流程
    [root@localhost etc]# vi rsyncd.conf
    
    # /etc/rsyncd: configuration file for rsync daemon mode
    
    # See rsyncd.conf man page for more options.
    
    # configuration example:
    
    uid = rsync
    gid = rsync
    use chroot = no
    max connections = 400
    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
    
    [backup]
      path = /home/yxh/back/
      ignore errors
      read only = no
      write only = no
      list = false
      fake super = yes
      auth users = rsync_backup
      secrets file = /etc/rsync.password
    rsyncd.conf

    inotify master部署

          master主机上的文件发生变化的时候     slave主机会自动进行同步变化的文件

    [root@node2 ~]# yum install rsync –y
    [root@node2 ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
    [root@node2 ~]# tar zxf inotify-tools-3.14.tar.gz
    [root@node2 ~]# cd inotify-tools-3.14
    [root@node2 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
    [root@node2 inotify-tools-3.14]# mkdir -p /home/yxh/back/
    [root@node2 inotify-tools-3.14]# echo "yxh" >/etc/rsync.password
    [root@node2 inotify-tools-3.14]# chmod 600 /etc/rsync.password
    
    
    测试同步
    [root@node2 back]# ls
    test.txt
    [root@node2 back]#  rsync  -avz /home/yxh/back/test.txt rsync_backup@192.168.11.175::backup --password-file=/etc/rsync.password
    
    sending incremental file list
    test.txt
    
    sent 100 bytes  received 43 bytes  286.00 bytes/sec
    total size is 7  speedup is 0.05
    安装步骤
    [root@node2 local]# vi inotify.sh
    
    #!/bin/bash
    
    host01=192.168.11.175
    user=rsync_backup
    rsync_passfile=/etc/rsync.password
    inotify_home=/usr/local/inotify
    
    #judge
    if [ ! -e "$src" ] 
    || [ ! -e "${rsync_passfile}" ] 
    || [ ! -e "${inotify_home}/bin/inotifywait" ] 
    || [ ! -e "/usr/bin/rsync" ];
    then
    echo "Check File and Folder"
    exit 9
    fi
    
    ${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src 
    | while read file
    do
    
    #  rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
    cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
    done
    exit 0
    同步脚本

    3.执行脚本实现自动更新

       [root@node2 local]# sh inotify.sh &       这种方式虽然可以实现脚本在后台运行 但是一旦关闭终端便会失效

       nohup sh inotify.sh >run.log 2>&1 &      采取这种执行方式 即使关闭终端也可以生效 只要不重启系统即可一直在后台运行

      

    [root@node2 local]# sh inotify.sh &
    [1] 32260
    
    在master上添加一个test2.txt文件保存
    [root@node2 back]# ls
    test.txt
    [root@node2 back]# vi test2.txt
    [root@node2 back]# ls
    test2.txt  test.txt
    
    
    然后到slave节点上查看指定目录
    test2.txt被自动同步到本地来
    [root@localhost back]# ls
    test2.txt  test.txt
    [root@localhost back]# vi test2.txt
    
    
    加入开机启动
    # echo  "/bin/bash  /home/yxh/inotify.sh &" >>/etc/rc.local
    View Code

    NFS实现映射远程磁盘目录

    nfs安装
       在两台机器上安装nsf 、 portmap
       yum  install  nfs-utils  portmap
     
    88配置
      88机器上的/uploaddir目录映射到89本地的/uploaddir目录
      88编辑配置文件
      [root]# vi /etc/exports
      /uploaddir/ 10.199.142.89(rw,sync,no_root_squash)
    
      systemctl start rpcbind
      systemctl start nfs
    
      89配置
       systemctl start rpcbind
       systemctl start nfs
    
       mkdir /uploaddir
       mount -t nfs 10.199.142.88:/uploaddir/  /uploaddir/
    
       mount
        10.199.142.88:/uploaddir on /uploaddir type nfs4 
    nfs安装配置

    线上案例

    vi /etc/rsync.password
    
    root:rootpass
    
    
    chmod 600 /etc/rsync.password
    客户端配置用户名和密码
    root@9 etc]# vi 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 = 400
    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
    
    uid = root
    gid = root
    use chroot = no
    max connections = 10
    strict modes = yes
    hosts allow = 88
    port = 873
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
    
    
    [backup]
    path = /deploydir
    ignore errors
    read only = no
    write only = no
    list = false
    fake super = yes
    secrets file = /etc/rsync.password
    客户端rsyncd.conf
    [root@88 inotify-tools-3.14]# echo "rootpass" >/etc/rsync.password
    [root@88 inotify-tools-3.14]#  chmod 600 /etc/rsync.password
    
    
    [root@88 uploaddir]# rsync -avz /uploaddir/22.txt root@10.89::backup --password-file=/etc/rsync.password
    服务端配置和测试
    [root@88 ~]# vi inotify.sh
    
    #!/bin/bash
    src=/uploaddir
    host01=1.89
    host02=1.90
    user=root
    dst=backup
    rsync_passfile=/etc/rsync.password
    inotify_home=/usr
    
    #judge
    if [ ! -e "$src" ] 
    || [ ! -e "${rsync_passfile}" ] 
    || [ ! -e "${inotify_home}/bin/inotifywait" ] 
    || [ ! -e "/usr/bin/rsync" ];
    then
    echo "Check File and Folder"
    exit 9
    fi
    
    ${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src 
    | while read file
    do
    
    cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
    
    cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host02::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
    done
    exit 0
    后台服务脚本
    [root@-88]# cat /etc/rsync.password 
    rootpass
    
    [root@88]# cat /etc/rsync/rsync.password 
    root:rootpass
    
    [root@88]# cat /etc/rsyncd.conf 
    pid file = /var/run/rsyncd.pid
    uid = nobody
    gid = nobody
    use chroot = no
    log format = %t %a %m %f %b
    syslog facility = local3
    timeout = 300
    address = 10.8
    
    [backup]
    uid =root
    gid =root
    use chroot = yes
    max connections = 400
    path = /deploydir
    ignore errors
    read only = no
    write only = no
    list = false
    fake super = yes
    secrets file = /etc/rsync/rsync.password
    同一主机既是服务端也是客户端
  • 相关阅读:
    Sql2000存储过程分页
    扇贝网页记单词插件
    notepad++正则替换保留原内容
    给 QuickTime 播放器添加快进x秒,快退x秒
    谷歌浏览器隐私设置错误 NET::ERR_CERT_AUTHORITY_INVALID 攻击者可能会试图从 xx 窃取您的信息(例如:密码、通讯内容或信用卡信息)。
    Java实体映射工具MapStruct 与BeanUtils性能比较
    阿里DDD项目最佳实践-COLA 架构总览
    Java实体映射工具MapStruct详解
    Martin Fowler大神
    领域模型的核心本质是什么?
  • 原文地址:https://www.cnblogs.com/yxh168/p/11396717.html
Copyright © 2011-2022 走看看