rsync实用的文件同步命令介绍

sync是Linux系统下的文件同步和数据传输工具,可用于同步文件、代码发布

1.安装.

yum install -y xinetd yum insatll -y rsync

2.配置

打开rsync功能vim /etc/xinetd.d/rsync

service rsync
{
        disable = no    #把yes改成no
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID

}

编辑主配置文件vim /etc/rsyncd.conf服务端

uid = nobody       #目录或文件的属主属组为nobody,同步的时候报错权限不足检查目录文件的所属用户组
gid = nobody
use chroot = yes
max connections = 30
pid file=/var/run/rsyncd.pid
log file=/var/log/rsyncd.log
list = no
[data]    #同步项 模块     【同步项不需要再服务端添加】
path = /usr/local/hero_all_backup/           
hosts allow = 192.168.50.146  
read only = yes

启动即可

rsync –daemon

3.使用.

 rsync -avz aaa.txt 192.168.0.162::data
  • -v, –verbose 详细模式输出

  • -a, –archive 归档模式,表示以递归方式传输文件,并保持所有文件属性

  • -z, 对备份的文件在传输时进行压缩处理

  • –-delete 删除那些DST中SRC没有的文件

  • –-exclude= 指定排除不需要传输的文件模式

4.其他.

rsync -avz --delete /tmp/2/ /var/spool/clientmqueue/ 

同步/tmp/2空目录到/var/spool/clientmqueue/ 即删除/var/spool/clientmqueue/目录下的无用文件。

rsync -avz --delete --exclude=".svn" --exclude="*.swp"

同步的时候排除.svn和.swp的隐藏文件

rsync增量传输大文件优化技巧

问题

rsync用来同步数据非常的好用,特别是增量同步。但是有一种情况如果不增加特定的参数就不是很好用了。比如你要同步多个几十个G的文件,然后网络突然断开了一下,这时候你重新启动增量同步。但是发现等了好久都没有进行数据传输,倒是机器的IO一直居高不下。

原因

rsync具体的增量同步算法不太清楚。根据它的表现来看,可能在增量同步已经存在的一个文件时,会校验已传输部分数据是否已源文件一致,校验完成才继续增量同步这个文件剩下的数据。所以如果对一个大文件以这样的算法来增量同步是非常花时间并且占用IO资源的。

方法

半夜花了一个多小时查看了rsync的文档,发现有一个参数能快速恢复大文件的增量同步,–append。设置–append参数会在增量同步时计算文件大小并直接追加新的数据到文件,这样就省了费IO校验的过程。不过这个参数最好只在源文件和目标文件都不会更改的时候使用比较安全,比如备份的文件。

ssh端口更改后rsync的用法

rsync有两种常用的认证方式,一种为rsync-daemon方式,另外一种则是ssh。

在一些场合,使用rsync-daemon方式会比较缺乏灵活性,ssh方式则成为首选。但是今天实际操作的时候发现当远端服务器的ssh默认端口被修改后,rsync时找不到一个合适的方法来输入对方ssh服务端口号。

在查看官方文档后,找到一种方法,即使用-e参数。

-e参数的作用是可以使用户自由选择欲使用的shell程序来连接远端服务器,当然也可以设置成使用默认的ssh来连接,但是这样我们就可以加入ssh的参数了。

具体语句写法如下:

  1. rsync -e ‘ssh -p 1234’ username@hostname:SourceFile DestFile

其他参数完全按照rsync的规定格式加入即可。

上面语句中比较新鲜的地方就是使用了单引号,目的是为了使引号内的参数为引号内的命令所用。没有引号的话系统就会识别-p是给rsync的一个参数了。我的描述可能比较烂,详情可以参考rsync官方描述:

Command-line arguments are permitted in COMMAND provided that COMMAND is presented to rsync as a single argument. You must use spaces (not tabs or other whitespace) to separate the command and args from each other, and you can use single- and/or double-quotes to preserve spaces in an argument (but not backslashes). Note that doubling a single-quote inside a single-quoted string gives you a single-quote; likewise for double-quotes (though you need to pay attention to which quotes your shell is parsing and which quotes rsync is parsing).

转自:http://yynotes.net/rsync-with-ssh-without-default-port/

使用sersync实时同步文件

sersync的介绍

sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:
sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
摘自:http://coolcode.google.com/p/sersync/

