text: <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>第 4 章 File Synchronize</title><link rel="stylesheet" type="text/css" href="..//docbook.css" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><meta name="keywords" content="openfiler, freenas, proftpd,pureftpd,vsftpd, rsync,wget,samba" /><link rel="home" href="../index.html" title="Netkiller Linux Storage 手札" /><link rel="up" href="../index.html" title="Netkiller Linux Storage 手札" /><link rel="prev" href="../ftp/pureftpd.html" title="3.6. Pure-FTPd + LDAP + MySQL + PGSQL + Virtual-Users + Quota" /><link rel="next" href="tsync.html" title="4.2. tsync" /></head><body><a xmlns="" href="//www.netkiller.cn/">Home</a> |
  		<a xmlns="" href="//netkiller.github.io/">简体中文</a> |
  	    <a xmlns="" href="http://netkiller.sourceforge.net/">繁体中文</a> |
  	    <a xmlns="" href="/journal/index.html">杂文</a> |
  	    <a xmlns="" href="//www.netkiller.cn/home/donations.html">打赏(Donations)</a> |
  	    <a xmlns="" href="https://github.com/netkiller">Github</a> |
  	    <a xmlns="" href="http://my.oschina.net/neochen/">OSChina 博客</a> |
  	    <a xmlns="" href="https://cloud.tencent.com/developer/column/2078">云社区</a> |
  	    <a xmlns="" href="https://yq.aliyun.com/u/netkiller/">云栖社区</a> |
  	    <a xmlns="" href="https://www.facebook.com/bg7nyt">Facebook</a> |
  	    <a xmlns="" href="http://cn.linkedin.com/in/netkiller/">Linkedin</a> |
  	    <a xmlns="" href="https://zhuanlan.zhihu.com/netkiller">知乎专栏</a> |
  	    <a xmlns="" href="//www.netkiller.cn/home/video.html">视频教程</a> |
  	    <a xmlns="" href="//www.netkiller.cn/home/about.html">About</a><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">第 4 章 File Synchronize</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="../ftp/pureftpd.html">上一页</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="tsync.html">下一页</a></td></tr></table><hr /></div><table xmlns=""><tr><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&amp;repo=netkiller.github.io&amp;type=watch&amp;count=true&amp;size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe></td><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&amp;repo=netkiller.github.io&amp;type=fork&amp;count=true&amp;size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe></td><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&amp;type=follow&amp;count=true&amp;size=large" height="30" width="240" frameborder="0" scrolling="0" style="width:240px; height: 30px;" allowTransparency="true"></iframe></td></tr></table><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a id="index"></a>第 4 章 File Synchronize</h1></div></div></div><div class="toc"><p><strong>目录</strong></p><dl class="toc"><dt><span class="section"><a href="index.html#rsync">4.1. rsync - fast remote file copy program (like rcp)</a></span></dt><dd><dl><dt><span class="section"><a href="index.html#rsync.setup">4.1.1. 安装Rsync与配置守护进程</a></span></dt><dd><dl><dt><span class="section"><a href="index.html#source">4.1.1.1. install with source</a></span></dt><dt><span class="section"><a href="index.html#aptitude">4.1.1.2. install with aptitude</a></span></dt><dt><span class="section"><a href="index.html#rsync.xinetd">4.1.1.3. xinetd</a></span></dt><dt><span class="section"><a href="index.html#systemctl">4.1.1.4. CentOS 7 - systemctl</a></span></dt></dl></dd><dt><span class="section"><a href="index.html#rsyncd.conf">4.1.2. rsyncd.conf</a></span></dt><dt><span class="section"><a href="index.html#rsync.option">4.1.3. rsync 参数说明</a></span></dt><dd><dl><dt><span class="section"><a href="index.html#idp34">4.1.3.1. -n, --dry-run perform a trial run with no changes made</a></span></dt><dt><span class="section"><a href="index.html#idp35">4.1.3.2. --bwlimit=KBPS          limit I/O bandwidth; KBytes per second</a></span></dt><dt><span class="section"><a href="index.html#idp36">4.1.3.3. -e, --rsh=COMMAND           specify the remote shell to use</a></span></dt></dl></dd><dt><span class="section"><a href="index.html#example">4.1.4. step by step to learn rsync</a></span></dt><dt><span class="section"><a href="index.html#rsync.example">4.1.5. rsync examples</a></span></dt><dd><dl><dt><span class="section"><a href="index.html#idp37">4.1.5.1. upload</a></span></dt><dt><span class="section"><a href="index.html#idp38">4.1.5.2. download</a></span></dt><dt><span class="section"><a href="index.html#idp39">4.1.5.3. mirror</a></span></dt><dt><span class="section"><a href="index.html#idp40">4.1.5.4. rsync delete</a></span></dt><dt><span class="section"><a href="index.html#idp41">4.1.5.5. backup to a central backup server with 7 day incremental</a></span></dt><dt><span class="section"><a href="index.html#idp42">4.1.5.6. backup to a spare disk</a></span></dt><dt><span class="section"><a href="index.html#idp43">4.1.5.7. mirroring vger CVS tree</a></span></dt><dt><span class="section"><a href="index.html#idp44">4.1.5.8. automated backup at home</a></span></dt><dt><span class="section"><a href="index.html#idp45">4.1.5.9. Fancy footwork with remote file lists</a></span></dt></dl></dd><dt><span class="section"><a href="index.html#rsync.windows">4.1.6. rsync for windows</a></span></dt><dt><span class="section"><a href="index.html#rsync.sh">4.1.7. 多进程 rsync 脚本</a></span></dt></dl></dd><dt><span class="section"><a href="tsync.html">4.2. tsync</a></span></dt><dt><span class="section"><a href="lsyncd.html">4.3. lsyncd</a></span></dt><dd><dl><dt><span class="section"><a href="lsyncd.html#idp46">4.3.1. 安装</a></span></dt><dt><span class="section"><a href="lsyncd.html#idp51">4.3.2. 配置 lsyncd.conf</a></span></dt><dd><dl><dt><span class="section"><a href="lsyncd.html#idp50">4.3.2.1. lsyncd.conf 配置项说明</a></span></dt><dd><dl><dt><span class="section"><a href="lsyncd.html#idp47">4.3.2.1.1. settings 全局设置</a></span></dt><dt><span class="section"><a href="lsyncd.html#idp48">4.3.2.1.2. sync 定义同步参数</a></span></dt><dt><span class="section"><a href="lsyncd.html#idp49">4.3.2.1.3. rsync</a></span></dt></dl></dd></dl></dd><dt><span class="section"><a href="lsyncd.html#idp52">4.3.3. 配置演示</a></span></dt></dl></dd><dt><span class="section"><a href="unison.html">4.4. Unison File Synchronizer</a></span></dt><dd><dl><dt><span class="section"><a href="unison.html#idp53">4.4.1. local</a></span></dt><dt><span class="section"><a href="unison.html#idp54">4.4.2. remote</a></span></dt><dt><span class="section"><a href="unison.html#idp55">4.4.3. config</a></span></dt></dl></dd><dt><span class="section"><a href="csync2.html">4.5. csync2 - cluster synchronization tool</a></span></dt><dd><dl><dt><span class="section"><a href="csync2.html#idp56">4.5.1. server</a></span></dt><dt><span class="section"><a href="csync2.html#idp57">4.5.2. node</a></span></dt><dt><span class="section"><a href="csync2.html#idp58">4.5.3. test</a></span></dt><dt><span class="section"><a href="csync2.html#idp59">4.5.4. Advanced Configuration</a></span></dt><dt><span class="section"><a href="csync2.html#idp60">4.5.5. 编译安装</a></span></dt></dl></dd><dt><span class="section"><a href="synctool.html">4.6. synctool</a></span></dt></dl></div>
  	
  	<div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="rsync"></a>4.1. rsync - fast remote file copy program (like rcp)</h2></div></div></div>
  	
  	<p>rsync is an open source utility that provides fast incremental file transfer. rsync is freely available under the GNU General Public License version 2 and is currently being maintained by Wayne Davison.</p>
  	<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="rsync.setup"></a>4.1.1. 安装Rsync与配置守护进程</h3></div></div></div>
  		
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="source"></a>4.1.1.1. install with source</h4></div></div></div>
  			
  			<div class="procedure"><a id="idp128"></a><p class="title"><strong>过程 4.1. rsync</strong></p><ol class="procedure" type="1"><li class="step"><p>安装rsync</p>
  					<p>在AS3 第二张CD上找到rsync-2.5.6-20.i386.rpm</p>
  					<pre class="screen">
  [root@linuxas3 root]# cd /mnt
  [root@linuxas3 mnt]# mount cdrom
  [root@linuxas3 mnt]# cd cdrom/RedHat/RPMS
  [root@linuxas3 RPMS]# rpm -ivh rsync-2.5.6-20.i386.rpm
  					</pre>
  				</li><li class="step"><p>配置/etc/rsyncd.conf</p>
  					<p>在rh9,as3系统上rsync安装后,并没有创建rsyncd.conf文档，要自己创建rsyncd.conf文档</p>
  					<pre class="screen">
  [root@linuxas3 root]# vi /etc/rsyncd.conf
  
  uid=nobody
  gid=nobody
  max connections=5
  use chroot=no
  log file=/var/log/rsyncd.log
  pid file=/var/run/rsyncd.pid
  lock file=/var/run/rsyncd.lock
  #auth users=root
  secrets file=/etc/rsyncd.passwd
  
  [postfix]
  path=/var/mail
  comment = backup mail
  ignore errors
  read only = yes
  list = no
  auth users = postfix
  
  [netkiller]
  path=/home/netkiller/web
  comment = backup 9812.net
  ignore errors
  read only = yes
  list = no
  auth users = netkiller
  
  [pgsqldb]
  path=/var/lib/pgsql
  comment = backup postgresql database
  ignore errors
  read only = yes
  list = no
  					</pre>
  		
  			  		<ol type="a" class="substeps">
  			    	<li class="step"><p>选项说明</p>
  				    	<pre class="screen">
  uid = nobody
  gid = nobody
  use chroot = no         # 不使用chroot
  max connections = 4     # 最大连接数为4
  pid file = /var/run/rsyncd.pid           #进程ID文件
  lock file = /var/run/rsync.lock
  log file = /var/log/rsyncd.log    # 日志记录文件
  secrets file = /etc/rsyncd.pwd    # 认证文件名,主要保存用户密码，权限建议设为600，所有者root
  
  [module]            # 这里是认证的模块名，在client端需要指定
  path = /var/mail    # 需要做镜像的目录
  comment = backup xxxx # 注释
  ignore errors         # 可以忽略一些无关的IO错误
  read only = yes       # 只读
  list = no             # 不允许列文件
  auth users = postfix  # 认证的用户名，如果没有这行，则表明是匿名
  
  [other]
  path = /path/to...
  comment = xxxxx
  				    	</pre>
  			    	</li>
  			    	<li class="step"><p>密码文件</p>
  			    		<p>在server端生成一个密码文件/etc/rsyncd.pwd</p>
  				    	<pre class="screen">
  [root@linuxas3 root]# echo postfix:xxx &gt;&gt;/etc/rsyncd.pwd
  [root@linuxas3 root]# echo netkiller:xxx &gt;&gt;/etc/rsyncd.pwd
  [root@linuxas3 root]# chmod 600 /etc/rsyncd.pwd
  				    	</pre>
  			    	</li>
  			    	<li class="step"><p>启动rsync daemon</p>
  				    	<pre class="screen">
  [root@linuxas3 root]# rsync --daemon
  				    	</pre>
  			    	</li>
  			  		</ol>
  				</li><li class="step">
  					<p>添加到启动文件</p>
  					<pre class="screen">
  echo　"rsync　--daemon"　&gt;&gt;　/etc/rc.d/rc.local                                    [  OK  ]
  					</pre>
  					<p>cat /etc/rc.d/rc.local 确认一下</p>
  				</li><li class="step"><p>测试</p>
  					<pre class="screen">
  [root@linux docbook]#  rsync rsync://netkiller.8800.org/netkiller
  [root@linux tmp]# rsync rsync://netkiller@netkiller.8800.org/netkiller
  Password:
  
  [chen@linux temp]$  rsync -vzrtopg --progress --delete postfix@netkiller.8800.org::postfix /tmp
  Password:
  				</pre>
  			</li></ol></div>
  		</div>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="aptitude"></a>4.1.1.2. install with aptitude</h4></div></div></div>
  			
  			<div class="procedure"><a id="idp129"></a><p class="title"><strong>过程 4.2. installation setp by setp</strong></p><ol class="procedure" type="1"><li class="step">
  					<p>installation</p>
  					<pre class="screen">
  $ sudo apt-get install rsync
  					</pre>
  				</li><li class="step">
  					<p>enable</p>
  					<pre class="screen">
  $ sudo vim /etc/default/rsync
  
  RSYNC_ENABLE=true
  					</pre>
  				</li><li class="step">
  					<p>config /etc/rsyncd.conf</p>
  					<pre class="screen">
  $ sudo vim /etc/rsyncd.conf
  
  uid=nobody
  gid=nobody
  max connections=5
  use chroot=no
  pid file=/var/run/rsyncd.pid
  lock file=/var/run/rsyncd.lock
  log file=/var/log/rsyncd.log
  #auth users=root
  secrets file=/etc/rsyncd.secrets
  
  [neo]
  path=/home/neo/www
  comment = backup neo
  ignore errors
  read only = yes
  list = no
  auth users = neo
  
  [netkiller]
  path=/home/netkiller/public_html
  comment = backup netkiller
  ignore errors
  read only = yes
  list = no
  auth users = netkiller
  
  [mirror]
  path=/var/www/netkiller.8800.org/html/
  comment = mirror netkiller.8800.org
  exclude = .svn
  ignore errors
  read only = yes
  list = yes
  
  [music]
  path=/var/music
  comment = backup music database
  ignore errors
  read only = yes
  list = no
  
  [pgsqldb]
  path=/var/lib/pgsql
  comment = backup postgresql database
  ignore errors
  read only = yes
  list = no
  auth users = neo,netkiller
  					</pre>
  				</li><li class="step">
  					<p>/etc/rsyncd.secrets</p>
  					<pre class="screen">
  $ sudo vim  /etc/rsyncd.secrets
  
  neo:123456
  netkiller:123456
  					</pre>
  					<p></p>
  					<pre class="screen">
  $ sudo chmod 600 /etc/rsyncd.secrets
  					</pre>
  				</li><li class="step">
  					<p>start</p>
  					<pre class="screen">
  $ sudo /etc/init.d/rsync start
  					</pre>
  				</li><li class="step">
  					<p>test</p>
  					<pre class="screen">
  $ rsync -vzrtopg --progress --delete neo@localhost::neo /tmp/test1/
  $ rsync -vzrtopg --progress --delete localhost::music /tmp/test2/
  					</pre>
  				</li><li class="step">
  					<p>firewall</p>
  					<pre class="screen">
  $ sudo ufw allow rsync
  					</pre>
  				</li></ol></div>
  		</div>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="rsync.xinetd"></a>4.1.1.3. xinetd</h4></div></div></div>
  			
  			<p>CentOS 6 之前的版本可以使用 xinetd， CentOS 7 不建议使用</p>
  			<pre class="screen">
  yum install xinetd
  			</pre>
  			<p>配置 /etc/xinetd.d/rsync</p>
  			<pre class="screen">
  vim /etc/xinetd.d/rsync
  
  # default: off
  # description: The rsync server is a good addition to an ftp server, as it \
  #	allows crc checksumming etc.
  service rsync
  {
  	disable	= yes
  	flags		= IPv6
  	socket_type     = stream
  	wait            = no
  	user            = root
  	server          = /usr/bin/rsync
  	server_args     = --daemon
  	log_on_failure  += USERID
  }
  			</pre>
  			<p>disable	= yes 改为 disable	= no</p>
  			<p># vim /etc/rsyncd.conf</p>
  			<pre class="screen">
  chkconfig xinetd on
  /etc/init.d/xinetd restart
  			</pre>
  		</div>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="systemctl"></a>4.1.1.4. CentOS 7 - systemctl</h4></div></div></div>
  			
  			<pre class="screen">
  systemctl enable rsyncd
  systemctl start rsyncd
  systemctl restart rsyncd
  systemctl stop rsyncd
  			</pre>
  			<p>启动配置项 /etc/sysconfig/rsyncd</p>
  			<pre class="screen">
  # cat /etc/sysconfig/rsyncd
  OPTIONS="" 			
  			</pre>
  			<p>启动脚本</p>
  			<pre class="screen">
  # cat /usr/lib/systemd/system/rsyncd.service
  [Unit]
  Description=fast remote file copy program daemon
  ConditionPathExists=/etc/rsyncd.conf
  
  [Service]
  EnvironmentFile=/etc/sysconfig/rsyncd
  ExecStart=/usr/bin/rsync --daemon --no-detach "$OPTIONS"
  
  [Install]
  WantedBy=multi-user.target			
  			</pre>
  		</div>
  	</div>
  	<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="rsyncd.conf"></a>4.1.2. rsyncd.conf</h3></div></div></div>
  		
  		<pre class="screen">
  # Minimal configuration file for rsync daemon
  # See rsync(1) and rsyncd.conf(5) man pages for help
  
  # This line is required by the /etc/init.d/rsyncd script
  pid file = /var/run/rsyncd.pid
  port = 873
  address = 192.168.1.171
  #uid = nobody
  #gid = nobody
  uid = root
  gid = root
  
  use chroot = yes
  read only = yes
  
  
  #limit access to private LANs
  hosts allow=192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0
  hosts deny=*
  
  max connections = 5
  motd file = /etc/rsyncd/rsyncd.motd
  
  #This will give you a separate log file
  #log file = /var/log/rsync.log
  
  #This will log every file transferred - up to 85,000+ per user, per sync
  #transfer logging = yes
  
  log format = %t %a %m %f %b
  syslog facility = local3
  timeout = 300
  
  [home]
  path = /home
  list=yes
  ignore errors
  auth users = linux
  secrets file = /etc/rsyncd/rsyncd.secrets
  comment = linuxsir home
  exclude =   beinan/  samba/
  
  [beinan]
  path = /opt
  list=no
  ignore errors
  comment = optdir
  auth users = beinan
  secrets file = /etc/rsyncd/rsyncd.secrets
  
  [www]
  path = /www/
  ignore errors
  read only = true
  list = false
  hosts allow = 172.16.1.1
  hosts deny = 0.0.0.0/32
  auth users = backup
  secrets file = /etc/backserver.pas
  
  
  [web_user1]
  path = /home/web_user1/
  ignore errors
  read only = true
  list = false
  hosts allow = 202.99.11.121
  hosts deny = 0.0.0.0/32
  uid = web_user1
  gid = web_user1
  auth users = backup
  secrets file = /etc/backserver.pas
  
  
  [pub]
    	comment = Random things available for download
    	path = /path/to/my/public/share
    	read only = yes
    	list = yes
    	uid = nobody
    	gid = nobody
    	auth users = pub
    	secrets file = /etc/rsyncd.secrets
  		</pre>
  	</div>
  
  	<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="rsync.option"></a>4.1.3. rsync 参数说明</h3></div></div></div>
  		
  		<pre class="screen">
  		
  命令行选项
  -v, --verbose 详细模式输出
  -q, --quiet 精简输出模式
  -c, --checksum 打开校验开关，强制对文件传输进行校验
  -a, --archive 归档模式，表示以递归方式传输文件，并保持所有文件属性，等于-rlptgoD
  -r, --recursive 对子目录以递归模式处理
  -R, --relative 使用相对路径信息
  -b, --backup 创建备份，也就是对于目的已经存在有同样的文件名时，将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
  --backup-dir 将备份文件(如~filename)存放在在目录下。
  -suffix=SUFFIX 定义备份文件前缀
  -u, --update 仅仅进行更新，也就是跳过所有已经存在于DST，并且文件时间晚于要备份的文件。(不覆盖更新的文件)
  -l, --links 保留软链结
  -L, --copy-links 想对待常规文件一样处理软链结
  --copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
  --safe-links 忽略指向SRC路径目录树以外的链结
  -H, --hard-links 保留硬链结
  -p, --perms 保持文件权限
  -o, --owner 保持文件属主信息
  -g, --group 保持文件属组信息
  -D, --devices 保持设备文件信息
  -t, --times 保持文件时间信息
  -S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
  -n, --dry-run现实哪些文件将被传输
  -W, --whole-file 拷贝文件，不进行增量检测
  -x, --one-file-system 不要跨越文件系统边界
  -B, --block-size=SIZE 检验算法使用的块尺寸，默认是700字节
  -e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
  --rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
  -C, --cvs-exclude 使用和CVS一样的方法自动忽略文件，用来排除那些不希望传输的文件
  --existing 仅仅更新那些已经存在于DST的文件，而不备份那些新创建的文件
  --delete 删除那些DST中SRC没有的文件
  --delete-excluded 同样删除接收端那些被该选项指定排除的文件
  --delete-after 传输结束以后再删除
  --ignore-errors 及时出现IO错误也进行删除
  --max-delete=NUM 最多删除NUM个文件
  --partial 保留那些因故没有完全传输的文件，以是加快随后的再次传输
  --force 强制删除目录，即使不为空
  --numeric-ids 不将数字的用户和组ID匹配为用户名和组名
  --timeout=TIME IP超时时间，单位为秒
  -I, --ignore-times 不跳过那些有同样的时间和长度的文件
  --size-only 当决定是否要备份文件时，仅仅察看文件大小而不考虑文件时间
  --modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口，默认为0
  -T --temp-dir=DIR 在DIR中创建临时文件
  --compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
  -P 等同于 --partial
  --progress 显示备份过程
  -z, --compress 对备份的文件在传输时进行压缩处理
  --exclude=PATTERN 指定排除不需要传输的文件模式
  --include=PATTERN 指定不排除而需要传输的文件模式
  --exclude-from=FILE 排除FILE中指定模式的文件
  --include-from=FILE 不排除FILE指定模式匹配的文件
  --version 打印版本信息
  --address 绑定到特定的地址
  --config=FILE 指定其他的配置文件，不使用默认的rsyncd.conf文件
  --port=PORT 指定其他的rsync服务端口
  --blocking-io 对远程shell使用阻塞IO
  -stats 给出某些文件的传输状态
  --progress 在传输时现实传输过程
  --log-format=formAT 指定日志文件格式
  --password-file=FILE 从FILE中得到密码
  --bwlimit=KBPS 限制I/O带宽，KBytes per second
  -h, --help 显示帮助信息
  		
  		</pre>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp34"></a>4.1.3.1. -n, --dry-run perform a trial run with no changes made</h4></div></div></div>
  			
  			<p>模拟运行，显示日志，但不做复制操作。</p>
  			<pre class="screen">
  rsync -anvzP /www/* root@172.16.0.1/www
  			</pre>
  		</div>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp35"></a>4.1.3.2. --bwlimit=KBPS          limit I/O bandwidth; KBytes per second</h4></div></div></div>
  			
  			<p>速度限制，限制为 100k Bytes/s</p>
  			<pre class="screen">
  rsync -auvzP--bwlimit=100 /www/* root@172.16.0.1/www
  			</pre>
  		</div>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp36"></a>4.1.3.3. -e, --rsh=COMMAND           specify the remote shell to use</h4></div></div></div>
  			
  			<pre class="screen">
  rsync -auzv --rsh=ssh root@202.130.101.33:/www/example.com/* /backup/example.com/ 
  # --rsh=ssh 可以省略
  rsync -auzv root@202.130.101.33:/www/example.com/* /backup/example.com/				
  			</pre>
  			<p>如果需要特别参数，可以这样写，这里指定连接SSH的端口为20</p>
  			<pre class="screen">
  rsync -auzv --rsh='ssh -p20' root@202.130.101.34:/www/example.com/* /backup/example.com/  			
  			</pre>
  		</div>
  	</div>
  	<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="example"></a>4.1.4. step by step to learn rsync</h3></div></div></div>
  		
  		<div class="procedure"><ol class="procedure" type="1"><li class="step">
  				<p>transfer file from src to dest directory</p>
  				<pre class="screen">
  neo@netkiller:/tmp$ mkdir rsync
  neo@netkiller:/tmp$ cd rsync/
  neo@netkiller:/tmp/rsync$ ls
  neo@netkiller:/tmp/rsync$ mkdir src dest
  neo@netkiller:/tmp/rsync$ echo file1 &gt; src/file1
  neo@netkiller:/tmp/rsync$ echo file2 &gt; src/file2
  neo@netkiller:/tmp/rsync$ echo file3 &gt; src/file3
  				</pre>
  			</li><li class="step">
  				<p>skipping directory</p>
  				<pre class="screen">
  neo@netkiller:/tmp/rsync$ mkdir src/dir1
  neo@netkiller:/tmp/rsync$ mkdir src/dir2
  neo@netkiller:/tmp/rsync$ rsync src/* dest/
  skipping directory src/dir1
  skipping directory src/dir2
  				</pre>
  			</li><li class="step">
  				<p>recurse into directories</p>
  				<pre class="screen">
  neo@netkiller:/tmp/rsync$ rsync -r src/* dest/
  neo@netkiller:/tmp/rsync$ ls dest/
  dir1  dir2  file1  file2  file3
  				</pre>
  			</li><li class="step">
  				<p>backup</p>
  				<pre class="screen">
  neo@netkiller:/tmp/rsync$ rsync -r --backup --suffix=.2008-11-21 src/* dest/
  neo@netkiller:/tmp/rsync$ ls dest/
  dir1  dir2  file1  file1.2008-11-21  file2  file2.2008-11-21  file3  file3.2008-11-21
  neo@netkiller:/tmp/rsync$
  				</pre>
  				<p>backup-dir</p>
  				<pre class="screen">
  neo@netkiller:/tmp/rsync$ rsync -r --backup --suffix=.2008-11-21 --backup-dir mybackup src/* dest/
  neo@netkiller:/tmp/rsync$ ls dest/
  dir1  dir2  file1  file1.2008-11-21  file2  file2.2008-11-21  file3  file3.2008-11-21  mybackup
  neo@netkiller:/tmp/rsync$ ls dest/mybackup/
  file1.2008-11-21  file2.2008-11-21  file3.2008-11-21
  				</pre>
  				<p></p>
  				<pre class="screen">
  rsync -r --backup --suffix=.2008-11-21 --backup-dir ../mybackup src/* dest/
  neo@netkiller:/tmp/rsync$ ls
  dest  mybackup  src
  neo@netkiller:/tmp/rsync$ ls src/
  dir1  dir2  file1  file2  file3
  				</pre>
  			</li><li class="step">
  				<p>update</p>
  				<pre class="screen">
  neo@netkiller:/tmp/rsync$ rm -rf dest/*
  neo@netkiller:/tmp/rsync$ rsync -r -u src/* dest/
  neo@netkiller:/tmp/rsync$ echo netkiller&gt;&gt;src/file2
  neo@netkiller:/tmp/rsync$ rsync -v -r -u src/* dest/
  building file list ... done
  file2
  
  sent 166 bytes  received 42 bytes  416.00 bytes/sec
  total size is 38  speedup is 0.18
  				</pre>
  				<p>update by time and size</p>
  				<pre class="screen">
  neo@netkiller:/tmp/rsync$ echo Hi&gt;src/dir1/file1.1
  neo@netkiller:/tmp/rsync$ rsync -v -r -u src/* dest/
  building file list ... done
  dir1/file1.1
  
  sent 166 bytes  received 42 bytes  416.00 bytes/sec
  total size is 41  speedup is 0.20
  				</pre>
  			</li><li class="step">
  				<p>--archive</p>
  				<pre class="screen">
  rsync -a src/ dest/
  				</pre>
  			</li><li class="step">
  				<p>--compress</p>
  				<pre class="screen">
  rsync -a -z src/ dest/
  				</pre>
  			</li><li class="step">
  				<p>--delete</p>
  				<p>src</p>
  				<pre class="screen">
  svn@netkiller:~$ ls src/
  dir1  dir2  file1  file2  file3
  				</pre>
  				<p>dest</p>
  				<pre class="screen">
  neo@netkiller:~$ rsync -v -u -a --delete -e ssh svnroot@127.0.0.1:/home/svnroot/src /tmp/dest
  svnroot@127.0.0.1's password:
  receiving file list ... done
  created directory /tmp/dest
  src/
  src/file1
  src/file2
  src/file3
  src/dir1/
  src/dir2/
  
  sent 104 bytes  received 309 bytes  118.00 bytes/sec
  total size is 0  speedup is 0.00
  				</pre>
  				<p>src</p>
  				<pre class="screen">
  svn@netkiller:~$ rm -rf src/file2
  svn@netkiller:~$ rm -rf src/dir2
  				</pre>
  				<p>dest</p>
  				<pre class="screen">
  neo@netkiller:~$ rsync -v -u -a --delete -e ssh svnroot@127.0.0.1:/home/svnroot/src /tmp/dest
  svnroot@127.0.0.1's password:
  receiving file list ... done
  deleting src/dir2/
  deleting src/file2
  src/
  
  sent 26 bytes  received 144 bytes  68.00 bytes/sec
  total size is 0  speedup is 0.00
  				</pre>
  			</li></ol></div>
  	</div>
  	<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="rsync.example"></a>4.1.5. rsync examples</h3></div></div></div>
  		
  		<p><a class="ulink" href="http://samba.anu.edu.au/rsync/examples.html" target="_top">http://samba.anu.edu.au/rsync/examples.html</a></p>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp37"></a>4.1.5.1. upload</h4></div></div></div>
  		
  		<pre class="screen">
  $ rsync -v -u -a --delete --rsh=ssh --stats localfile username@hostname:/home/username/
  		</pre>
  		<p>for example:</p>
  		<p>I want to copy local workspace of eclipse directory to another computer.</p>
  		<pre class="screen">
  $ rsync -v -u -a --delete --rsh=ssh --stats workspace neo@192.168.245.131:/home/neo/
  		</pre>
  	</div>
  	<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp38"></a>4.1.5.2. download</h4></div></div></div>
  		
  		<pre class="screen">
  $ rsync -v -u -a --delete --rsh=ssh --stats neo@192.168.245.131:/home/neo/* /tmp/
  		</pre>
  	</div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp39"></a>4.1.5.3. mirror</h4></div></div></div>
  		
  		<p>rsync使用方法</p>
  		<p>rsync rsync://认证用户@主机/模块</p>
  		<pre class="screen">
  rsync -vzrtopg --progress --delete 认证用户@主机::模块 /mirror目录
  		</pre>
  	</div>
  	<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp40"></a>4.1.5.4. rsync delete</h4></div></div></div>
  			
  			<div class="example"><a id="idp113"></a><p class="title"><strong>例 4.1. examples</strong></p><div class="example-contents">
  				
  				<p>用rsync删除目标目录</p>
  				<div class="literallayout"><p><br />
  				<br />
  mkdir /root/blank<br />
  rsync --delete-before -a -H -v --progress --stats /root/blank/ ./cache/<br />
  				<br />
  				</p></div>
  			</div></div><br class="example-break" />
  		</div>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp41"></a>4.1.5.5. backup to a central backup server with 7 day incremental</h4></div></div></div>
  			
  			<div class="example"><a id="idp114"></a><p class="title"><strong>例 4.2. backup to a central backup server with 7 day incremental</strong></p><div class="example-contents">
  				
  				<pre class="screen">
  				
  #!/bin/sh
  
  # This script does personal backups to a rsync backup server. You will end up
  # with a 7 day rotating incremental backup. The incrementals will go
  # into subdirectories named after the day of the week, and the current
  # full backup goes into a directory called "current"
  # tridge@linuxcare.com
  
  # directory to backup
  BDIR=/home/$USER
  
  # excludes file - this contains a wildcard pattern per line of files to exclude
  EXCLUDES=$HOME/cron/excludes
  
  # the name of the backup machine
  BSERVER=owl
  
  # your password on the backup server
  export RSYNC_PASSWORD=XXXXXX
  
  
  ########################################################################
  
  BACKUPDIR=`date +%A`
  OPTS="--force --ignore-errors --delete-excluded --exclude-from=$EXCLUDES
        --delete --backup --backup-dir=/$BACKUPDIR -a"
  
  export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
  
  # the following line clears the last weeks incremental directory
  [ -d $HOME/emptydir ] || mkdir $HOME/emptydir
  rsync --delete -a $HOME/emptydir/ $BSERVER::$USER/$BACKUPDIR/
  rmdir $HOME/emptydir
  
  # now the actual transfer
  rsync $OPTS $BDIR $BSERVER::$USER/current
  				
  				</pre>
  			</div></div><br class="example-break" />
  		</div>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp42"></a>4.1.5.6. backup to a spare disk</h4></div></div></div>
  			
  			<div class="example"><a id="idp115"></a><p class="title"><strong>例 4.3. backup to a spare disk</strong></p><div class="example-contents">
  				
  				<pre class="screen">
  				
  I do local backups on several of my machines using rsync. I have an
  extra disk installed that can hold all the contents of the main
  disk. I then have a nightly cron job that backs up the main disk to
  the backup. This is the script I use on one of those machines.
  
      #!/bin/sh
  
      export PATH=/usr/local/bin:/usr/bin:/bin
  
      LIST="rootfs usr data data2"
  
      for d in $LIST; do
  	mount /backup/$d
  	rsync -ax --exclude fstab --delete /$d/ /backup/$d/
  	umount /backup/$d
      done
  
      DAY=`date "+%A"`
  
      rsync -a --delete /usr/local/apache /data2/backups/$DAY
      rsync -a --delete /data/solid /data2/backups/$DAY
  
  
  
  The first part does the backup on the spare disk. The second part
  backs up the critical parts to daily directories.  I also backup the
  critical parts using a rsync over ssh to a remote machine.
  				
  				</pre>
  			</div></div><br class="example-break" />
  		</div>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp43"></a>4.1.5.7. mirroring vger CVS tree</h4></div></div></div>
  			
  			<div class="example"><a id="idp116"></a><p class="title"><strong>例 4.4. mirroring vger CVS tree</strong></p><div class="example-contents">
  				
  				<pre class="screen">
  				
  The vger.rutgers.edu cvs tree is mirrored onto cvs.samba.org via
  anonymous rsync using the following script.
  
      #!/bin/bash
  
      cd /var/www/cvs/vger/
      PATH=/usr/local/bin:/usr/freeware/bin:/usr/bin:/bin
  
      RUN=`lps x | grep rsync | grep -v grep | wc -l`
      if [ "$RUN" -gt 0 ]; then
  	    echo already running
  	    exit 1
      fi
  
      rsync -az vger.rutgers.edu::cvs/CVSROOT/ChangeLog $HOME/ChangeLog
  
      sum1=`sum $HOME/ChangeLog`
      sum2=`sum /var/www/cvs/vger/CVSROOT/ChangeLog`
  
      if [ "$sum1" = "$sum2" ]; then
  	    echo nothing to do
  	    exit 0
      fi
  
      rsync -az --delete --force vger.rutgers.edu::cvs/ /var/www/cvs/vger/
      exit 0
  
  Note in particular the initial rsync of the ChangeLog to determine if
  anything has changed. This could be omitted but it would mean that the
  rsyncd on vger would have to build a complete listing of the cvs area
  at each run. As most of the time nothing will have changed I wanted to
  save the time on vger by only doing a full rsync if the ChangeLog has
  changed. This helped quite a lot because vger is low on memory and
  generally quite heavily loaded, so doing a listing on such a large
  tree every hour would have been excessive.
  				
  				</pre>
  			</div></div><br class="example-break" />
  		</div>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp44"></a>4.1.5.8. automated backup at home</h4></div></div></div>
  			
  			<div class="example"><a id="idp117"></a><p class="title"><strong>例 4.5. automated backup at home</strong></p><div class="example-contents">
  				
  				<pre class="screen">
  				
  I use rsync to backup my wifes home directory across a modem link each
  night. The cron job looks like this
  
      #!/bin/sh
      cd ~susan
      {
      echo
      date
      dest=~/backup/`date +%A`
      mkdir $dest.new
      find . -xdev -type f \( -mtime 0 -or -mtime 1 \) -exec cp -aPv "{}"
      $dest.new \;
      cnt=`find $dest.new -type f | wc -l`
      if [ $cnt -gt 0 ]; then
        rm -rf $dest
        mv $dest.new $dest
      fi
      rm -rf $dest.new
      rsync -Cavze ssh . samba:backup
      } &gt;&gt; ~/backup/backup.log 2&gt;&amp;1
  
  
  note that most of this script isn't anything to do with rsync, it just
  creates a daily backup of Susans work in a ~susan/backup/ directory so
  she can retrieve any version from the last week. The last line does
  the rsync of her directory across the modem link to the host
  samba. Note that I am using the -C option which allows me to add
  entries to .cvsignore for stuff that doesn't need to be backed up.
  				
  				</pre>
  			</div></div><br class="example-break" />
  		</div>
  		<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="idp45"></a>4.1.5.9. Fancy footwork with remote file lists</h4></div></div></div>
  			
  			<div class="example"><a id="idp118"></a><p class="title"><strong>例 4.6. Fancy footwork with remote file lists</strong></p><div class="example-contents">
  				
  				<pre class="screen">
  				
  One little known feature of rsync is the fact that when run over a
  remote shell (such as rsh or ssh) you can give any shell command as
  the remote file list. The shell command is expanded by your remote
  shell before rsync is called. For example, see if you can work out
  what this does:
  
  	rsync -avR remote:'`find /home -name "*.[ch]"`' /tmp/
  
  note that that is backquotes enclosed by quotes (some browsers don't
  show that correctly).
  				
  				</pre>
  			</div></div><br class="example-break" />
  		</div>
  	</div>
  	<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="rsync.windows"></a>4.1.6. rsync for windows</h3></div></div></div>
  		
  		<p>http://www.rsync.net/resources/howto/windows_rsync.html</p>
  	</div>
  	<div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="rsync.sh"></a>4.1.7. 多进程 rsync 脚本</h3></div></div></div>
  		
  		<pre class="screen">
  		
  #!/usr/bin/perl
  
  my $path = "/data";          #本地目录
  my $ip="172.16.xxx.xxx";     #远程目录
  my $maxchild=5;              #同时并发的个数
  
  open FILE,"ls $path|";
  while()
  {
  
          chomp;
          my $filename = $_;
          my $i = 1;
          while($i&lt;=1){
                  my $un = `ps -ef |grep rsync|grep -v grep |grep avl|wc -l`;
                  $i =$i+1;
                  if( $un &lt; $maxchild){
                          system("rsync -avl --size-only $path/$_   $ip:$path &amp;") ;
                  }else{
                          sleep 5;
                          $i = 1;
                  }
          }
  }
  		
  		</pre>
  	</div>
  	
  </div>
  
  
  	
  
  
  	
  	
  	
  
  </div><div xmlns="" id="disqus_thread"></div><script xmlns="">
  
  var disqus_config = function () {
  this.page.url = "http://www.netkiller.cn";  // Replace PAGE_URL with your page's canonical URL variable
  this.page.identifier = 'netkiller'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
  };
  
  (function() { // DON'T EDIT BELOW THIS LINE
  var d = document, s = d.createElement('script');
  s.src = '//netkiller.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
  })();
  </script><noscript xmlns="">Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript><br xmlns="" /><script xmlns="" type="text/javascript" id="clustrmaps" src="//cdn.clustrmaps.com/map_v2.js?u=r5HG&amp;d=9mi5r_kkDC8uxG8HuY3p4-2qgeeVypAK9vMD-2P6BYM"></script><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="../ftp/pureftpd.html">上一页</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="tsync.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">3.6. Pure-FTPd + LDAP + MySQL + PGSQL + Virtual-Users + Quota </td><td width="20%" align="center"><a accesskey="h" href="../index.html">起始页</a></td><td width="40%" align="right" valign="top"> 4.2. tsync</td></tr></table></div><script xmlns="">
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  
    ga('create', 'UA-11694057-1', 'auto');
    ga('send', 'pageview');
  
  </script><script xmlns="" async="async">
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?93967759a51cda79e49bf4e34d0b0f2c";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script><script xmlns="" async="async">
  (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
          bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
      }
      else {
          bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script><script xmlns="" type="text/javascript" src="/js/q.js" async="async"></script></body></html>