1. 当前位置:网站首页 > 

Ubuntu下 rsync同步文件实例


Rsync(remote synchronize) 是一个远程数据同步工具,可以使用“Rsync算法”同步本地和远程主机之间的文件。
rsync的好处是只同步两个文件不同的部分,相同的部分不在传递。类似于增量备份,
这使的在服务器传递备份文件或者同步文件,比起scp工具要省好多时间。
OS:ubuntu server 10.04
server:192.168.64.128
client:192.168.64.145

server

1.ubuntu  server 10.04默认已安装rsync,rsync服务默认不是启动的,我们要修改下面的文件。
$sudo vi /etc/default/rsync
RSYNC_ENABLE=true   #false改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文件

# 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
syslog facility=daemon
#socket options=

# MODULE OPTIONS

[my_rsync_bk] #名字可以任意命名,只要客户端的rsync命令一致

        comment = public archive
        path = /home/rsync_bk                             #指定路径,如果没有这个目录自己建。
        use chroot = no
#       max connections=10
        lock file = /var/lock/rsyncd
# the default for read only is yes...
        read only = yes
        list = yes
        uid = nobody
        gid = nogroup

#       exclude = 
#       exclude from = 
#       include =
#       include from =
        auth users = liu_rsync              #rsync连接时的用户名,要和客户端rsync的命令一致
        secrets file = /etc/rsyncd.secrets #这里是保存密码的地方,如果屏蔽掉就不用密码了
        strict modes = yes
        hosts allow = 192.168.64.145 #运行的客户端ip
#       hosts deny =
        ignore errors = yes
        ignore nonreadable = yes
        transfer logging = yes
        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



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

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

client
我们再来看一下客户端的操作,一般客户端不需要进行特殊的配置,直接同步即可
 rsync -vzrtopg --progress  liu_rsync@192.168.64.128::my_rsync_bk .  #.为当前目录,在服务端touch一个文件,同步后就会出现,当然你也可以指定一个位置比如/databk,需注意所建立的权限!


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

!注意:下面这两步操作是必须的,没有足够权限同步就会不成功,ls -l 你所指向的目录
sudo chmod 0600 /etc/rsync.pwd
sudo chown 普通用户:普通用户组 /etc/rsync.pwd

然后我们打开crontab,使它自动同步
$crontab -e
* * * * * rsync -a --password-file=/etc/rsync.pwd liu_rsync@192.168.64.128::my_rsync_bk /databk   

crontab随机启动  sudo vi /etc/rc.local

参数说明:

-v verbose,即详细模式

-z 压缩

-r recursive 递归

-topg 保持文件原有属性,一般不用加

 --progress 用来显示详细进度情况

--delete 表示如果服务器删除了一个文件,客户端也应对应删除

--exclude="*.sh" 表明不包括某些文件

--password-file=/etc/rsync.pwd 指定所使用的密码文件

最后一项是需要同步的目录  

注意指定使用的密码文件中只需要有密码,不要有用户名。

------------------------------------------------------------------------

1.在本地机器上对两个目录同步

$ rsync -zvr filename1 filename2

上述代码是将filename1中的文件与filename2中的文件同步,如果将filename2中的文件同步到filename1中,修改代码为:

$ rsync -zvr filename2 filename1

2.使用rsync –a 同步保留时间按标记

$ rsync -azv filename1 filename2

使用上述命令,将filename2中新同步的文件的时间与filename1中的创建的时间相同,

它保留符号链接、权限、时间标记、用户名及组名相同。

3.从本地同步文件到远程服务器

$rsync -avz filename1 ubuntu@192.168.0.1:/home/ubuntu/filename2

上述命令是将本地的filename1同步到远程192.168.0.1的主机上。

注意:如果远程主机的端口不是默认的22端口,假如是3000端口,上述的命令修改为,

$ rsync -avz '-e ssh -p 4000' filename1 ubuntu@192.168.0.1:/home/ubuntu/filename2

4.将远程服务器的文件同步到本地

与步骤3类似,只是将filename1与远程服务器的位置对换一下,

$rsync -avz ubuntu@192.168.0.1:/home/ubuntu/filename2 filename1

同理如果端口不是22,使用以下命令

$ rsync -avz '-e ssh -p 4000' ubuntu@192.168.0.1:/home/ubuntu/filename2 filename1 

