zoukankan      html  css  js  c++  java
  • centos文件实时同步inotify+rsync

    我的应用场景是重要文件备份

    端口:873,备份端打开即可

    下载地址:https://rsync.samba.org/ftp/rsync/src/

    服务端和客户端要保持版本一致

    网盘链接:https://pan.baidu.com/s/1wTsj0cCfXRcREnbzeIviuQ

    备份端

    1、安装rsync:

    ...

    2、设置密码文件

    vim /etc/rsync.pas
    
    cjh:123456  #格式 用户名:密码   这里的用户名和密码跟系统没有关系的

    配置文件

    vi /etc/rsyncd.conf  #文件不一定有,可以自己创建
    
    
    # /etc/rsyncd: configuration file for rsync daemon mode
    
    # See rsyncd.conf man page for more options.
    
    # configuration example:
    
    # uid = nobody
    # gid = nobody
    # 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

    加上我的配置

    uid =root #root是必须的,同步时权限问题
    gid =root 
    use chroot = yes
    max connections = 1
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsyncd.lock
    log file = /var/log/rsyncd.log
    transfer logging = yes
    log format = %t %a %m %f %b
    timeout = 300
    [test]
    read only =no #必须是no
    path = /root/      #同步目录路径,要注意同步过来的时候模块名会拼接,比如/root/test/ 这里就只需要填/root/
    comment = xxx       #可选,无太大作用
    auth users =cjh        #用户名,注意跟系统的用户密码没关系
    secrets file =/etc/rsync.pas #密码文件
    hosts allow = ip1,ip2  #源文件端ip

    启动

    which rsync
    /xx/rsync
    
    #后台启动
    /xx/rsync --daemon

    ps -ef | grep rsync #看看服务是否正常运行

    源文件端

    1、安装启动rsync+inotify

    注意密码文件格式跟备份端不一样,比如这里直接写123456就可以了,不需要用户名

    特别介绍一下就是:rsync有六种不同的工作模式

    1、rsync [OPTION]... SRC DEST
    2、rsync [OPTION]... SRC [USER@]HOST:DEST
    3、rsync [OPTION]... [USER@]HOST:SRC DEST
    4、rsync [OPTION]... [USER@]HOST::SRC DEST
    5、rsync [OPTION]... SRC [USER@]HOST::DEST
    6、rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

    1)拷贝本地文件。(没用过,不知道效果怎样)

    2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。(如果没有做免密登录,每次都要输密码)

    3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。(如果没有做免密登录,每次都要输密码)

    4)从远程rsync服务器中拷贝文件到本地机。(定期备份可以用这个)

    5)从本地机器拷贝文件到远程rsync服务器中。(实时备份用这个)

    6)列远程机的文件列表。(没用过)

    2、一次性同步测试

    rsync -av /root/test/ cjh@备份端IP::test --password-file=/etc/rsync.pas --bwlimit=100

    test是客户端定义的模块,/root/test2 是客户端的目录,密码文件跟服务端不同的是只需要填密码,--bwlimit是网速的限制,--delete是完全同步目录文件

    实时同步

     安装inotify-tools,下载的网站打不开,我就直接用yum方式安装

    yum install -y inotify-tools

    实时同步脚本(attrib指文件的属性,比如只读)

    #!/bin/bash
    host1=IP  #备份服务器IP
    src=/root/test/  #同步目录最好保持一致
    dst1=test   #模块名称
    user1=cjh   #用户名
    /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e create,modify,attrib $src 
    | while read files
    do
            rsync -av $src $user1@$host1::$dst1 --password-file=/etc/rsync.pas --bwlimit=100
            echo "${files} was rsynced."
    done

    启动脚本

    nohup sh run.sh  > run.out & 2>&1 &

    测试通过

    注意点:

    1、chmod 600 /etc/rsync.pas,两端都需要做这个操作

    2、大文件的同步是很慢的,同步的时候卡住注意是不是文件太大了

    3、上线前认真做好测试和普通方式的备份,以及尽量不要使用--delete,防止数据丢失

    4、/etc/rsyncd.conf配置文件中一个注释都不能写,上面只是为了方便表达参数含义!

    5、当文件数量超多的时候,同步会有一点延迟,大概是几分钟

     转载请注明博客出处:http://www.cnblogs.com/cjh-notes/

  • 相关阅读:
    hibernate 字段名最好不要使用数据库的保留字
    Tomcat Java内存溢出 PermGen space 解决方案
    jsp关于include html、jsp等文件出现乱码问题的解决方案
    hibernate annotation注解 columnDefinition用法
    hibernate annotation注解 主键ID自增长
    Java将整个文件夹里的文本中的字符串替换成另外一个字符串(可用于项目复制,变成另一个项目)
    FreeMarker 乱码解决方案 生成静态html文件
    Java发送post请求
    编程式导航
    vue-router 的重定向-redirect
  • 原文地址:https://www.cnblogs.com/cjh-notes/p/10802807.html
Copyright © 2011-2022 走看看