<p style="margin: 1em 0;"><strong>引言</strong><br>
在数据迁移、备份和日常文件同步任务中,<code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">rsync</code> 是 Linux
系统管理员和开发者的瑞士军刀。它通过<strong>增量传输算法</strong>仅同步变化的文件部分,大幅提升效率;保留文件权限、时间戳等元数据,并支持压缩传输。无论是本地还是远程操作,<code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">rsync</code>
都能以最小开销完成任务。本文将带你从安装到实战,全面掌握这一神器!</p>
<hr style="border: none; border-top: 1px solid #1a1a1a; height: 1px; margin: 1em 0;">
<h4 id="一安装-rsync" style="margin-top: 1.4em;">一、安装 rsync</h4>
<p style="margin: 1em 0;"><code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">rsync</code> 通常预装在主流 Linux
发行版中。若未安装,可通过包管理器快速完成:</p>
<h5 id="ubuntudebian" style="margin-top: 1.4em; font-size: 1em; font-style: italic;">Ubuntu/Debian</h5>
<div class="sourceCode" id="cb1" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb1-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb1-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="fu" style="color: #06287e;">sudo</span> apt install rsync <span class="at" style="color: #7d9029;">-y</span></span></code></pre></div>
<h5 id="centosrhel" style="margin-top: 1.4em; font-size: 1em; font-style: italic;">CentOS/RHEL</h5>
<div class="sourceCode" id="cb2" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb2-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb2-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="fu" style="color: #06287e;">sudo</span> yum install rsync <span class="at" style="color: #7d9029;">-y</span></span></code></pre></div>
<h5 id="验证安装" style="margin-top: 1.4em; font-size: 1em; font-style: italic;">验证安装</h5>
<div class="sourceCode" id="cb3" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb3-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb3-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="fu" style="color: #06287e;">rsync</span> <span class="at" style="color: #7d9029;">--version</span></span>
<span id="cb3-2" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb3-2" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="co" style="color: #60a0b0; font-style: italic;"># 输出示例:rsync version 3.2.3 protocol version 31</span></span></code></pre></div>
<hr style="border: none; border-top: 1px solid #1a1a1a; height: 1px; margin: 1em 0;">
<h4 id="二核心使用场景与命令详解" style="margin-top: 1.4em;">二、核心使用场景与命令详解</h4>
<h5 id="本地目录同步" style="margin-top: 1.4em; font-size: 1em; font-style: italic;">1. <strong>本地目录同步</strong></h5>
<p style="margin: 1em 0;">将 <code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">~/Documents/</code> 同步到备份目录(保留所有属性):</p>
<div class="sourceCode" id="cb4" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb4-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb4-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="fu" style="color: #06287e;">rsync</span> <span class="at" style="color: #7d9029;">-av</span> ~/Documents/ /backup/docs/</span></code></pre></div>
<ul style="padding-left: 1.7em; margin-top: 1em;">
<li><strong><code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">-a</code></strong>:归档模式(保留权限、所有者、时间戳等)<br>
</li>
<li><strong><code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">-v</code></strong>:显示详细过程</li>
</ul>
<h5 id="远程同步通过-ssh" style="margin-top: 1.4em; font-size: 1em; font-style: italic;">2. <strong>远程同步(通过 SSH)</strong></h5>
<p style="margin: 1em 0;">将本地目录推送到远程服务器:</p>
<div class="sourceCode" id="cb5" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb5-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb5-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="fu" style="color: #06287e;">rsync</span> <span class="at" style="color: #7d9029;">-avz</span> <span class="at" style="color: #7d9029;">-e</span> <span class="st" style="color: #4070a0;">"ssh -p 2222"</span> ~/project/ user@remote-ip:/path/to/backup/</span></code></pre></div>
<ul style="padding-left: 1.7em; margin-top: 1em;">
<li><strong><code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">-z</code></strong>:启用压缩传输<br>
</li>
<li><strong><code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">-e</code></strong>:指定 SSH 端口(默认 22
可省略)</li>
</ul>
<p style="margin: 1em 0;">从远程拉取文件到本地:</p>
<div class="sourceCode" id="cb6" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb6-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb6-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="fu" style="color: #06287e;">rsync</span> <span class="at" style="color: #7d9029;">-avz</span> user@remote-ip:/remote/path/ ~/local/</span></code></pre></div>
<h5 id="增量备份与清理" style="margin-top: 1.4em; font-size: 1em; font-style: italic;">3. <strong>增量备份与清理</strong></h5>
<p style="margin: 1em 0;">同步时<strong>删除目标端多余文件</strong>(确保两端一致):</p>
<div class="sourceCode" id="cb7" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb7-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb7-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="fu" style="color: #06287e;">rsync</span> <span class="at" style="color: #7d9029;">-av</span> <span class="at" style="color: #7d9029;">--delete</span> ~/source/ ~/destination/</span></code></pre></div>
<h5 id="排除文件目录" style="margin-top: 1.4em; font-size: 1em; font-style: italic;">4. <strong>排除文件/目录</strong></h5>
<p style="margin: 1em 0;">忽略 <code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">node_modules</code> 和 <code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">.log</code> 文件:</p>
<div class="sourceCode" id="cb8" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb8-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb8-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="fu" style="color: #06287e;">rsync</span> <span class="at" style="color: #7d9029;">-av</span> <span class="at" style="color: #7d9029;">--exclude</span><span class="op" style="color: #666666;">=</span><span class="st" style="color: #4070a0;">'node_modules'</span> <span class="at" style="color: #7d9029;">--exclude</span><span class="op" style="color: #666666;">=</span><span class="st" style="color: #4070a0;">'*.log'</span> ~/app/ /backup/app/</span></code></pre></div>
<p style="margin: 1em 0;">使用 <code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">--exclude-from=file.list</code> 可从文件读取排除规则。</p>
<h5 id="限速传输避免带宽耗尽" style="margin-top: 1.4em; font-size: 1em; font-style: italic;">5.
<strong>限速传输(避免带宽耗尽)</strong></h5>
<p style="margin: 1em 0;">限制速度为 500 KB/s:</p>
<div class="sourceCode" id="cb9" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb9-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb9-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="fu" style="color: #06287e;">rsync</span> <span class="at" style="color: #7d9029;">-avz</span> <span class="at" style="color: #7d9029;">--bwlimit</span><span class="op" style="color: #666666;">=</span>500 ~/largefiles/ user@remote:/backup/</span></code></pre></div>
<hr style="border: none; border-top: 1px solid #1a1a1a; height: 1px; margin: 1em 0;">
<h4 id="三高级技巧" style="margin-top: 1.4em;">三、高级技巧</h4>
<ol type="1" style="padding-left: 1.7em; margin-top: 1em;">
<li><p style="margin: 1em 0;"><strong>断点续传</strong><br>
添加 <code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">--partial</code> 选项保留部分传输的文件,下次自动续传。</p>
<div class="sourceCode" id="cb10" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb10-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb10-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="fu" style="color: #06287e;">rsync</span> <span class="at" style="color: #7d9029;">-av</span> <span class="at" style="color: #7d9029;">--partial</span> <span class="at" style="color: #7d9029;">--progress</span> largefile.iso user@remote:/backup/</span></code></pre></div></li>
<li><p style="margin: 1em 0;"><strong>实时同步(结合 inotify)</strong><br>
使用 <code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">inotify-tools</code> 监听文件变化并自动触发同步:</p>
<div class="sourceCode" id="cb11" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb11-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb11-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="cf" style="color: #007020; font-weight: bold;">while</span> <span class="ex">inotifywait</span> <span class="at" style="color: #7d9029;">-r</span> <span class="at" style="color: #7d9029;">-e</span> modify,create,delete ~/data<span class="kw" style="color: #007020; font-weight: bold;">;</span> <span class="cf" style="color: #007020; font-weight: bold;">do</span></span>
<span id="cb11-2" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb11-2" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a> <span class="fu" style="color: #06287e;">rsync</span> <span class="at" style="color: #7d9029;">-avz</span> ~/data/ user@remote:/backup/data/</span>
<span id="cb11-3" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb11-3" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="cf" style="color: #007020; font-weight: bold;">done</span></span></code></pre></div></li>
<li><p style="margin: 1em 0;"><strong>备份快照</strong><br>
按日期创建备份目录,保留历史版本:</p>
<div class="sourceCode" id="cb12" style="background-color: transparent; overflow: visible; margin: 1em 0;"><pre class="sourceCode bash" style="background-color: transparent; overflow: visible; margin: 0;"><code class="sourceCode bash" style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; padding: 0; overflow-wrap: normal; background-color: transparent; overflow: visible; white-space: pre; position: relative;"><span id="cb12-1" style="color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25;"><a href="#cb12-1" aria-hidden="true" tabindex="-1" style="color: #1a1a1a;"></a><span class="fu" style="color: #06287e;">rsync</span> <span class="at" style="color: #7d9029;">-av</span> ~/data/ /backup/data-<span class="va" style="color: #19177c;">$(</span><span class="fu" style="color: #06287e;">date</span> +%Y%m%d<span class="va" style="color: #19177c;">)</span>/</span></code></pre></div></li>
</ol>
<hr style="border: none; border-top: 1px solid #1a1a1a; height: 1px; margin: 1em 0;">
<h4 id="四常见问题排查" style="margin-top: 1.4em;">四、常见问题排查</h4>
<ul style="padding-left: 1.7em; margin-top: 1em;">
<li><strong>权限错误</strong>:添加
<code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">--rsync-path='sudo rsync'</code> 或确保目标目录可写。<br>
</li>
<li><strong>连接超时</strong>:检查防火墙及 SSH
配置(<code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">-e "ssh -o ConnectTimeout=10"</code>)。<br>
</li>
<li><strong>文件跳过</strong>:使用 <code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">--ignore-existing</code> 或
<code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">--update</code> 控制覆盖逻辑。</li>
</ul>
<hr style="border: none; border-top: 1px solid #1a1a1a; height: 1px; margin: 1em 0;">
<h4 id="五总结" style="margin-top: 1.4em;">五、总结</h4>
<p style="margin: 1em 0;"><code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">rsync</code>
凭借其<strong>高效性、灵活性和可靠性</strong>成为数据同步的首选工具。关键优势总结:<br>
- ✅ <strong>增量同步</strong>:仅传输变化部分,节省时间和带宽。<br>
- ✅
<strong>完整性保障</strong>:保留文件属性,支持校验(<code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">-c</code>
选项)。<br>
- ✅ <strong>脚本友好</strong>:易于集成到自动化任务(如 Cron
定时备份)。</p>
<p style="margin: 1em 0;"><strong>最佳实践建议</strong>:<br>
> 生产环境使用前,先在测试目录验证命令效果;远程操作时建议配置 SSH
密钥免密登录;敏感数据通过 <code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">--password-file</code> 或 SSH
隧道保护。</p>
<blockquote style="margin: 1em 0 1em 1.7em; padding-left: 1em; border-left: 2px solid #e6e6e6; color: #606060;">
<p style="margin: 1em 0;"><strong>附录</strong>:官方文档参考 <code style="font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; font-size: 85%; margin: 0; hyphens: manual; white-space: pre-wrap;">man rsync</code> 或访问 <a href="https://rsync.samba.org/" style="color: #1a1a1a;">rsync.samba.org</a></p>
</blockquote>
相关文章