Tutorial

Rsyncでローカルとリモートのディレクトリを同期する方法

Published on December 1, 2020
Default avatar

By

日本語
Rsyncでローカルとリモートのディレクトリを同期する方法

はじめに

「remote sync(リモート同期)」を表すRsyncは、リモートとローカルでファイルを同期するツールです。変更されたファイルのみを移動して、コピーするデータ量を最小限に抑えるアルゴリズムを採用しています。

このガイドでは、この強力なユティリティの基本的な使い方について取り上げます。

Rsyncとは?

Rsyncは、とても柔軟性に富んだネットワーク対応の同期ツールです。LinuxとUnixのようなシステムにおいて多く普及しており、システムスクリプトのツールとしての人気も高いため、ほとんどのLinuxディストリビューションにデフォルトとして含まれます。

基本的な構文

rsyncの基本的な構文は非常に単純で、操作方法はssh、scp、cpと似ています。

次のコマンドでテストディレクトリを2つとテストファイルをいくつか作成します。

  1. cd ~
  2. mkdir dir1
  3. mkdir dir2
  4. touch dir1/file{1..100}

ここに、dir1というディレクトリと、その中に100個の空ファイルがあります。

  1. ls dir1
Output
file1 file18 file27 file36 file45 file54 file63 file72 file81 file90 file10 file19 file28 file37 file46 file55 file64 file73 file82 file91 file100 file2 file29 file38 file47 file56 file65 file74 file83 file92 file11 file20 file3 file39 file48 file57 file66 file75 file84 file93 file12 file21 file30 file4 file49 file58 file67 file76 file85 file94 file13 file22 file31 file40 file5 file59 file68 file77 file86 file95 file14 file23 file32 file41 file50 file6 file69 file78 file87 file96 file15 file24 file33 file42 file51 file60 file7 file79 file88 file97 file16 file25 file34 file43 file52 file61 file70 file8 file89 file98 file17 file26 file35 file44 file53 file62 file71 file80 file9 file99

また、dir2という空のディレクトリもあります。

同じシステムでdir1の内容をdir2に同期するには、次のように入力します。

  1. rsync -r dir1/ dir2

-rオプションはrecursive(再帰)を表し、ディレクトリ同期に必要です。

代わりに-aフラグを使用することもできます。

  1. rsync -a dir1/ dir2

-aオプションは、コンビネーションフラグです。「archive」を表すこのオプションは、再帰的に同期し、シンボリックリンク、スペシャルファイル、デバイスファイル、変更時刻、グループ、所有者、パーミッションを保持します。-rよりも一般的で、通常はこちらを使用するのがよいでしょう。

重要

お気付きのように、上記のコマンドの1つ目の引数の後に、トレイリングスラッシュ(/)が付いています。

  1. rsync -a dir1/ dir2

これは、「dir1の内容」を表すのに必要なものです。トレイリングスラッシュがないと、dir1がディレクトリごとdir2内に配置されます。これによって以下のような階層が生成されます。

  1. ~/dir2/dir1/[files]

rsyncコマンドを実行する前に、必ず引数を再確認してください。Rsyncでは、-nまたは--dry-runオプションでこうしたチェックができます。-vフラグ(verbose[詳細]の略)も適切な出力の入手に必要です。

  1. rsync -anv dir1/ dir2
Output
sending incremental file list ./ file1 file10 file100 file11 file12 file13 file14 file15 file16 file17 file18 . . .

この出力を、トレイリングスラッシュを削除した場合の出力と比較します。

  1. rsync -anv dir1 dir2
Output
sending incremental file list dir1/ dir1/file1 dir1/file10 dir1/file100 dir1/file11 dir1/file12 dir1/file13 dir1/file14 dir1/file15 dir1/file16 dir1/file17 dir1/file18 . . .

ディレクトリごと転送されるのがわかります。

Rsyncでリモートシステムと同期する方法

リモートシステムとの同期は、リモートマシンにSSHアクセス可能で、双方のマシンにrsyncがインストールされているなら簡単です。2台のマシン間でSSHアクセスが検証されたら、この構文を使用して、前述のdir1フォルダをリモートコンピュータに同期できます(今回はディレクトリごと転送_したい_ので、トレイリングスラッシュを省いていることにご注意ください)。

  1. rsync -a ~/dir1 username@remote_host:destination_directory

これは、ローカルシステムからリモートシステムにディレクトリをプッシュするため、「push」操作といいます。逆の操作は、「pull」 といいます。「pull」はリモートディレクトリをローカルシステムに同期します。dir1がローカルシステムではなくリモートシステムにある場合、構文は次のようになります。

  1. rsync -a username@remote_host:/home/username/dir1 place_to_sync_on_local_machine

cpや類似ツールと同様、転送元は1つ目の引数、転送先は2つ目の引数と決まっています。

Rsyncの便利なオプション

Rsyncには、ユーティリティのデフォルトの動作を変更するオプションが多数用意されています。重要度の高いフラグについてはすでに触れました。

テキストファイルのように未圧縮のファイルを転送している場合、-zオプションを付けて圧縮すると、ネットワーク転送負荷を軽減できます。

  1. rsync -az source destination

-Pフラグは大変便利です。これはフラグ--progress--partialを組み合わせたものです。1つ目は転送の進捗バーを表示するフラグで、2つ目は中断した転送の再開を可能するフラグです。

  1. rsync -azP source destination
Output
sending incremental file list ./ file1 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=99/101) file10 0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=98/101) file100 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=97/101) file11 0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=96/101) . . .

コマンドを再度実行すると、変更がないため出力に時間がかかりません。これは、rsyncが変更時刻を利用して、変更有無を判断できることをよく表しています。

  1. rsync -azP source destination
Output
sending incremental file list sent 818 bytes received 12 bytes 1660.00 bytes/sec total size is 0 speedup is 0.00

一部のファイルで変更時刻を更新すると、rsyncが差分のみを巧みに再コピーするのが確認できます。

  1. touch dir1/file{1..10}
  2. rsync -azP source destination
Output
sending incremental file list file1 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=99/101) file10 0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=98/101) file2 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=87/101) file3 0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=76/101) . . .

2つのディレクトリを真に同期させるには、転送元から削除されたファイルを、転送先ディレクトリでも削除する必要があります。デフォルトでは、rsyncは転送先ディレクトリからファイルを削除しません。

--deleteオプションでこの動作を変更できます。このオプションを使用する前に、-dry-runオプションを使用して、データ損失を防ぐテストを実施してください。

  1. rsync -a --delete source destination

同期するディレクトリ内で、特定のファイルやディレクトリを除外するには、--exclude=オプションに続けてカンマで区切った一覧形式でそれらを指定します。

  1. rsync -a --exclude=pattern_to_exclude source destination

除外を指定したパターンがある場合、それが別のパターンに一致したら、--include=オプションを使用してその除外をオーバーライドできます。

  1. rsync -a --exclude=pattern_to_exclude --include=pattern_to_include source destination

最後に、rsyncの--backupオプションは、重要なファイルのバックアップ保存に使用します。バックアップファイルの保存先ディレクトリを指定する--backup-dirオプションと組み合わせて使用します。

  1. rsync -a --delete --backup --backup-dir=/path/to/backups /path/to/source destination

まとめ

Rsyncは、ネットワーク接続でのファイル転送を容易にし、ローカルディレクトリの同期を安定させます。柔軟性に富むrsyncを使用すれば、様々なファイルレベルの操作も可能になります。

さらに、rsyncに習熟すると複雑なバックアップ操作の設計や、何をどう転送するかについて、きめ細やかな制御ができるようになります。

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel