<!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" id="sixapart-standard">

<head>
    <meta name="generator" content="Hugo 0.56.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="/css/theme_scrapbook.css" />
    <link rel="start" href="https://wellfrog27.github.io" title="Home" />
    <link rel='shortcut icon' type='image/x-icon' href="https://wellfrog27.github.io/favicon.ico" />
    

    <title>rsync - 阮一峰的网络日志</title>
    <link rel="stylesheet" href="/css/custom.css" />
    
    <script src="/js/click_enlarge_image.js"></script>
    





<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'UA-123-45', 'auto');
	
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>



</head>


<body id="scrapbook" class="mt-entry-archive one-column">

    <div id="container">
        <div id="container-inner">
<div id="header">
    <div id="header-inner">
        <div id="header-content">
            
            <div id="header-name">
                阮一峰的网络日志
                
                <span>» <a href="/blog/" title="首页">首页</a></span>
                
                <span>» <a href="/blog/archives/" title="档案">档案</a></span>
                
            </div>
            
            <div id="google_search" style="background-image:url('/images/searchtop.gif');">
                
                <form action="https://www.baidu.com/s" target="_blank" method="get" id="cse-search-box">
                    <div>
                        <input type="text" size="20" name="origin" class="searchbox" id="sbi" value="">
                        <input type="hidden" name="wd" value="">
                        
                        <input type="image" src="/images/top_search_submit.gif" class="searchbox_submit" value=""
                            alt="搜索" name="sa" onclick="this.form.wd.value = this.form.origin.value + ' 阮一峰'">
                    </div>
                </form>

                
            </div>
            <div id="feed_icon">
                
            </div>

        </div>
    </div>

</div>



<div id="content">
    <div id="content-inner">
        <div id="alpha">
            <div id="alpha-inner">

                <div id="entry-1983" class="entry-asset asset hentry">

                    <div class="asset-header">
                        <div class="asset-nav entry-nav">

                            <div class="entry-location">
                                <ul>
                                    
                                    <li>上一篇：<a href="https://wellfrog27.github.io/blog/weekly/weekly-8/"
                                            title="科技爱好者周刊：第8期">科技爱好者周刊：第8期</a>
                                    </li>
                                    

                                    
                                    <li>下一篇：<a href="https://wellfrog27.github.io/blog/vim/shell/"
                                            title="shell 命令">shell 命令</a>
                                    </li>
                                    

                                </ul>
                            </div>



                            <div class="entry-categories">
                                <p>分类<span class="delimiter">：</span></p>
                                <ul>
                                    <li><a href="https://wellfrog27.github.io/blog/linux/"
                                            rel="tag">Linux</a></li>
                                </ul>
                            </div>




                            <div class="entry-location-mobile" style="float: right;">
                                <ul>
                                    
                                    <li><a href="https://wellfrog27.github.io/blog/weekly/weekly-8/" title="科技爱好者周刊：第8期">⇐</a></li>
                                    
                                    
                                    <li><a href="https://wellfrog27.github.io/blog/vim/shell/" title="shell 命令">⇒</a></li>
                                    
                                </ul>
                            </div>

                        </div>
                    </div>

                    <article class="hentry">
                        <h1 id="page-title" class="asset-name entry-title">rsync</h1>
                        
                        
                        

                        <div class="asset-meta">
                            <p class="vcard author">作者： <a class="fn url" href="/">
                                    
                                    阮一峰
                                    
                                </a></p>
                            <p>日期： <a href="https://wellfrog27.github.io/blog/linux/"><abbr class="published"
                                        title=2015-06-12&#32;00:00:00&#32;&#43;0000&#32;UTC>2015年6月12日</abbr></a>
                            </p>
                        </div>

                        <div class="asset-content entry-content" id="main-content">
                            

<p>rsync 用来在两台计算机或者两个路径之间同步文件。它名称里面的<code>r</code>就是指 remote。</p>

<p>与其他文件传输工具（如 FTP 或 SCP）不同，rsync 检查发送方和接收方上的文件，仅传输同步它们所需的文件。</p>
<div class="highlight"><div style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">$ rsync -a /source/path /target/path</code></pre></td></tr></table>
</div>
</div>
<p>上面的命令会创造一个完全备份。如果目标路径不存在，就会新建。</p>

<p><code>-a</code>参数表示递归复制。</p>