参考:http://www.eduicc.com/read-24.html

           http://jordy.easymorse.com/?p=273





 服务器之间常常要保持些文件或目录的一致,比如一些大的软件下载网站,它们通常使用多台服务器来提供下载服务。当一台服务器上的文件更新后,其它的服务器也需要更新,而且 在更新的时候应该是只对新增或是修改过的文件进行更新,否则会造成网络带宽和时间的浪费。rsync就是能有效的保持文件及目录的一致的优秀软件。
  rsync,remote synchronize
  顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息,而且可以通过ssh方式来传输文件,这样其保密性也非常好,另外它还是免费的软件。rysnc的官方网站:::URL::http://rsync.samba.org/,可以从上面得到最新的版本。当然,因为rsync是一款如此有用的软件,所以很多Linux的发行版本都将它收录在内了。你的Linux里并没有安装rsync,你可以按以下的安法自行安装: 
  一、安装过程
  1.下载rsync
  目前(2003年9月)最新的rsync版本是2.5.6,从rysnc的官方网站上下载一个回来:
  # wget ::URL::http://ftp.samba.org/ftp/rsync/rsync-2.5.6.tar.gz 

  2.解压
  # tar -xzpvf rsync-2.5.6.tar.gz

  3.编译安装
  # cd rsync-2.5.6/
  # ./configure --prefix=/usr/local/rsync
  # make
  # make install
  以上过程没有出现的话就安装好了,现在就有rsync命令可以用了,rsync命令放在
/usr/local/rsync/bin。用rsync命令可以去运行有rsync服务的服务器上抓取资料。
  如果要把当前的机器变成一台rsync服务器的话,就需要继续进行一些配置了。

  二、配置rsync服务
  配置一个简单的rsync服务并不复杂,你需要修改或建立一些配置文件。
  1.rsyncd.conf
  # vi /etc/rsyncd.motd
  rsyncd.con是rsync服务的主要配置文件,它控制rsync服务的各种属性,下面给出一个
rsyncd.conf文件的例子:
  #先定义整体变量
  secrets file = /etc/rsyncd.secrets
  motd file = /etc/rsyncd.motd
  read only = yes
  list = yes
  uid = nobody
  gid = nobody
  hosts allow = 192.168.100.90 #哪些电脑可以访问rsync服务
  hosts deny = 192.168.100.0/24 #哪些电脑不可以访问rsync服务
  max connections = 2
  log file = /var/log/rsyncd.log
  pid file = /var/run/rsyncd.pid
  lock file = /var/run/rsync.lock
  #再定义要rsync目录
  [terry]
  comment = Terry 's directory from 192.168.100.21
  path = /home/terry
  auth users = terry,rsync
  [test]
  comment = test rsync
  path = /home/test
  在上面的配置文件中,限定了192.168.100.0/24这个子网中,只有192.168.100.90的机器可以来访问这台rsync服务器的rsync服务。配置文件的后面部分定义了两个rsync的目录,terry目录是只有知道terry、rsync两个账号的人才能使用的,而text目录是无需账号就可以访问的。rsync在定义目录时还提供了一些其它选项,可以作更严格的控制。

  2.rsyncd.secrets
  # vi /etc/rsyncd.secrets
  rsyncd.secrets是存储rsync服务的用户名和密码的,它是一个明文的文本文件,下面给出一个rsyncd.secrets文件的例子:
  terry:12345
  rsync:abcde
  因为rsyncd.secrets存储了rsync服务的用户名和密码,所以非常重要,因此文件的属性必须
设为600,只有所有者可以读写:
  # chmod 600 /etc/rsyncd.secrets

  3.rsyncd.motd
  # vi /etc/rsyncd.motd
  rsyncd.motd记录了rsync服务的欢迎信息,你可以在其中输入任何文本信息,如:
  Welcome to use the rsync services!

  4.services
  # vi /etc/services
  services并不是rsync的配置文件,这一步也可以不做。而修改了services文件的好处就在于
