• Ubuntu rsync同步


    >服务器端:Ubuntu 9.10 - 192.168.1.3
    客户端:Ubuntu 10.04 - 192.168.1.73

    我们先来设置一下服务器端的配置

    1.ubuntu系统安装完之后,rsync服务默认不是启动的,我们要修改下面的文件。
    $sudo vi /etc/default/rsync
    RSYNC_ENABLE=true

    2.修改配置文件
    $sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
    我们先来查看一下这个文件
    $sudo cat /etc/rsyncd.conf

    # sample rsyncd.conf configuration file

    # GLOBAL OPTIONS

    #motd file=/etc/motd #登录欢迎信息
    #log file=/var/log/rsyncd #日志文件
    # for pid file, do not use /var/run/rsync.pid if
    # you are going to run rsync out of the init.d script.
    pid file=/var/run/rsyncd.pid

    # 指定rsync发送日志消息给syslog时的消息级别,常见的消息级别是:uth, authpriv, cron, daemon, ftp, kern, lpr, mail, news, security, sys-log, user, uucp, local0, local1, local2, local3,local4, local5, local6和local7。默认值是daemon。 
    #syslog facility=daemon

    #自定义tcp选项,默认是关闭的
    #socket options=

    #以下是模块信息,我们可以创建多个模块
    # MODULE OPTIONS

    [ftp]

            comment = public archive #模块描述
            path = /var/www/pub #需要同步的路径
            use chroot = yes #默认是yes|true,如果为true,那么在rsync在传输文件以前首先chroot到path参数指定的目录下。这样做的原因是实现额 外的安全防护,但是缺点是需要root权限,并且不能备份指向外部的符号连接指向的目录文件。
    #       max connections=10 #最大连接数
            lock file = /var/lock/rsyncd #指定支持max connections参数的锁文件。
    # the default for read only is yes...
            read only = yes #只读选项
            list = yes #客户请求时可用模块时是否列出该模块
            uid = nobody #设定该模块传输文件时守护进程应该具有的uid
            gid = nogroup #设定该模块传输文件时守护进程应具有的gid,此项与uid配合可以确定文件的访问权限
    #       exclude = #用来指定多个由空格隔开的多个模式列表,并将其添加到exclude列表中。这等同于在客户端命令中使用--exclude来指定模式,不过配置文 件中指定的exlude模式不会传递给客户端,而仅仅应用于服务器。一个模块只能指定一个exlude选项,但是可以在模式前面使用"-"和"+"来指定 是exclude还是include
    #       exclude from = #可以指定一个包含exclude模式定义的文件名
    #       include = #与exclude相似
    #       include from = #可以指定一个包含include模式定义的文件名
    #       auth users = #该选项指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块。这里的用户和系统用户没有任何关系。如果"auth users"被设置,那么客户端发出对该模块的连接请求以后会被rsync请求challenged进行验证身份这里使用的 challenge/response认证协议。用户的名和密码以明文方式存放在"secrets file"选项指定的文件中。默认情况下无需密码就可以连接模块(也就是匿名方式)
    #       secrets file = /etc/rsyncd.secrets #该文件每行包含一个username:password对,以明文方式存储,只有在auth users被定义时,此选项才生效。同时我们需要将此文件权限设置为0600
            strict modes = yes #该选项指定是否监测密码文件的权限,如果该选项值为true那么密码文件只能被rsync服务器运行身份的用户访问,其他任何用户不可以访问该文件。默认值为true
    #       hosts allow = #允许的主机
    #       hosts deny = #拒绝访问的主机
            ignore errors = no #设定rsync服务器在运行delete操作时是否忽略I/O错误
            ignore nonreadable = yes #设定rysnc服务器忽略那些没有访问文件权限的用户
            transfer logging = no #使rsync服务器使用ftp格式的文件来记录下载和上载操作在自己单独的日志中
    #       log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. #设定日志格式
            timeout = 600 #超时设置(秒)
            refuse options = checksum dry-run #定义一些不允许客户对该模块使用的命令选项列表
            dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz #告诉rysnc那些文件在传输前不用压缩,默认已设定压缩包不再进行压缩
            

    日志格式选项列表:
    %h:远程主机名
    %a:远程IP地址
    %l:文件长度字符数
    %p:该次rsync会话的进程id
    %o:操作类型:"send"或"recv"、”del.”
    %f:文件名
    %P:模块路径
    %m:模块名
    %t:当前时间
    %u:认证的用户名(匿名时是null)
    %b:实际传输的字节数
    %c:当发送文件时,该字段记录该文件的校验码

    下面我们来定义自己的conf文件

    # [GLOBAL OPTIONS]

    #motd file=/etc/motd
    log file=/var/log/rsyncd
    # for pid file, do not use /var/run/rsync.pid if
    # you are going to run rsync out of the init.d script.
    pid file=/var/run/rsyncd.pid
    syslog facility=daemon

    #socket options=

    # [MODULE OPTIONS]

    [serverdb]

            comment = Server Databases
            path = /home/flow/Documents/db_backup
            use chroot = no
    #       max connections=10
            lock file= /var/lock/rsyncd
    # the default for read only is yes...
            read only = true
            list = true
            uid = nobody
            gid = nogroup
            exclude = db_backup.sh
    #       exclude from = 
    #       include = 
    #       include from = 
            auth users = liubing
            secrets file = /etc/rsyncd.secrets 
            strict modes = true
            hosts allow = 192.168.1.73
    #       hosts deny = 
            ignore errors = true
            ignore nonreadable = true
            transfer logging = true
            log format = %t[%p]: host %h (%a) %o %f (%l bytes). Total %b bytes.
            timeout = 600
            refuse options = checksum dry-run
            dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

    创建一个密码文件
    $sudo vi /etc/rsyncd.secrets
    liubing:123456
    $sudo chmod 0600 /etc/rsyncd.secrets

    启动rsync
    sudo /etc/init.d/rsync start


    我们再来看一下客户端的操作,一般客户端不需要进行特殊的配置,直接同步即可
    $rsync -vzrtopg --progress liubing@192.168.1.3::serverdb .
    password
    成功!

    我们把这个同步工作交给crontab去执行。首先我们要创建一个密码文件
    $sudo vi /etc/rsync.pwd输入123456,保存

    !注意:下面这两步操作是必须的
    sudo chmod 0600 /etc/rsync.pwd
    sudo chown liubing:liubing /etc/rsync.pwd

    然后我们打开crontab,加入以下任务
    $crontab -e
    * */1 * * * rsync -a --password-file=/etc/rsync.pwd liubing@192.168.1.3::serverdb /home/liubing/Documents/db_backup

    http://www.php-oa.com/2009/02/25/flock.html

    我使用crontab同步一个文件夹时,发现一个问题,我在crontab中设置的1分钟运行一次.但当那个文件夹的内容改变时.1分钟不一定能同步完,但这时第二个rsync进行又起来了.

    这个就产生一个问题,二个rsync一起处理相同的文件,这样会出问题.如下

    1
    1 * * * * /usr/bin/rsync -avlR /data/files    172.16.xxx.xxx:/data

    本来想写个脚本来解决,但太麻烦.所以用了个linux下的锁..呵呵,象下面这个.

    1
    1 * * * * flock -xn /var/run/rsync.lock -c ' rsync -avlR /data/files    172.16.xxx.xxx:/data'

    这样,使用flock的-x参数先建一个锁文件,然后-n指定,如果锁存在,就等待.直到建成功锁在会运行-c后面的命令.这样第一个进程没有运行完前,锁文件都会存在.这样就不会二个rsync同时并发处理一个东西了

  • 相关阅读:
    React源码解析-从头写一个React的难点与思路
    2017前端书籍推荐——如何一步步看懂框架源码
    React-ReactElement解析
    新手初学WPF本地化
    IOS 关闭键盘的几种方式
    专注技术
    test
    盒子模型
    CSS选择器详解(二)通用选择器和高级选择器
    CSS选择器详解(一)常用选择器
  • 原文地址:https://www.cnblogs.com/maseng/p/3790853.html
Copyright © 2020-2023  润新知