安装rsync

在使用sersync之前,我们必须安装配置好rsync服务器。这里我们需要注意的是,纯粹的使用rsync做单向同步时,rsync的守护进程是运行在文件推送的服务器上,而接收的服务器是运行rsync客户端。使用sersync做文件实时同步刚好相反,用于接收文件的服务器运行rsync守护进程。
安装rsync的步骤在此不叙述,请看以前的教程配置:http://devops.webres.wang/2011/06/rsync-server-setup/或者使用本站提供的脚本更容易地安装:http://devops.webres.wang/2011/09/centos-one-key-configure-rsync-server-script/

安装sersync

到这里http://coolcode.google.com/p/sersync/downloads/list下载最新的二进制安装包,现在最新的版本是sersync2.5,我们以centos-32位为例讲解。

  1. wget http://sersync.googlecoolcode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz
  2. mkdir /usr/sersync
  3. tar xzf sersync2.5_32bit_binary_stable_final.tar.gz -C /usr/sersync/

就这样,sersync安装完成,下面介绍如何配置及使用。

配置sersync

sersync的配置文件在/usr/sersync/confxml.xml。
首先创建连接rsyncd的密码文件:

  1. echo "123456" >/usr/sersync/rsync.pas
  2. chmod 600 /usr/sersync/rsync.pas

下面是confxml.xml文件的一些配置解释:

  1. <?xml version=”1.0″ encoding=”ISO-8859-1″?>
  2. <head version=”2.5″>
  3. <host hostip=”localhost” port=”8008″></host>
  4. <debug start=”false”/>
  5. <fileSystem xfs=”false”/>
  6. <filter start=”true”>
  7. <exclude expression=”(.*).php”></exclude>
  8. <exclude expression=”(.*).html”></exclude>
  9. <exclude expression=”(.*).htm”></exclude>
  10. <exclude expression=”^tmp/*”></exclude>
  11. <!—监控事件的过程中过滤特定文件,和特定文件夹的文件 –>
  12. </filter>
  13. <inotify>
  14. <delete start=”true”/>
  15. <createFolder start=”true”/>
  16. <createFile start=”true”/>
  17. <closeWrite start=”true”/>
  18. <moveFrom start=”true”/>
  19. <moveTo start=”true”/>
  20. <attrib start=”false”/>
  21. <modify start=”true”/>
  22. <!—设置要监控的事件 –>
  23. </inotify>
  24.  
  25. <sersync>
  26. <localpath watch=”/var/www”>
  27. <!—设置要监控的目录 –>
  28. <remote ip=”xx.xx.xx.xx” name=”pppei”/>
  29. <!—指定远端rsync服务器的地址和模块名 –>
  30. </localpath>
  31. <rsync>
  32. <commonParams params=”-artuz”/>
  33. <auth start=”true” users=”pppei” passwordfile=”/usr/sersync/rsync.pas”/>
  34. <!—是否启用验证,并指定密码存放文件 –>
  35. <userDefinedPort start=”false” port=”874″/><!– port=874 –>
  36. <timeout start=”false” time=”100″/><!– timeout=100 –>
  37. <ssh start=”false”/>
  38. </rsync>
  39. <failLog path=”/tmp/rsync_fail_log.sh” timeToExecute=”60″/><!–default every 60mins execute once–>
  40. <crontab start=”true” schedule=”1440″><!–600mins–>
  41. <!—是否启用执行完整rsync,并指定执行周期 –>
  42. <crontabfilter start=”true”>
  43. <!—设置完整执行rsync时的过滤条件 –>
  44. <exclude expression=”*.php”></exclude>
  45. <exclude expression=”*.html”></exclude>
  46. <exclude expression=”*.htm”></exclude>
  47. <exclude expression=”tmp/*”></exclude>
  48. </crontabfilter>
  49. </crontab>
  50. <plugin start=”false” name=”command”/>
  51. </sersync>
  52.  
  53. <plugin name=”command”>
  54. <param prefix=”/bin/sh” suffix=”" ignoreError=”true”/>  <!–prefix /opt/tongbu/mmm.sh suffix–>
  55. <filter start=”false”>
  56. <include expression=”(.*).php”/>
  57. <include expression=”(.*).sh”/>
  58. </filter>
  59. </plugin>
  60.  
  61. <plugin name=”socket”>
  62. <localpath watch=”/opt/tongbu”>
  63. <deshost ip=”192.168.138.20″ port=”8009″/>
  64. </localpath>
  65. </plugin>
  66. <plugin name=”refreshCDN”>
  67. <localpath watch=”/data0/htdocs/cms.xoyo.com/site/”>
  68. <cdninfo domainname=”ccms.chinacache.com” port=”80″ username=”xxxx” passwd=”xxxx”/>
  69. <sendurl base=”http://pic.xoyo.com/cms”/>
  70. <regexurl regex=”false” match=”cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images”/>
  71. </localpath>
  72. </plugin>
  73. </head>

请根据自己的具体情况修改。

sersync2命令使用说明

1.在主服务器上开启sersync守护进程,使sersync在后台运行,开启实时同步。

  1. ./sersync -d

过程如下:

  1. [root@localhost GNU-Linux-x86]# ls
  2. confxml.xml  sersync2
  3. [root@localhost GNU-Linux-x86]# ./sersync2 -d
  4. set the system param
  5. execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
  6. execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
  7. parse the command param
  8. daemon thread num: 10
  9. parse xml config file
  10. host ip : localhost     host port: 8008
  11. config xml parse success
  12. please set /etc/rsyncd.conf max connections=0 Manually
  13. sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
  14. please according your cpu ,use -n param to adjust the cpu rate
  15. run the sersync:
  16. watch path is: /opt/tongbu

表明,sersync已经开启,可以在本地监控路径下建立文件,查看远程是否同步成功。

2.在开启实时监控的之前对主服务器目录与远程目标机目录进行一次整体同步

  1. ./sersync -r

如果需要将sersync运行前,已经存在的所有文件或目录全部同步到远程,要以-r参数运行sersync,将本地与远程整体同步一次。
如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步。-r参数将会无效

3.查看启动参数帮助

  1. ./sersync –help

4.指定配置文件

  1. ./sersync -o XXXX.xml

对于sersync使用可执行文件目录下的默认配置文件confxml.xml,如果需要使用另一个配置文件,可以使用-o参数指定其它配置文件。

5.指定默认的线程池的线程总数

  1. ./sersync -n num

例如 ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过这个参数调低,如果机器配置较高,可以用-n跳高线程总数。

6.不进行同步,只运行插件

  1. ./sersync -m pluginName

例如./sersync -m command,则在监控到文件事件后,不对远程目标服务器进行同步,而是直接运行command插件。

7.多个参数可以配合使用

  1. ./sersync -n 8 -o abc.xml -r -d

表示,设置线程池工作线程为8个,指定abc.xml作为配置文件,在实时监控前作一次整体同步,以守护进程方式在后台运行。

8.通常情况下,对本地到远程整体同步一遍后,在后台运行实时同步。

  1. ./sersync -r -d

最后需要把sersync命令加入到/etc/rc.local以开机自启动:

  1. echo "/usr/sersync/sersync2 -d -o /usr/sersync/confxml.xml" >>/etc/rc.local

如果需要同步多个目录,可以创建多个配置文件,如/usr/sersync/sersync2 -d -o /usr/sersync/xxx.xml

参考:
http://www.pppei.net/blog/post/124
官方教程

ERROR: module is read only

执行rsync出现如下错误:
rsync: writefd_unbuffered failed to write 4 bytes to socket [sender]: Connection reset by peer (104)
ERROR: module is read only
rsync error: syntax or usage error (code 1) at main.c(866) [receiver=3.0.6]
rsync: connection unexpectedly closed (5 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]

关键的错误信息是:ERROR: module is read only

解决方法:在rsync.conf文件中加入read only=no。

rsync错误:@ERROR: auth failed on module XXX 的原因之一

在Linux下使用rsync,将远程目录下的文件同步到本地目录时,可能会出现以下错误:
@ERROR: auth failed on module XXX
其中,XXX 表示你的远程rsync服务模块名称。
出现这种情况,先检查你的用户名和密码是否正确,如果都正确,有一个可能是原因是:远程rsync服务器的帐户密码文件的权限必须为600,例如,你在rsyncd.conf中设置了secrets file = /etc/rsyncd/rsync_pwd
那么你就必须确保rsync_pwd的访问权限为600:
chmod 600 /etc/rsyncd/rsync_pwd
然后你的问题可能就解决了。
来源:http://blog.csdn.net/learnhard/article/details/5542765

CentOS一键配置rsync服务器脚本

1、保存下面的代码为一个文件,上传到服务器端,名称为rsync.sh

  1. #!/bin/bash
  2. #rsync Written by zhumaohai
  3. #For more information please visit http://devops.webres.wang
  4. echo “Please input the rsync username:”
  5. read username
  6. echo “Please input the rsync username password:”
  7. read password
  8. echo “Please input the allow ip address:”
  9. read allowip
  10. echo “Please input the path you want to rsync:”
  11. read rsyncpath
  12. echo “==========================input all completed========================”
  13. echo “==========================install rsync========================”
  14. yum -y install rsync
  15. useradd $username
  16. mkdir /etc/rsyncd
  17. cat >/etc/rsyncd/rsyncd.conf<<EOF
  18. # Minimal configuration file for rsync daemon
  19. # See rsync(1) and rsyncd.conf(5) man pages for help
  20.  
  21. # This line is required by the /etc/init.d/rsyncd script
  22. pid file = /var/run/rsyncd.pid   
  23. port = 873
  24. #address = $serverip
  25. #uid = nobody
  26. #gid = nobody   
  27. uid = root   
  28. gid = root   
  29.  
  30. use chroot = yes
  31. read only = yes
  32.  
  33.  
  34. #limit access to private LANs
  35. hosts allow=$allowip
  36. hosts deny=*
  37.  
  38. max connections = 5
  39. motd file = /etc/rsyncd/rsyncd.motd
  40.  
  41. #This will give you a separate log file
  42. #log file = /var/log/rsync.log
  43.  
  44. #This will log every file transferred – up to 85,000+ per user, per sync
  45. #transfer logging = yes
  46.  
  47. log format = %t %a %m %f %b
  48. syslog facility = local3
  49. timeout = 300
  50.  
  51. [home]   
  52. path = $rsyncpath   
  53. list=yes
  54. ignore errors
  55. auth users = $username
  56. secrets file = /etc/rsyncd/rsyncd.secrets 
  57. EOF
  58. echo “$username:$password” > /etc/rsyncd/rsyncd.secrets
  59. chmod 600 /etc/rsyncd/rsyncd.secrets
  60. cat >/etc/rsyncd/rsyncd.motd<<EOF
  61. +++++++++++++++++++++++++++
  62. + webres.wang  rsync  2011-2012 +
  63. +++++++++++++++++++++++++++
  64. EOF
  65. /usr/bin/rsync –daemon  –config=/etc/rsyncd/rsyncd.conf
  66. echo “/usr/bin/rsync –daemon  –config=/etc/rsyncd/rsyncd.conf” >>/etc/rc.d/rc.local
  67. ps -aux | grep rsync

2、赋予脚本权限

  1. chmod +x rsync.sh

3、执行脚本

  1. ./rsync.sh

4、客户端同样需要安装rsync
具体配置见http://devops.webres.wang/2011/06/rsync-server-setup/

Linux下使用rsync最快速删除海量文件的方法

昨天遇到了要在Linux下删除海量文件的情况,需要删除数十万个文件。这个是之前的程序写的日志,增长很快,而且没什么用。这个时候,我们常用的删除命令rm -fr * 就不好用了,因为要等待的时间太长。所以必须要采取一些非常手段。我们可以使用rsync来实现快速删除大量文件。

1、先安装rsync:

  1. yum install rsync

2、建立一个空的文件夹:

  1. mkdir /tmp/test

3、用rsync删除目标目录:

  1. rsync –delete-before -a -H -v –progress –stats /tmp/test log

这样我们要删除的log目录就会被清空了,删除的速度会非常快。
文章来源:http://www.ha97.com/4107.html

rsync服务器架设(数据同步|文件增量备份)

我们在使用服务器发布我们的网站的时候,通常要考虑到文件的备份,而文件的备份比较高效的备份是增加备份,rsync软件就是这样的一个工具。为了实现多个服务器负载均衡,我们需要这几个服务器之间进行数据同步,而rsync软件也能胜任,下面我们来介绍如何架设rsync服务器来达到文件增量备份和数据同步的功能。
继续阅读rsync服务器架设(数据同步|文件增量备份)