系统知道873端口对就的服务名为rsync。修改services的方法就是确保services中有如下两行,
没有的话就自行加入:
  rsync  873/tcp  # rsync
  rsync  873/udp  # rsync
  5./etc/xinetd.d/rsync
  # vi /etc/xinetd.d/rsync
  建立一个名为/etc/xinetd.d/rsync文件,输入以下内容:
  service rsync
  {
    disable = no
    socket_type  = stream
    wait      = no
    user      = root
    server     = /usr/local/rsync/bin/rsync
    server_args  = --daemon
    log_on_failure += USERID
  }
  保存后,就可以运行rsync服务了。输入以下命令:
  # /etc/rc.d/init.d/xinetd reload
  这样rsync服务就在这台机器上(192.168.100.21)运行起来了,接下来就是如何来使用它了。

  三、rsync命令的用法
  在配置完rsync服务器后,就可以从客户端发出rsync命令来实现各种同步的操作。rsync有很
多功能选项,下面就对介绍一下常用的选项:
  rsync的命令格式可以为:
  1. rsync [OPTION]... SRC [SRC]... [USER@]HOST : DEST]
  2. rsync [OPTION]... [USER@]HOST:SRC DEST]
  3. rsync [OPTION]... SRC [SRC]... DEST]
  4. rsync [OPTION]... [USER@]HOST::SRC [DEST]
  5. rsync [OPTION]... SRC [SRC]... [USER@]HOST :: DEST]
  6. rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

  rsync有六种不同的工作模式:
  1. 拷贝本地文件;当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。
  2.使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST
路径地址包含单个冒号":"分隔符时启动该模式。
  3.使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC
地址路径包含单个冒号":"分隔符时启动该模式。
  4. 从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。
  5. 从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。
  6. 列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。
  下面以实例来说明:
  # rsync -vazu -progress terry@192.168.100.21:/terry/ /home
  v详细提示
  a以archive模式操作,复制目录、符号连接
  z压缩
  u只进行更新,防止本地新文件被重写,注意两者机器的时钟的同时
  -progress指显示
  以上命令是保持客户机192.168.100.90上的/home/terry目录和rsync服务器上的terry目录同
步。该命令执行同步之前会要求你输入terry账号的密码,这个账号是我们前面在rsyncd.secrets
文件中定义的。如果想将这条命令写到一个脚本中,然后定时执行它的话,可以使用--password-file
选项,具体命令如下:
  # rsync -vazu -progress --password-file=/etc/rsync.secret
  terry@192.168.100.21:/terry/ /home
  要使用--password-file选项,就得先建立一个存放密码的文件,这里指定为/etc/rsync.secret。
其内容很简单,如下:
  terry:12345
  同样要修改文件属性如下:
  # chmod 600 /etc/rsyncd.secrets

  四、利用rsync保持Linux服务器间的文件同步实例
  现在假设有两台Linux服务器A(192.168.100.21)和B(192.168.100.90),服务器A中的
/home/terry和服务器B中的/home/terry这两个目录需要保持同步,也就是当服务器A中文件发生
改变后,服务器B中的文件也要对应去改变。
  我们按上面的方法,在服务器A上安装rsync,并将其配置为一台rsync服务器,并将/home/terry
目录配置成rsync共享出的目录。然后在服务器B上安装rsync,因为B只做客户端,所以无需配置。
然后在服务器B,建立以下脚本:
  #!/bin/bash
  /usr/loca/rsync/bin/rsync -vazu -progress --delete
  --password-file=/etc/rsync.secret terry@192.168.100.21:/terry/ /home
  将这个脚本保存为AtoB.sh,并加上可执行属性:
  # chmod 755 /root/AtoB.sh
  然后,通过crontab设定,让这个脚本每30分钟运行一次。执行命令:
  # crontab -e
  输入以下一行:
  0,30 * * * * /root/AtoB.sh
  保存退出,这样服务器B每个小时的0分和30分时都会自动运行一次AtoB.sh,AtoB.sh是负责
保持服务器B和服务器A同步的。这样就保证了服务器A的所有更新在30钟后,服务器B也一样取
得了和服务器A一样的最新的资料。

  五、其它应用
  rsync除了同步文件及目录之外,还可以利用它来实现对远程网站的远程备份。如果再结合脚本和Crontab就能实现定时自动远程备份。其可以实现与商业化的备份和镜象产品的类似效果,但完全免费。

         附:rsync有六种不同的工作模式:
  1. 拷贝本地文件;当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。
  2.使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST
路径地址包含单个冒号":"分隔符时启动该模式。
  3.使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC
地址路径包含单个冒号":"分隔符时启动该模式。
  4. 从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。
  5. 从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。
  6. 列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。





  前段日子在网上找rsync daemon的filter等过滤规则的详解,怎么也找不到,哥一怒之下,决定自己动手翻译,于是有了本文…
    本文内容翻译自rsync的官方文档:http://rsync.samba.org/ftp/rsync/rsync.html,以及http://rsync.samba.org/ftp/rsync/rsyncd.conf.html,非完整的全文翻译,并且按照我的需要,对原文的相关章节或条目进行了适当的重新编排。官方文档发布日期:26 Mar 2011。

    rsync是Unix/Linux系统中一款优秀高效的镜像同步和远程数据备份工具,它可以把本地文件拷贝到远程主机,或从远程主机拷贝文件到本地,也可以在本地的两个目录之间进行拷贝,但不支持两个远程主机之间的互相拷贝。

    关于rsync的工作方式:
    rsync连接远程主机进行同步或备份时有两种途径:使用远程shell程序(如ssh或rsh)进行连接,或使用TCP直接连接rsync daemon。
    当源路径或目的路径的主机名后面包含一个冒号分隔符时,rsync使用远程shell传输;当源路径或目的路径的主机名后面包含两个冒号,或使用rsync://URL时,rsync使用TCP直接连接rsync daemon。
    特别的,如果只指定了源路径,而没有指定目的路径,rsync将会显示源路径中的文件列表,类似于使用命令ls -l。
 rsync把本地端看作client,把远程端当成server。注意:不要把server与rsync daemon混淆!daemon一定是server,而server却不一定是daemon,也可能是远程shell的衍生进程。
    至于什么是daemon,以及远程shell衍生daemon,下文会有说明。

    关于rsync的安装:
    rsync安装方式是最常规的./configure && make && make install,只是要注意:源机器和目标机器都要安装。

    关于rsync的启动:
    前面说过rsync在进行同步或备份时,有两种途经连接远程主机:使用远程shell,或使用TCP连接远程daemon,可以把它们分别称之为shell模式和daemon模式。只有daemon模式才有启动rsync一说。
    如果使用shell模式,rsync安装完成后就可以直接使用了,无所谓启动,也不需要任何配置文件。远程传输时一般使用ssh作为传输工具,当然,也可以使用参数-e来指定一个远程shell。
    如果使用daemon模式,就无需远程shell了,但必须在一台机器上启动rsync daemon,默认端口873.

    你可以经由inetd启动daemon,或者作为一个独立进程启动daemon,或者由rsync client通过远程shell启动daemon。
     1)如果把它作为一个独立进程来启动,只需要运行命令:rsync --daemon即可;
     2)如果通过inetd来运行,要修改两个文件:/etc/services和/etc/inetd.conf:
      在/etc/services文件中添加如下行:rsync 873/tcp
      在/etc/inetd.conf中添加:rsync stream tcp nowait root /usr/bin/rsync rsyncd --daemon
  注意把/usr/bin/rsync换成你的rsync安装目录!然后重启inetd服务,使配置文件生效。

    关于由client通过远程shell启动daemon,官方文档中有一个条目:USING RSYNC-DAEMON FEATURES VIA A REMOTE-SHELL CONNECTION。
    条目中说,rsync支持这种用法:使用远程shell连接host,然后衍生出一个single-use “daemon”服务器,来读取远程用户家目录下的配置文件。从用户的角度看,通过远程shell连接而启用的daemon传输,它的命令行语法跟正常的rsync daemon传输是一样的,唯一不同的是,你必须在命令中使用--rsh=COMMAND来明确远程shell程序。如下例:
    rsync -av --rsh=ssh host::module /dest
  译者注:虽然官方文档中说这种用法在某些情况下很有用,但我觉得很别扭。

    当rsync作为daemon运行时,它需要一个用户身份。如果你希望启用chroot,则必须以root的身份来运行daemon,监听端口(1024以下,默认873),或设定文件属主;如果不启用chroot,也可以不使用root用户来运行daemon,但该用户必须对相应的模块拥有读写数据、日志和lock file的权限。
    当rsync以daemon模式运行时,它还需要一个配置文件:rsyncd.conf。修改这个配置后不必重启rsync daemon,因为每一次的client连接都会去重新读取该文件。
    译者注:关于配置文件rsyncd.conf的设定,后面会有详细的参数解释。

    关于rsync的两种模式:
    该部分额外的内容来自IBM官方网站上的一篇文章。该文章把rsync的运行模式扩展成了四种,但我认为rsync官方文档的两种模式的划分更加合理一些。
    所谓rsync daemon,就是在一台机器上永久运行的rsync进程。在任何一台安装rsync的机器上运行rsync --daemon,则这台机器上运行的rsync就是rsync daemon。我们可以把文件发送给daemon,也可以向daemon请求文件。
    daemon模式非常适合创建中心备份服务器,或项目存储库。
    两种模式的区别前面已经说过,shell模式在源路径和目的路径中使用一个冒号,而daemon模式使用两个冒号。

    关于rsync命令的语法:
  1. 本地拷贝: 
  2.    rsync [选项] SRC… DEST,在本地进行复制操作
  3.  
  4. shell模式: 
  5.    rsync [选项] [user@]Host:SRC… [DEST],拉动作,从远程主机拉文件到本地 
  6.    rsync [选项] SRC… [user@]Host:DEST,推动作,把本地文件推送到远程主机
  7.  
  8. daemon模式: 
  9.    rsync [选项] [user@]Host::SRC… [DEST],拉动作 
  10.    rsync [选项] rsync://[user@]Host [:port]/SRC… [DEST],仍然是拉动作 
  11.    rsync [选项] SRC... [user@]Host::DEST,推动作 
  12.    rsync [选项] SRC...rsync://[user@]Host [:port]/DEST,仍然是推动作 
                                                <div style="box-sizing:border-box;font-size:14px;font-family:宋体, &quot;Arial Narrow&quot;, arial, serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#2c2c2c;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:28px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                    <span style="box-sizing:border-box;font-family:Arial;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;">&nbsp; &nbsp; 官方文档的举例:<br style="box-sizing:border-box;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;" data-filtered="filtered" />

    1)rsync -t *.c foo:src/

        把当前目录中所有以.c结尾的文件传输到机器名为foo的src目录中。如果某些文件已经在远程主机中存在,则跳过该文件。

    2)rsync -avz foo:src/bar /data/tmp

         从机器名为foo的远程主机上把/src/bar目录及其下所有文件传输到本地机器的/data/tmp目录中。注意:源路径src/bar的最后没有斜杠”/”!

        源路径的最后是否有斜杠有不同的含义:有斜杠,只是复制目录中的文件;没有斜杠的话,不但要复制目录中的文件,还要复制目录本身!

        目的路径的最后有没有斜杠,对传输没有影响。

    3)rsync -av host:file1 :file2 host:file{3,4} /dest/

         rsync -av host::mod/file{1,2} host::mod/file3 /dest/

         rsync -av host::mod/file1 ::mod/file{3,4}

        这个例子是rsync更高级一点的用法,使用rsync同时传输多个指定文件。

    rsync可以一次传输多个指定的文件,方法是添加多个远程主机参数,或忽略主机名。如上例。



    关于rsync命令中的选项:

    上面例子中提到了一些选项,rsync的选项有很多,请参考官方文档。常用的选项如下:

                                                    
  1. -a:归档模式,保持文件的所有属性,相当于-rlptgoD 
  2. -r:递归模式 
  3. -e:指定一个远程shell 
  4. -z:文件传输时进行压缩处理 
  5. --delete:镜像中常用,删除DEST中那些SRC中不存在的文件 
                                                                        <div style="box-sizing:border-box;font-size:14px;font-family:宋体, &quot;Arial Narrow&quot;, arial, serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#2c2c2c;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:28px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-family:Arial;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"> daemon也有一些常用选项,下面会有介绍。<br style="box-sizing:border-box;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;" data-filtered="filtered" />



    client如何连接rsync daemon:

    上面第3个例子已经给出了client连接daemon的语法示例。但是官方文档又把本条目单独列了出来,提醒我们在连接rsync daemon时要注意的几点:

    1) daemon模式中的源路径,即SRC,实际上是模块的名称

    2) 如果未在双冒号::后面指定源路径,则rsync会列出daemon上所有可能的模块

    3) 不要使用--rsh(-e)参数

    4) 有些模块可能需要密码认证,你可以使用--password-file选项,来指定密码文件



    启用一个rsync daemon来接受client的连接:

    除--daemon外,rsync daemon启动时还有其它一些常用选项:

    --daemon:

      使rsync作为一个daemon运行。daemon有两种启动方式,前面已经说过。对client发出的每一次连接请求,daemon会读取配置文件rsyncd.conf,然后做出相应的回应。

    --bwlimit=KBPS:

      指定daemon每秒传输的最大千字节。client在请求连接时也可以指定一个--bwlimit值,但该值不能大于daemon端的设置。

    --config=FILE:

      指定daemon的配置文件。只有当rsync以daemon模式运行的时候,该选项才有效。默认的配置文件为/etc/rsyncd.conf,除非daemon是通过远程shell运行,并且远程user非超级用户,这种情况下,配置文件rsyncd.conf默认存在于当前目录(在$HOME目录中比较典型)。关于通过远程shell启用daemon,见上文。

    --port=PORT:

      指定daemon监听的端口。

    --log-file=FILE:

      该选项告诉daemon不要使用rsyncd.conf中指定的log文件,而要使用此处指定的文件。此选项很少用到


                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;text-align:center;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><strong style="box-sizing:border-box;font-weight:bold;">rsync 远程数据同步工具详解</strong></span>
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><span style="box-sizing:border-box;font-weight:400;color:#888888;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><br style="box-sizing:border-box;" data-filtered="filtered" />

rysnc(remote synchronize)在CentOS系统默认安装在/usr/bin,此外rysnc在windows平台下也有相应版本。主页地址为: http://rsync.samba.org/


                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;color:#888888;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;">rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远 程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。</span>
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-weight:400;color:#888888;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;" data-filtered="filtered"></span>
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;color:#888888;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><br style="box-sizing:border-box;" data-filtered="filtered" />

rsync本来是用于替代rcp的一个工具,目前由rsync.samba.org维护,所以rsync.conf文件的格式类似于samba的主配 置文件。rsync可以通过rsh或ssh使用,也能以daemon模式去运行,在以daemon方式运行时rsync server会打开一个873端口,等待客户端去连接。连接时,rsync server会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,以后则就只需进行增量备份。

 

                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-weight:400;color:#0000ff;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><strong style="box-sizing:border-box;font-weight:bold;">------------------------------------------------------------------------------------------------------------------------------------------------------------</strong></span>
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;text-align:center;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;">&nbsp;</span>
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;" data-filtered="filtered"></span>
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><strong style="box-sizing:border-box;font-weight:bold;">rsync的命令格式可以为以下六种:</strong>&nbsp;<br style="box-sizing:border-box;" data-filtered="filtered" />



  rsync [OPTION]... SRC DEST

  rsync [OPTION]... SRC [USER@]HOST:DEST

  rsync [OPTION]... [USER@]HOST:SRC DEST

  rsync [OPTION]... [USER@]HOST::SRC DEST

  rsync [OPTION]... SRC [USER@]HOST::DEST

  rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
 



  对应于以上六种命令格式,rsync有六种不同的工作模式:



  1、拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:rsync -a /data /backup

  2、使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。如:rsync -avz *.c foo:src

  3、使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。如:rsync -avz foo:src/bar /data

  4、从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。如:rsync -av root@172.16.78.192::www /databack

  5、从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。如:rsync -av /databack root@172.16.78.192::www

  6、列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。如:rsync -v rsync://172.16.78.192/www


                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><strong style="box-sizing:border-box;font-weight:bold;">rsync 命令常用参数</strong>&nbsp;<br style="box-sizing:border-box;" data-filtered="filtered" />

-a 递归方式传输文件,并保持文件属性,等价-rlptgoD不包含-H 

-r 子目录以递归模式处理

-l 保持符号链接文件

-H 保持硬链接文件

-p 保持文件权限

-t 保持文件时间信息

-g 保持文件归属组信息

-o 保持文件归属用户信息

-D 保持设备文件和特殊文件

-z 在传输文件时进行压缩处理

--exclude=PATTERN 指定排除一个不需要传输文件匹配模式

--exclude-from=FILE 从FILE中读取排除规则

--include=PATTERN 指定需要传输的文件匹配模式

--include-from=FILE 从FILE中读取匹配规则

--copy-unsafe-links 复制指向复制源路径目录以外的链接文件

--safe-links 忽略指向复制源路径目录以外的链接文件(默认)

--existing 仅更新那些已经存在于接收端的文件,而不复制新创建文件

--ignore-existing 忽略那些已经存在于接收端的文件,只复制新创建文件

-b 当有变化时,对目标目录中的旧文件进行备份

--backup-dir=DIR 与-b结合使用,指定备份路径

--link-dest=DIR 当文件未改变时在指定路径创建硬链接文件

--delete 删除接收端还存在而保存端不存在的文件

--delete-before 接收端在传输之前进行删除操作(默认)

--delete-during 接收端在传输过程中进行删除操作

--delete-after 接收端在传输之后进行删除操作

--delete-excluded 接收端在传输同时进行删除操作

--e,--rsh=COMMAND 指定代替rsh的shell程序,例如可以指定为ssh

--ignore-erros 即使出现I/O错误也要进行删除

--partial 保留因故没有完全传输的文件,以加快随后的再次传输

--progress 在传输时显示传输过程

-p 等价于—partial—progress

--delay-updates 将正在更新的文件先保存到.~tmp~临时目录,待传输完毕再更新目标文件

-v,--verbose 详细输出模式

-q,--quiet 精简输出模式

-h,--human-readable 输出文件大小

-n,--dry-run 显示那些文件将要被传输

--list-only 仅列出文件而不进行复制

--rsync-path=PROGRAM 指定远程服务器上的rsync命令所在路径

--password-file=FILE 指定从FILE中读取口令,避免在终端中输入口令

-4,-6 使用IPv4或者IPv6

--version 打印版本信息

--help 显示帮组信息


                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><br style="box-sizing:border-box;" data-filtered="filtered" />


注意: 若用普通用户身份运行rsync命令,同步后的文件的属主将改变为这个普通用户身份;若用超级用户身份运行rsync命令同步后文件属性保持原来用户身份不变。若指定-tg但目标计算机没有指定用户和组,则复制的文件归入501用户 501组

                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            &nbsp;
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-weight:400;color:#0000ff;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><strong style="box-sizing:border-box;font-weight:bold;">------------------------------------------------------------------------------------------------------------------------------------------------------------</strong></span>
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><br style="box-sizing:border-box;" data-filtered="filtered" />


启动 rsync 服务 
启动 rsync 服务
 

                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;">vi /etc/xinetd.d/rsync&nbsp; 把disable = YES改成NO<br style="box-sizing:border-box;" data-filtered="filtered" />

service rsync

{

        disable = no

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/bin/rsync

        server_args     = --daemon

        log_on_failure  += USERID

}

                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            &nbsp;
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><br style="box-sizing:border-box;" data-filtered="filtered" />


                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            &nbsp;
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><strong style="box-sizing:border-box;font-weight:bold;">配置服务端&nbsp;</strong></span><span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><strong style="box-sizing:border-box;font-weight:bold;">rsync</strong>&nbsp;<br style="box-sizing:border-box;" data-filtered="filtered" />

rsync 服务器可以独立运行也可以由xinetd运行,CentOS默认以xinetd运行,同时默认监听873端口。需要创建rsync配置文件 vi /etc/rsyncd.conf

在文件中[module]之外的所有配置行都是全局参数,也可以在全局参数部分定义模块参数,模块参数主要功能定义哪个目录要被同步,可以根据需要来定义多个模块 uid = root 

gid = root 

max connections = 10

use chroot = no 

log file = /var/log/rsyncd.log 

pid file = /var/run/rsyncd.pid 

lock file = /var/run/rsyncd.lock 



[tongbu] 

path = /opt/tongbu 

comment = hello world 

ignore errors = yes 

read only = no 

hosts allow = 192.168.1.125 

hosts deny = *

(可以匿名连接,不安全,详细参考以下)

                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;">启动并设置随系统启动 rsync,要求</span>&nbsp;<span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;">服务端和客户端都启动</span>&nbsp;<span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;">,否则使用的时候报错Connection refused</span>
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            &nbsp;
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;color:#800000;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><br style="box-sizing:border-box;" data-filtered="filtered" />


