文書の表示以前のリビジョンバックリンク文書の先頭へ この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。 ====== rsyncとsshを利用したリモートサーバの高速バックアップ ====== <WRAP zoomimg w300 x1_2 minfont_10> {{:linux:kaki_kanpo_food.jpg?360|ストレスホルモンを抑え、脳内🧠活性酸素を消去する、薬膳食材の牡蠣🤤 - 「厚岸町牡蠣まつり」にて}} <WRAP desc> **ストレスホルモンを抑え、脳内🧠活性酸素を消去する、薬膳食材の牡蠣…🤤 - 「厚岸町牡蠣まつり」にて** (内容とはあまり関係ないイメージです)\\ </WRAP> </WRAP> 本家: [[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を利用するとリモートサーバとの間でディレクトリの同期処理を行える。\\ ==== ローカルディレクトリ → ローカルディレクトリ (ディスク間コピーなど) ==== **ローカルディレクトリ -> ローカルディレクトリ** の転送を行うには、以下のコマンドを実行する😉\\ <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avP</font> <font color="#A347BA">--delete</font> <font color="#A347BA">--copy-links</font> /home/samba/source /mnt/backup/ </pre></html></WRAP> </WRAP> <html><code>-a</code></html> 元のパーミッションやグループなどを保持したまま同期\\ <html><code>-v</code></html> 処理中のファイル名を表示\\ <html><code>-P</code></html> 進行状況を表示\\ <html><code>--delete</code></html> コピー元で削除されたファイルは、コピー先でも削除\\ <html><code>--copy-links</code></html> シンボルリンク先のファイルをコピー\\ <html><code>-z</code></html> データを圧縮する(ローカルコピーでは速度が低下するので指定しない)\\ <html> <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>\\ ==== ローカルディレクトリ → リモートサーバ の転送 ==== **ローカルディレクトリ -> リモートサーバ** の転送を行うには、以下のコマンドを実行する😉 <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avz</font> <font color="#A347BA">--delete</font> <font color="#A347BA">-e</font> ssh ローカルディレクトリ ユーザ@リモートサーバ:転送先フォルダ </pre></html></WRAP> </WRAP> <html><code>-a</code></html> 元のパーミッションやグループなどを保持したまま同期\\ <html><code>-v</code></html> 処理中のファイル名を表示\\ <html><code>-z</code></html> データを圧縮する(回線が遅い場合などに使用)\\ <html><code>--delete</code></html> コピー元で削除されたファイルは、コピー先でも削除\\ <html><code>-e ssh</code></html> リモートシェルを指定\\ <html> <!-- a8net --> <a href="https://px.a8.net/svt/ejp?a8mat=3YYGEZ+9B72CY+3IZO+I2PXE" rel="nofollow">自社で腰を落ち着けて仕事がしたい方は【社内SE転職ナビ】へ</a> <img border="0" width="1" height="1" src="https://www16.a8.net/0.gif?a8mat=3YYGEZ+9B72CY+3IZO+I2PXE" alt=""> </html> ==== リモートサーバ → ローカルディレクトリ のバックアップ ==== **リモートサーバ -> ローカルディレクトリ** のバックアップを行うには、以下のコマンドを実行する😉 <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avz</font> <font color="#A347BA">--delete</font> <font color="#A347BA">-e</font> ssh ユーザ@リモートサーバ:バックアップ元ディレクトリ ローカルバックアップ先ディレクトリ </pre></html></WRAP> </WRAP> ssh の port に 10022を指定する場合は、<html><code>"ssh -p<port>"</code></html>のようにダブルクォーテーションで囲って指定する🤔\\ <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avz</font> <font color="#A347BA">--delete</font> <font color="#A347BA">-e</font> <font color="#A2734C">"ssh -p10022"</font> ユーザ@リモートサーバ:バックアップ元ディレクトリ ローカルバックアップ先ディレクトリ </pre></html></WRAP> </WRAP> 実行例: <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avz</font> <font color="#A347BA">--delete</font> <font color="#A347BA">-e</font> ssh username@remote.server.com:/remote/server/directory/ /local/client/directory/20081016 </pre></html></WRAP> <WRAP color_result><html><pre> username@remote.server.com's password: receiving file list ... done ./ log/ log/geeklog.tomoyan.net.1.log log/geeklog.tomoyan.net.2.log log/geeklog.tomoyan.net.3.log ... sent 2594 bytes received 403518 bytes 14767.71 bytes/sec total size is 140703092 speedup is 346.46 </pre></html></WRAP> </WRAP> <html> <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>\\ ===== rsyncコマンドの元ディレクトリ指定の違いによる動作 ===== ディレクトリ間の同期を行う場合は、元ディレクトリ指定の違いによる動作に注意する😉\\ \\ 元ディレクトリの中身が先ディレクトリに展開される例: <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avz</font> <font color="#A347BA">--delete</font> <font color="#A347BA">-e</font> ssh username@remote.server.com:/remote/server/<b class=DiYE>directory</b><b class=RED>/</b> /local/client/directory/20081016 </pre></html></WRAP> </WRAP> <html><code>/remote/server/<b class=RED>directory/</b></code></html> のように、元ディレクトリの最後に "<html><code><b class=RED>/</b></code></html>" を付けると、<html><code>directory</code></html> の内容が <html><code>20081016</code></html> 内に展開されるかたちで同期される🤔\\ \\ 先ディレクトリに元ディレクトリ名のディレクトリが作成される例: <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avz</font> <font color="#A347BA">--delete</font> <font color="#A347BA">-e</font> ssh username@remote.server.com:/remote/server/<b class=DiYE>directory</b> /local/client/directory/20081016 </pre></html></WRAP> </WRAP> <html><code>/remote/server/<b class=RED>directory</b></code></html> のように、元ディレクトリの最後に "<html><code><b class=RED>/</b></code></html>" を付けないと、<html><code>20081016</code></html> 内に <html><code>directory</code></html> が作成されてその中に同期される🤔\\ ===== rsync でレジューム同期を行うには? ===== <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">-e</font> ssh user@remote.srv.com:/remote/server/directory/ /local/client/directory/20150804 </pre></html></WRAP> </WRAP> <html><code>-P</code></html> --partial --progressと同じ ===== rsync 進行状況を正確に知る ===== <html><code>-P</code></html> または <html><code>--progress</code></html> で ir-chk=3604/<wrap em>183542</wrap> と表示されて、処理中に ir-chk=3005/<wrap em>184250</wrap> のように実行状況の分母が増えて変動するので、__完了までの正確な進行状況が分かりにくい場合がある__😥 ir-chk (for incremental recursion check)\\ \\ <html><code>--no-inc-recursive</code></html> オプションを使うと、最初に全ての処理対象をカウントしてから to-chk=3019/196542 のように実行状況の分母が変動しないので、__完了までの正確な進行状況が分かりやすい__😍\\ <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">--no-inc-recursive</font> <font color="#A347BA">-e</font> ssh user@remote.srv.com:/remote/server/directory/ /local/client/directory/20150804 </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|翻訳]]\\ ===== 冗長でくどい Verbose ...😅 ===== <html><code>-v</code></html> オプションは <html><code>--verbose</code></html> なので...🤔\\ <html><code>-v</code></html> 処理中のファイル名を表示\\ <html><code>-vv</code></html> スキップされたファイル名も表示\\ <html><code>-vvv</code></html> デバッグレベルの処理詳細を表示\\ <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-a<b class=RED>vvv</b>zP</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> </WRAP> ===== 差分転送はわかるけど、どうしてそんなに高速なの?🤔 ===== [[md>@saksenaprashant06/the-algorithm-behind-rsync-76c66a278152|The Algorithm behind Rsync. “we want algorithms that are good in… | by Prashant Saksena | Medium]]\\ 要約するとA, B間でブロック署名をやり取りして、ブロックごとに差分のみを高速転送するアルゴリズムだから...🤔\\ ===== レシピ ===== [[.rsync:fedora_vps_duplicate|Fedora VPS サーバーを rsync で複製する😍]]\\ ===== トラブルシューティング ===== ==== rsync で同期中に Operation not permitted のエラーが出る ==== 以下のようなエラーが出る場合は、rsync がファイルシステムのタイムスタンプやパーミッションをセットする権限がないため発生している。\\ <WRAP color_term> <WRAP color_result><html><pre> rsync: failed to set times on "/var/www/html/.": Operation not permitted (1) </pre></html></WRAP> </WRAP> 権限のあるユーザーで同期するか、sudo を付けて実行するとエラーはでなくなる。\\ <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:single">sudo</u></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avz</font> <font color="#A347BA">--delete</font> <font color="#A347BA">-e</font> ssh user@remote.server.com:/remote/server/directory/ /local/client/directory/ </pre></html></WRAP> </WRAP> ==== rsync で同期中に mkdir "/var/www/html/" failed: No such file or directory (2) のエラーが出る ==== 以下のようなエラーが出る場合は、rsync が ”/var/www/html/” の mkdir に失敗しているため発生している。\\ 対処方法はあらかじめ "/var/www/html/" を用意してあげるか、階層が存在するディレクトリをコピーする。\\ あらかじめ階層を用意するには以下のコマンドを実行する。\\ <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">mkdir</font> <font color="#A347BA">-p</font> <u style="text-decoration-style:single">/var/www/html/</u> </pre></html></WRAP> </WRAP> ==== マウントした NTFS で何もコピーできない ==== 以下のように表示されるだけで中身がなにもコピーされない。\\ <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avz</font> <font color="#A347BA">--delete</font> <font color="#A347BA">-e</font> /var/www/vhosts/ <font color="#A2734C">'/mnt/win_c-drv/Program Files </font><font color="#12488B"><b>(</b></font><font color="#A2734C">x86</font><font color="#12488B"><b>)</b></font><font color="#A2734C">/Ampps/www/vhosts/'</font> </pre></html></WRAP> <WRAP color_result><html><pre> sending incremental file list drwxrwxrwx 0 2019/09/13 05:48:31 . sent 48 bytes received 64 bytes 224.00 bytes/sec total size is 0 speedup is 0.00 </pre></html></WRAP> </WRAP> <html><code>-a</code></html> オプションは **-rlptgoD** を指定した事になる。\\ <html><code>-v</code></html> オプションは **verbose (詳細表示)**\\ <html><code>-z</code></html> オプションは **転送時に圧縮**\\ <html><code>-e</code></html> オプションは **"ssh -p10022" SSH でポートを指定する**\\ この場合マウントした NTFS なので **-e** オプションは不要である。(単純にコマンドオプションのミス😅)\\ \\ <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-avz</font> <font color="#A347BA">--delete</font> /var/www/vhosts/ <font color="#A2734C">'/mnt/win_c-drv/Program Files </font><font color="#12488B"><b>(</b></font><font color="#A2734C">x86</font><font color="#12488B"><b>)</b></font><font color="#A2734C">/Ampps/www/vhosts/'</font> </pre></html></WRAP> <WRAP color_result><html><pre> 〜省略〜 sent 1,243,360,982 bytes received 891,926 bytes 8,378,807.46 bytes/sec total size is 1,626,537,962 speedup is 1.31 </pre></html></WRAP> </WRAP> **-e** オプションを外すとすんなりコピーできた🤣\\ それでもコピー出来ない場合は **-a** オプションを分解して以下を試す。\\ <WRAP color_term> <WRAP color_command><html><pre> <font color="#0087FF"><b>$</b></font> <font color="#26A269">rsync</font> <font color="#A347BA">-rlptvz</font> <font color="#A347BA">--delete</font> /var/www/vhosts/ <font color="#A2734C">'/mnt/win_c-drv/Program Files </font><font color="#12488B"><b>(</b></font><font color="#A2734C">x86</font><font color="#12488B"><b>)</b></font><font color="#A2734C">/Ampps/www/vhosts/'</font> </pre></html></WRAP> <WRAP color_result><html><pre> 〜省略〜 sent 1,243,360,982 bytes received 891,926 bytes 8,378,807.46 bytes/sec total size is 1,626,537,962 speedup is 1.31 </pre></html></WRAP> </WRAP> ===== 参考文献 ===== [[https://linux.die.net/man/1/rsync|rsync(1) - Linux man page]]\\ [[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?]]\\ [[md>@saksenaprashant06/the-algorithm-behind-rsync-76c66a278152|The Algorithm behind Rsync. “we want algorithms that are good in… | by Prashant Saksena | Medium]]\\ linux/rsync.txt 最終更新: 2025/04/24 04:39by ともやん