<p>注意，<code>/home/user/dir/</code>和<code>/home/usr/dir</code>不一样。没有最后的斜杠，rsync 将复制整个目录。如果带有最后斜杠，则会拷贝目录下的所有内容，而不会新建这个目录。</p>
<div class="highlight"><div style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">$ rsync -avze ssh /home/user/directory/ user@remote.host.net:/home/user/directory/</code></pre></td></tr></table>
</div>
</div>
<p>上面命令中，<code>-e</code>参数表示使用 SSH 进行远程登录，</p>

<p>rsync 的最大特点就是它可以完成增量备份，也就是只复制有变动的文件。</p>
<div class="highlight"><div style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">$ rsync -a --link-dest /compare/path /source/path /target/path</code></pre></td></tr></table>
</div>
</div>
<p><code>-link-dest</code>参数指定一个比较路径，然后源文件路径跟这个路径进行比较，找出变动的文件，将它们拷贝到目标路径。那些没有变动的文件，会变成指向比较路径的链接。</p>

<p><code>-a</code>参数表示存档模式，保存所有的元数据，比如修改时间（modification time）、权限、所有者，并且软链接也会拷贝成软链接。</p>

<p><code>-v</code>参数表示输出细节。<code>-vv</code>表示输出更详细的信息，<code>-vvv</code>表示输出最详细的信息。</p>

<p><code>-h</code>参数表示以人类可读的格式输出。</p>

<p><code>--exclude</code>参数指定排除不进行复制的文件，比如<code>--exclude=&quot;*.iso&quot;</code>。rsync 会复制“点”开头的隐藏文件，如果要排除隐藏文件，可以这样写<code>--exclude=&quot;.*/&quot;</code>。</p>

<p><code>-z</code>参数用于压缩数据。</p>

<p><code>--delete</code>参数保证源路径删除的文件，在目标路径也同样删除。</p>
<div class="highlight"><div style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#eed;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">$ rsync -avH --delete /etc username@192.168.0.1:backup</code></pre></td></tr></table>
</div>
</div>
<p><code>--progress</code>参数表示显示进展。</p>

<h2 id="参考链接">参考链接</h2>

<ul>
<li><a href="https://www.howtoforge.com/mirroring_with_rsync">Mirror Your Web Site With rsync</a>, by Falko Timme</li>
</ul>

                        </div>

                        <div class="asset-footer">
                            <h3>文档信息</h3>
                            <ul>
                                <li>版权声明：自由转载-非商用-非衍生-保持署名（<a
                                        href="http://creativecommons.org/licenses/by-nc-nd/3.0/deed.zh">创意共享3.0许可证</a>）
                                </li>
                                <li>发表日期： <abbr class="published"
                                        title=".Date">2015年6月12日</abbr></li>
                            </ul>
                        </div>

                    </article>

                </div>

                
                
                <div id="related_entries">

                    <h2>相关文章</h2>
                    <ul>
                        
                        <li><strong>2019.07.15: <a href="https://wellfrog27.github.io/blog/linux/shells/fish-shell/"
                                    title="Fish Shell的用法">Fish Shell的用法</a></strong>

                            <div class="entry-body">
                                暂无描述，请点击阅读全文
                            </div>

                        </li>
                        
                        <li><strong>2019.06.26: <a href="https://wellfrog27.github.io/blog/linux/ssh/port-forwarding/"
                                    title="SSH 端口转发">SSH 端口转发</a></strong>

                            <div class="entry-body">
                                暂无描述，请点击阅读全文
                            </div>

                        </li>
                        
                        <li><strong>2019.03.30: <a href="https://wellfrog27.github.io/blog/linux/basic/"
                                    title="Linux的用法">Linux的用法</a></strong>

                            <div class="entry-body">
                                暂无描述，请点击阅读全文
                            </div>

                        </li>
                        
                        <li><strong>2019.03.09: <a href="https://wellfrog27.github.io/blog/linux/apache/config/"
                                    title="配置文件">配置文件</a></strong>

                            <div class="entry-body">
                                暂无描述，请点击阅读全文
                            </div>

                        </li>
                        
                    </ul>
                </div>
                

            </div>
        </div>
    </div>
</div>



<div id="click-img-container">
    <img src="" alt="Big Image">
</div>




<div id="disqus_thread"></div>
<script type="application/javascript">
    var disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "lin-hao-rong-de-wang-luo-ri-zhi" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>



<div id="footer">
  <div id="footer-inner">
    <div id="footer-content">
      <p>2019 &copy; <a title="电子邮件" href="mailto:yifeng.ruan@gmail.com" target="_blank">联系方式</a>
        
    </div>
  </div>
</div>
</div>
    </div>

    
    
</body>

</html>