rsync --daemon –-config=/etc/rsyncd.conf 
chkconfig rsync on

                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            &nbsp;
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;" data-filtered="filtered"><span style="box-sizing:border-box;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;background-color:#cfe2f3;" data-filtered="filtered"><span style="box-sizing:border-box;font-weight:400;color:#993300;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;" data-filtered="filtered"></span></span></span>
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            &nbsp;
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-weight:400;color:#0000ff;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><strong style="box-sizing:border-box;font-weight:bold;">------------------------------------------------------------------------------------------------------------------------------------------------------------</strong></span>
                                                                        </p>
                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><br style="box-sizing:border-box;" data-filtered="filtered" />


全局参数 
address 指定的服务器运行的IP地址。由xinetd运行时将忽略此参数
port 指定rsync守护进程监听端口号(默认873)。由xinetd运行时将忽略此参数
motd file 指定一个消息文件,当客户连接服务器时该文件内容将显示给客户
pid file rsync的守护进程将其PID写入指定的文件
log file 指定守护进程的日志文件,而不将日志发给syslog
syslog facility 指定发送日志消息给syslog时的消息级别
socket options 自定义tcp选项
 

                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><strong style="box-sizing:border-box;font-weight:bold;">基本模块参数</strong>&nbsp;<br style="box-sizing:border-box;" data-filtered="filtered" />

path 指定当前模块在rsync服务器上的同步路径,必须指定

comment 给模块指定一个描述

控制模块参数
use 默认为true,传输文件之前先chroot到path参数所指定的目录下,这样做实现额外的安全防护,但缺点需要root权限,并且不能备份指向path外部的符号链接所指向的目录文件
uid 制定该模块已指定的UID传输文件
gid 指定该模块已指定的GID文件
max connections 限制最大连接并发数以保护服务器,超过限制则提示稍后操作
lock file 指定支持max connections参数的锁文件,默认/etc/run/rsyncd.lock
list 指定当客户请求列出可以使用的模块列表时(默认ture),该模块是否被列出。如果false,可以创建隐藏的模块
read only 是否也许客户上传文件,默认ture则不也许上传。为false并且服务器目录具有读写权限则允许上传
write only 是否也许客户上传文件,默认ture则不也许下载。为false并且服务器目录具有读写权限则允许下载
ignore errors 指定rsync服务器在运行delete操作时是否忽略I/O错误
ignore nonreadable 指定rysnc服务器忽略那些没有访问文件权限的用户
timeout 设定连接超时时间,单位为秒
dont compress 告诉rysnc那些文件在传输前不用压缩,默认已设定压缩包不再进行压缩
refuse options 定义一些不允许客户对该模块使用的命令选项列表

                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><strong style="box-sizing:border-box;font-weight:bold;">文件筛选模块参数</strong>&nbsp;<br style="box-sizing:border-box;" data-filtered="filtered" />

exclude 指定排除的多个文件或目录,由空格隔开

exclude from 从指定文件中读取排除规则

include 指定包含复制多个文件或者目录,由空格隔开

include from 从指定文件中读取包含规则

用户认证模块参数 
auth users 指定用户才允许连接该模块
secrets file 指定一个口令认证文件,格式是 用户:密码,即指定用户和密码连接匹配才行,不匹配的话会提示ERROR: auth failed on module,不设置该参数则可以匿名连接。 
strict modes 指定是否检测口令文件权限。rsync认证口令文件权限一定是600

                                                                        <p style="box-sizing:border-box;font-size:14px;font-family:Helvetica, Tahoma, Arial, sans-serif;white-space:normal;word-spacing:0px;text-transform:none;font-weight:normal;color:#333333;padding-bottom:0px;font-style:normal;padding-top:0px;padding-left:0px;orphans:2;widows:2;margin:0px;letter-spacing:normal;line-height:25px;padding-right:0px;background-color:#ffffff;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;">
                                                                            <span style="box-sizing:border-box;font-size:12px;font-family:&quot;courier new&quot;, courier;font-weight:400;padding-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;"><strong style="box-sizing:border-box;font-weight:bold;">访问控制参数</strong>&nbsp;<br style="box-sizing:border-box;" data-filtered="filtered" />

hosts allow 指定哪些IP可以访问该模块

hosts deny 指定哪些IP不可以访问该模块,*表示全部




本文最后更新于2018-1-25,已超过 3个月没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!
版权说明

本文地址:https://www.chensj.net/?post=108
未标注转载均为本站远程,转载请注明文章出处:

联系我们

在线咨询:点击这里给我发消息

微信号:chensj923

工作日:9:00-23:00,节假日休息

扫码关注