両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン |
linux:rsync [2024/08/25 16:13] – [参考文献] ともやん | linux:rsync [2025/05/01 09:35] (現在) – [複数ディレクトリをリモートへ同期] ともやん |
---|
====== rsyncとsshを利用したリモートサーバの高速バックアップ ====== | ====== rsyncとsshを利用したリモートサーバの高速バックアップ ====== |
<WRAP zoomimg w300 x1_3> | <WRAP zoomimg w300 x1_2 minfont_10> |
{{:linux:kaki_kanpo_food.jpg|ストレスホルモンを抑える漢方食材の牡蠣🤤}} | {{:linux:kaki_kanpo_food.jpg?360|ストレスホルモンを抑え、脳内🧠活性酸素を消去する、薬膳食材の牡蠣🤤 - 「厚岸町牡蠣まつり」にて}} |
| <WRAP desc> |
| **ストレスホルモンを抑え、脳内🧠活性酸素を消去する、薬膳食材の牡蠣…🤤 - 「厚岸町牡蠣まつり」にて** (内容とはあまり関係ないイメージです)\\ |
| </WRAP> |
</WRAP> | </WRAP> |
**ストレスホルモンを抑える漢方食材の牡蠣…🤤**\\ | |
[[windows:rsync|rsync - 高速増分ファイル転送ユーティリティ]](Windows)\\ | |
| |
| 本家: [[https://rsync.samba.org/|rsync - samba.org]]\\ |
| ソースコード: [[git>RsyncProject/rsync|RsyncProject/rsync: An open source utility that provides fast incremental file transfer. It also has useful features for backup and restore operations among many other use cases.]]\\ |
| |
| rsync(アールシンク) は、UNIXシステムにおいて、差分符号化を使ってデータ転送量を最小化し、遠隔地間のファイルやディレクトリの同期を行うアプリケーションソフトウェアである。\\ |
| 類似のプログラムやプロトコルにはない rsync 独自の特徴として、ミラーサイトとの転送が双方向に高々1回で済む点がある。\\ |
| rsync はディレクトリ内容を表示し、ディレクトリやファイルをコピーできる。オプションでデータ圧縮や再帰も指定可能。\\ |
| |
| rsync プロトコルのデーモン rsyncd がデフォルトで使うTCPポート番号は 873 である。\\ |
| rsync はローカルなディレクトリ間の同期にも使えるし、RSH や SSH などのリモートシェル経由でも使える。\\ |
| 後者の場合、rsync のクライアントプログラムはローカルとリモートの両方にインストールされている必要がある。\\ |
| |
| GNU General Public License でリリースされており、フリーソフトウェアである。\\ |
| [[wwjp>Rsync|Rsync - Wikiwand]] より...\\ |
| |
| [[windows:rsync|rsync - 高速増分ファイル転送ユーティリティ]](Windows)\\ |
===== rsync + ssh の基本 ===== | ===== rsync + ssh の基本 ===== |
ここでは、rsyncとsshを利用してリモートサーバのバックアップを取る方法を説明する。\\ | ここでは、rsyncとsshを利用してリモートサーバのバックアップを取る方法を説明する。\\ |
<font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avzP</font> <font color="#A347BA">--delete</font> <font color="#A347BA">--no-inc-recursive</font> <font color="#A347BA">-e</font> ssh user@remote.srv.com:/remote/server/directory/ /local/client/directory/20150804 | <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avzP</font> <font color="#A347BA">--delete</font> <font color="#A347BA">--no-inc-recursive</font> <font color="#A347BA">-e</font> ssh user@remote.srv.com:/remote/server/directory/ /local/client/directory/20150804 |
</pre></html></WRAP> | </pre></html></WRAP> |
| </WRAP> |
| |
| ===== 除外 [--exclude] ===== |
| <WRAP color_term> |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">rsync</font> <font color="#A347BA">-rtlzvogpHAXP</font> <font color="#A347BA">--delete</font> <font color="#A347BA">--exclude</font> <u style="text-decoration-style:solid">/dev/</u> <font color="#A347BA">--exclude</font> <u style="text-decoration-style:solid">/proc/</u> <font color="#A347BA">--exclude</font> <u style="text-decoration-style:solid">/sys/</u> <font color="#A347BA">--exclude</font> <u style="text-decoration-style:solid">/var/run/</u> <font color="#A347BA">--exclude</font> <u style="text-decoration-style:solid">/var/lock/</u> <font color="#A347BA">--exclude</font> ifcfg<font color="#12488B"><b>*</b></font> <font color="#A347BA">--exclude</font> ssh_host_<font color="#12488B"><b>*</b></font> <font color="#A347BA">--exclude</font> fstab <font color="#A347BA">--block-size=4096</font> <font color="#A347BA">-e</font> <font color="#A2734C">"ssh -p12345"</font> <u style="text-decoration-style:solid">/</u> skv0001:/ |
| </pre></html></WRAP> |
| </WRAP> |
| <html><code>--exclude [パターン]</code></html> パターンに一致するものを除外する🤔\\ |
| [[.rsync:fedora_vps_duplicate|Fedora VPS サーバーを rsync で複製する😍]] のような場合に活躍するオプション😉\\ |
| |
| ==== 除外パターンを bash braces '{}' で効率よく書く✍😍 ==== |
| ''%%--%%exclude data/log'' ではなく ''%%--%%exclude=data/log'' で書く必要がある🤔\\ |
| ''%%--%%exclude\ data/log'' スペースをバックスラッシュでエスケープすると、bash はブレース展開をシングルクオートで囲むので rsync で **rsync error: syntax or usage error** になるからである🤔\\ |
| <WRAP color_term> |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avzP</font> <font color="#A347BA">--delete</font> \ |
| <font color="#A347BA">--exclude=</font><font color="#2A7BDE"><b>{</b></font><font color="#A347BA">data/log,lib/plugins/codemirror,lib/tpl/tomoyannet,lib/tpl/tomoyannet_lite</font><font color="#2A7BDE"><b>}</b></font> \ |
| <font color="#A347BA">--exclude=data/cache/</font><font color="#2A7BDE"><b>{</b></font><font color="#33D17A"><b>{</b></font><font color="#A347BA">0..9</font><font color="#33D17A"><b>}</b></font><font color="#A347BA">,</font><font color="#33D17A"><b>{</b></font><font color="#A347BA">a..f</font><font color="#33D17A"><b>}</b></font><font color="#2A7BDE"><b>}</b></font> \ |
| <font color="#A347BA">-e</font> ssh \ |
| skv0001://var/www/vhosts/www.tomoyan.net/dokuwiki/ \ |
| <u style="text-decoration-style:solid">~/my_projects/tomoyan.net/sites/tomoyan.net/dokuwiki</u> |
| </pre></html></WRAP> |
| ↓ |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>+</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avzP</font> <font color="#A347BA">--delete</font> \ |
| <font color="#A347BA">--exclude</font>=data/log <font color="#A347BA">--exclude</font>=lib/plugins/codemirror <font color="#A347BA">--exclude</font>=lib/tpl/tomoyannet <font color="#A347BA">--exclude=</font>=lib/tpl/tomoyannet_lite \ |
| <font color="#A347BA">--exclude=</font>=data/cache/0 <font color="#A347BA">--exclude</font>=data/cache/1 <font color="#A347BA">--exclude</font>=data/cache/2 <font color="#A347BA">--exclude</font>=data/cache/3 <font color="#A347BA">--exclude</font>=data/cache/4 <font color="#A347BA">--exclude</font>=data/cache/5 <font color="#A347BA">--exclude</font>=data/cache/6 <font color="#A347BA">--exclude</font>=data/cache/7 <font color="#A347BA">--exclude</font>=data/cache/8 <font color="#A347BA">--exclude</font>=data/cache/9 <font color="#A347BA">--exclude</font>=data/cache/a <font color="#A347BA">--exclude</font>=data/cache/b <font color="#A347BA">--exclude</font>=data/cache/c <font color="#A347BA">--exclude</font>=data/cache/d <font color="#A347BA">--exclude</font>=data/cache/e <font color="#A347BA">--exclude</font>=data/cache/f \ |
| -e ssh \ |
| skv0001://var/www/vhosts/www.tomoyan.net/dokuwiki/ \ |
| /home/tomoyan/my_projects/tomoyan.net/sites/tomoyan.net/dokuwiki |
| </pre></html></WRAP> |
| </WRAP> |
| |
| [[dt>rpalo/bash-brackets-quick-reference-4eh6|Bash Brackets Quick Reference - DEV Community]] [[gtr>https://dev.to/rpalo/bash-brackets-quick-reference-4eh6|翻訳]]\\ |
| |
| ==== 複数ディレクトリをリモートへ同期 ==== |
| <WRAP color_term> |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avP<b class=DiYE>R</b></font> <font color="#A347BA">--no-inc-recursive</font> <font color="#A347BA">--delete</font> <font color="#A347BA">-e</font> <font color="#A2734C">"ssh -p20022"</font> /var/cache/<font color="#2A7BDE"><b>{</b></font>dnf,libdnf5<font color="#2A7BDE"><b>}</b></font> root@wicked-beat:/ |
| </pre></html></WRAP> |
| <WRAP color_result><html><pre> |
| building file list ... |
| 1444 files to consider |
| /var/cache/libdnf5/updates-e19adde8fd271134/packages/ |
| /var/cache/libdnf5/updates-e19adde8fd271134/packages/amd-gpu-firmware-20250410-1.fc41.noarch.rpm |
| 26,173,116 100% 383.94MB/s 0:00:00 (xfr#1, to-chk=775/1444) |
| /var/cache/libdnf5/updates-e19adde8fd271134/packages/amd-ucode-firmware-20250311-1.fc41.noarch.rpm |
| 356,123 100% 4.30MB/s 0:00:00 (xfr#2, to-chk=774/1444) |
| /var/cache/libdnf5/updates-e19adde8fd271134/packages/amd-ucode-firmware-20250410-1.fc41.noarch.rpm |
| 357,604 100% 2.82MB/s 0:00:00 (xfr#3, to-chk=773/1444) |
| /var/cache/libdnf5/updates-e19adde8fd271134/packages/android-tools-35.0.2-2.fc41.x86_64.rpm |
| 2,124,004 100% 2.05MB/s 0:00:00 (xfr#4, to-chk=772/1444) |
| ... |
| </pre></html></WRAP> |
| </WRAP> |
| ''-R'', ''--relative'' 相対パス名を使用する\\ |
| |
| **送信されるパス情報の量を削減する...🤔**\\ |
| <WRAP color_term> |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avRP</font> <font color="#A347BA">--no-inc-recursive</font> <font color="#A347BA">--delete</font> <font color="#A347BA">-e</font> <font color="#A2734C">"ssh -p20022"</font> /var/cache/<b class=DiYE>./</b><font color="#2A7BDE"><b>{</b></font>dnf,libdnf5<font color="#2A7BDE"><b>}</b></font> root@wicked-beat://var/cache/ |
| </pre></html></WRAP> |
| <WRAP color_result><html><pre> |
| building file list ... |
| 1442 files to consider |
| libdnf5/updates-e19adde8fd271134/packages/ |
| libdnf5/updates-e19adde8fd271134/packages/appstream-data-41-11.fc41.noarch.rpm |
| 16,048,246 100% 3.86MB/s 0:00:03 (xfr#1, to-chk=769/1442) |
| libdnf5/updates-e19adde8fd271134/packages/arm-none-eabi-gcc-cs-14.1.0-4.fc41.x86_64.rpm |
| 281,265,139 100% 2.22MB/s 0:02:00 (xfr#2, to-chk=768/1442) |
| libdnf5/updates-e19adde8fd271134/packages/at-spi2-atk-2.54.1-1.fc41.i686.rpm |
| 95,484 100% 825.19kB/s 0:00:00 (xfr#3, to-chk=767/1442) |
| libdnf5/updates-e19adde8fd271134/packages/at-spi2-atk-2.54.1-1.fc41.x86_64.rpm |
| 88,221 100% 700.43kB/s 0:00:00 (xfr#4, to-chk=766/1442) |
| ... |
| </pre></html></WRAP> |
| </WRAP> |
| |
| <WRAP mincode> |
| It is also possible to limit the amount of path information that is sent as |
| implied directories for each path you specify. With a modern rsync on the sending side (beginning with 2.6.7), you can insert a dot and a slash into the source path, like this: |
| CW rsync -avR /foo/./bar/baz.c remote:/tmp/ |
| \\ |
| 送信されるパス情報の量を制限することも可能です |
| 指定する各パスの暗黙のディレクトリ。送信側に最新のRSYNCを使用すると(2.6.7で始まる)、このようなソースパスにドットとスラッシュを挿入できます。 |
| cw rsync -avr /foo/./bar/baz.c remote:/tmp/ |
</WRAP> | </WRAP> |
| |
[[md>@saksenaprashant06/the-algorithm-behind-rsync-76c66a278152|The Algorithm behind Rsync. “we want algorithms that are good in… | by Prashant Saksena | Medium]]\\ | [[md>@saksenaprashant06/the-algorithm-behind-rsync-76c66a278152|The Algorithm behind Rsync. “we want algorithms that are good in… | by Prashant Saksena | Medium]]\\ |
要約するとA, B間でブロック署名をやり取りして、ブロックごとに差分のみを高速転送するアルゴリズムだから...🤔\\ | 要約するとA, B間でブロック署名をやり取りして、ブロックごとに差分のみを高速転送するアルゴリズムだから...🤔\\ |
| |
| ===== レシピ ===== |
| [[.rsync:fedora_vps_duplicate|Fedora VPS サーバーを rsync で複製する😍]]\\ |
| |
===== トラブルシューティング ===== | ===== トラブルシューティング ===== |
| |
===== 参考文献 ===== | ===== 参考文献 ===== |
<html> | [[https://linux.die.net/man/1/rsync|rsync(1) - Linux man page]]\\ |
<!-- a8net --> | |
<a href="https://px.a8.net/svt/ejp?a8mat=3YYGEZ+9B72CY+3IZO+I3D2Q" rel="nofollow">選ばれて10年。エンジニアの気持ちが分かる転職エージェントは【アイムファクトリー】</a> | |
<img border="0" width="1" height="1" src="https://www18.a8.net/0.gif?a8mat=3YYGEZ+9B72CY+3IZO+I3D2Q" alt=""> | |
</html>\\ | |
[[http://www.maruko2.com/mw/rsync_%E3%81%A7%E3%83%87%E3%82%A3%E3%83%AC%E3%82%AF%E3%83%88%E3%83%AA%E3%81%AE%E5%90%8C%E6%9C%9F%EF%BC%88%E3%83%90%E3%83%83%E3%82%AF%E3%82%A2%E3%83%83%E3%83%97%EF%BC%89|rsync でディレクトリの同期(バックアップ) - maruko2 Note.]]\\ | [[http://www.maruko2.com/mw/rsync_%E3%81%A7%E3%83%87%E3%82%A3%E3%83%AC%E3%82%AF%E3%83%88%E3%83%AA%E3%81%AE%E5%90%8C%E6%9C%9F%EF%BC%88%E3%83%90%E3%83%83%E3%82%AF%E3%82%A2%E3%83%83%E3%83%97%EF%BC%89|rsync でディレクトリの同期(バックアップ) - maruko2 Note.]]\\ |
[[http://superuser.com/questions/534862/what-does-rsyncs-output-tell-here-about-to-chk|What does Rsync's output tell here about to-chk?]]\\ | [[http://superuser.com/questions/534862/what-does-rsyncs-output-tell-here-about-to-chk|What does Rsync's output tell here about to-chk?]]\\ |
[[md>@saksenaprashant06/the-algorithm-behind-rsync-76c66a278152|The Algorithm behind Rsync. “we want algorithms that are good in… | by Prashant Saksena | Medium]]\\ | [[md>@saksenaprashant06/the-algorithm-behind-rsync-76c66a278152|The Algorithm behind Rsync. “we want algorithms that are good in… | by Prashant Saksena | Medium]]\\ |
| |