<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>同步 &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/%E5%90%8C%E6%AD%A5/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Fri, 28 Mar 2014 09:32:43 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.1</generator>
	<item>
		<title>rsync安装与使用细节</title>
		<link>https://www.mudbest.com/rsync%e5%ae%89%e8%a3%85%e4%b8%8e%e4%bd%bf%e7%94%a8%e7%bb%86%e8%8a%82/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Fri, 28 Mar 2014 09:30:12 +0000</pubDate>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[rsync]]></category>
		<category><![CDATA[同步]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=2028</guid>

					<description><![CDATA[老套的搬用一下rsync的介绍，rsync是Linux系统下的数据镜像备份工具，从软件的命名上就可以看出来了— ... <a title="rsync安装与使用细节" class="read-more" href="https://www.mudbest.com/rsync%e5%ae%89%e8%a3%85%e4%b8%8e%e4%bd%bf%e7%94%a8%e7%bb%86%e8%8a%82/" aria-label="阅读 rsync安装与使用细节">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>老套的搬用一下rsync的介绍，rsync是Linux系统下的数据镜像备份工具，从软件的命名上就可以看出来了——remote sync。rsync支持大多数的类Unix系统，无论是Linux、Solaris还是BSD上都经过了良好的测试。它的特性如下： </p>
<p>1、可以镜像保存整个目录树和文件系统。<br />
2、可以很容易做到保持原来文件的权限、时间、软硬链接等等。 <span id="more-2028"></span><br />
3、无须特殊权限即可安装。<br />
4、优化的流程，文件传输效率高。<br />
5、可以使用rcp、ssh等方式来传输文件，当然也可以通过直接的socket连接。</p>
<p><strong>一、安装Rsync。</strong><br />
如果支持yum安装，直接使用：</p>
<pre class="brush: bash; title: ; notranslate">
yum install xinetd
</pre>
<p>如果是源码安装则到官方http://rsync.samba.org/下载source包即可，安装过程大致如下：</p>
<pre class="brush: bash; title: ; notranslate">
tar zxvf *
cd *
./configure
make
make install
#如果是已安装了RPM版的Rsync，并且要安装新版的Rsync，需要先删除Rsync 
#rpm -e rsync
</pre>
<p>Redhat会有一个包依赖rsync，可以使用rpm -e rsync &#8211;nodeps卸载。另外，RPM版的rsync带了一个服务端的启动脚本，/etc/xinetd.d/rsync可以保留，但是需要注意的是必须在主服务器和同步服务器上都安装rsync，其中主服务器上是以服务器模式运行rsync，而同步上则以客户端方式运行rsync。这样在主服务器上运行rsyn守护进程，在同步上用crond定时运行客户程序来同步主服务器上需要同步的内容。</p>
<p><strong>二、配置rsync服务器端</strong></p>
<p>rsync服务器的配置文件为/etc/rsyncd.conf，其控制认证、访问、日志记录等等。该文件是由一个或多个模块结构组成。一个模块定义以方括弧中的模块名开始，直到下一个模块定义开始或者文件结束，模块中包含格式为name=value的参数定义。每个模块其实就对应需要备份的一个目录树，比方说在我们的实例环境中，有三个目录树需要备份：/www/和/mirror/file0/和/mirror/file1/目录，那么就需要在配置文件中定义三个模块，分别对应三个目录树。配置文件是行为单位的，也就是每个新行都表示一个新的注释、模块定义或者参数赋值。</p>
<pre class="brush: bash; title: ; notranslate">
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
</pre>
<p>将disable 改为no，另外将配置成随系统启动：</p>
<pre class="brush: bash; title: ; notranslate">
chkconfig rsync on
</pre>
<p>重新启动：</p>
<pre class="brush: bash; title: ; notranslate">
service xinetd restart  
#重新加载配置
/etc/rc.d/init.d/xinetd reload  
</pre>
<p>查看进程是否在监听：</p>
<pre class="brush: bash; title: ; notranslate">
netstat -a | grep rsync
#或者
lsof -i :873
</pre>
<p>配置rsync账户相关：</p>
<pre class="brush: bash; title: ; notranslate">
#配置rsync密码文件，(名字随便写，只要和配置文件里的一致即可)，格式(一行一个用户) ：
vim /home/rsync.ps
hkshadow:hkshadow
#配置rsync密码文件权限 
chown root.root rsync.ps
#注意：该文件只能是root用户可读写的，注意，出于安全目的，这个文件的属性必需是只有属主可读，否则rsync将拒绝运行。 
chmod 600 rsync.ps
</pre>
<p>创建配置文件：</p>
<pre class="brush: bash; title: ; notranslate">
uid = nobody # 备份以什么身份进行，用户
ID gid = nobody # 备份以什么身份进行，组ID ，注意这个用户ID和组ID，如果要方便的话，可以设置成root，这样rsync几乎就可以读取任何文件和目录了，但是也带来安全隐患。建议设置成只能读取你要备份的目录和文件即可。 
use chroot = no  max connections = 0  #最大连接数，0代表没有限制  
#port = 873          #默认端口873  
pid file = /var/log/rsync/rsyncd.pid   #运行进程的ID写到哪里 
#lock file = /var/log/rsync/rsync.lock

log file = /var/log/rsync/rsyncd.log  #日志记录文件 
motd file = /var/log/rsync/rsyncd.motd  #欢迎消息 
strict modes =yes         #是否检查口令文件的权限 

&#x5B;txt]           # 指定认证的备份模块名  
path = /www/htdocs/pub/test/   # 需要备份的目录 
comment = BACKUP attachment           # 注释  
ignore errors              # 忽略一些无关的IO错误  
read only = false          # 设置为非只读，即可以传至服务器的相应目录。 
list = false                # 不允许列文件  
exclude = test/ test.php      #设置不同步的目录或文件用空格隔开 
hosts allow = 210.51.0.80     #允许连接服务器的主机IP地址，如果多个ip则用空格隔开 
hosts deny = 0.0.0.0/0.0.0.0         #禁止连接服务器的主机IP地址，也可为*，表示所有。 
auth users = msyn                     # 认证的用户名，如果没有这行，则表明是匿名 
secrets file = /home/rsync.ps      # 认证文件名，用来存放密码
</pre>
<p><strong>三、配置客户端(省略)</strong><br />
配置密码文件 (注：为了安全，设定密码档案的属性为：600。rsync.scrt的密码一定要和Rsync Server 密码设定案里的密码一样)</p>
<p>下一步是一个运行rsync的例子：</p>
<pre class="brush: bash; title: ; notranslate">
rsync -vzrltogp --progress --delete user@x.x.x.168::attachment  /usr/local/apache/htdocs/pub/html/ --password-file=/home/rsync.ps
</pre>
<p>上面这个命令行中-vzrtopg代表的意思是：<br />
-v表示verbose(详细)<br />
-z表示zip（压缩）<br />
-r表示recursive（递归目录）<br />
-topg是保持文件原有属性如o=owner(属主)、t=times(时间)、p=perms(权限)、g=group(组)的参数 -H表示保持硬连接<br />
-l：保留软链结  -u：表示只更新源文件比目标时间新的文件<br />
&#8211;progress是指显示出详细的进度情况<br />
&#8211;delete是指如果服务器端删除了这一文件，那么客户端也相应把文件删除，保持真正的一致。 user@x.x.x.168::attachment 是表示对服务器x.x.x.168中的attachment模块进行备份，登录帐号是user<br />
&#8211;password-file = /etc/rsync.scrt来指定密码文件，这里需要注意的是这份密码文件权限属性要设得只 有属主可读(权限为600)，属主为正在运行此命令的用户。<br />
-a:(-a, &#8211;archive equivalent to -rlptgoD)以archive模式操作，复制目录、符号连接在这里面</p>
<p>==========================================================================<br />
另外，可以创建一个脚本运行这个命令，并记录日志：</p>
<pre class="brush: bash; title: ; notranslate">
# cat /usr/local/bin/rsync.sh  
#!/bin/bash    
DATE=`date +%w`  
rsync -avzH --progress --delete inburst@192.168.168.52::inburst /home/quack/backup/$DATE --password-file=/etc/rsync.pass &gt; /var/log/rsync.$DATE   
</pre>
<p>修改/etc/crontab做好定时更新(配置文件 /etc/crontab)  </p>
<pre class="brush: bash; title: ; notranslate">
crontab -e  15 4 * * 6 root rsync.sh  
crontab -l    
//查看所有自定义任务 
/etc/init.d/crond restart    //重启动任务计划 

#crontab Crontab文件的格式： 
#第1列分钟1～59  
#第2列小时1～23（0表示子夜） 
#第3列日1～31  
#第4列月1～12  
#第5列星期0～6（0表示星期天）
</pre>
<p>扩展阅读：<br />
<strong>关于 rsync 中: 和 :: 及 rysnc 和 ssh 认证协议的区别</strong></p>
<p>（1）双冒号 “::”的用法：<br />
rsync 传输文件前需要登录认证，那么这个过程用到的协议有两种：ssh 和 rsync<br />
何时用ssh 协议呢？<br />
我们平时用的  rsync -av /SRC root@192.168.99.36:/DEST 就是默认用的 ssh 协议<br />
这种方式默认是省略了 -e ssh 的，与下面等价：</p>
<pre class="brush: bash; title: ; notranslate">
rsync -av /SRC -e ssh root@192.168.99.36:/DEST
</pre>
<p>当遇到要修改端口的时候，我们可以：</p>
<pre class="brush: bash; title: ; notranslate">
rsync -av /SRC -e &quot;ssh -p36000&quot; root@192.168.99.36:/DEST
</pre>
<p>何时用rysnc协议呢？</p>
<pre class="brush: bash; title: ; notranslate">
rsync -av /SRC rsync://root@192.168.99.36:36000/modual/DestPath
</pre>
<p>注意：这条语句显示的指明了使用rsync认证协议，port后的modual是rsync服务端配置文件rsyncd.conf<br />
里面配置的模块名，模块里面会包含一些用户名、密码、路径等认证信息。<br />
使用rsync认证，还有一种写法：</p>
<p>1<br />
rsync -av /SRC &#8211;port=36000 root@172.17.256.211::modual/DestPath<br />
注意：这种写法不需显示指定 rsync 协议，而是根据 :: 来识别的，端口自己用 &#8211;port 指定。<br />
而且这里 modual 前面没有 / 的。</p>
<p>总结： 双冒号:: 是用在 rsync 协议里面的，: 一般用在ssh协议里面，这两种用法各有千秋：<br />
rsync协议你需要在rsync服务端配置模块，这增加了运维工作量，但是安全，因为不需要对客户公开帐号密码。<br />
ssh协议方便，不需配置，拿到帐号密码即可开工，但是对客户是暴露的，有安全风险。<br />
还需要注意的是用rsync协议认证的时候，后面跟的是模块名，而不是路径，这点要注意。</p>
<p>（2）关于 rsync &#8211;port 的man文档如下：<br />
rsync 客户端 &#8211;port<br />
 &#8211;port=PORT<br />
              This  specifies  an alternate TCP port number to use rather than<br />
              the default of 873.  This is only needed if you  are  using  the<br />
              double-colon  (::) syntax to connect with an rsync daemon (since<br />
              the URL syntax has a way to specify the port as a  part  of  the<br />
              URL).<br />
rsync 服务端 &#8211;port<br />
 &#8211;port=PORT<br />
              This  specifies  an  alternate TCP port number for the daemon to<br />
              listen on rather than the default of 873.  See also  the  &#8220;port&#8221;<br />
              global option in the rsyncd.conf manpage.</p>
<p>（3）附一些参考文档：<br />
rsync实例用法及参数详解<br />
http://hi.baidu.com/leejun_2005/item/672c31d4c12a8b1b20e25041</p>
<p>rsync命令用法入门<br />
http://hi.baidu.com/leejun_2005/item/a04d17c4cf24547088ad9e7c</p>
<p>rsync 的核心算法<br />
http://hi.baidu.com/leejun_2005/item/11651fc379229f52bdef69b7</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
