【Linux】rsync コマンド

公開日時:2019年10月20日 / 最終更新日時:2019年10月20日

様々な機能を持つ rsync コマンドについて解説します。

 

 

rsync のインストール

rsync は yum コマンドでインストールできます。

# yum install rsync

 

※後述しますが、rsync でサーバー間でコピーする場合、コピー元だけでなくコピー先でも rsync をインストールしておく必要があります。両サーバーに rsync がインストールしていないと bash: rsync: command not found というエラーが出力されます。

 

 

 

rsync が利用しているポートは 22/TCP もしくは 873/TCP

rsync の公式サイトを確認すると rsync は 22/TCP もしくは 873/TCP ポートを利用しています。

 

■rsync 公式サイト

https://download.samba.org/pub/rsync/rsync.html

 

■22/TCP(sshのポート)を使うケース

  • can use any transparent remote shell, including ssh or rsh

 

There are two different ways for rsync to contact a remote system: using a remote-shell program as the transport (such as ssh or rsh) or contacting an rsync daemon directly via TCP. The remote-shell transport is used whenever the source or destination path contains a single colon (:) separator after a host specification. Contacting an rsync daemon directly happens when the source or destination path contains a double colon (::) separator after a host specification, OR when an rsync:// URL is specified (see also the "USING RSYNC-DAEMON FEATURES VIA A REMOTE-SHELL CONNECTION" section for an exception to this latter rule).

 

■22/TCP(ssh)を利用する例

# rsync -arvP test001@192.168.0.36:/home/test01/test_20181106.gz /tmp

 

※192.168.0.36: とコロンが1つの場合は ssh(22/TCP)を利用します。

 

 

 

■873/TCP を使うケース

It is also possible to use rsync without a remote shell as the transport. In this case you will directly connect to a remote rsync daemon, typically using TCP port 873. (This obviously requires the daemon to be running on the remote system, so refer to the STARTING AN RSYNC DAEMON TO ACCEPT CONNECTIONS section below for information on that.)

 

■873/TCP(rsyncd)を利用する例①

# rsync -arvP test001@192.168.0.36::/test/test_20181106.gz /tmp

 

■873/TCP(rsyncd)を利用する例②

# rsync -arvP rsync://test001@192.168.0.36/test/test_20181106.gz /tmp

 

※192.168.0.36: とコロンが1つの場合は ssh(22/TCP)を利用します。

 

 

お勧めは 873/TCP を利用しない

ssh のポート(22/TCP)が空いているなら、無理に rsyncd(873/TCP)を利用する必要はないでしょう。

rsyncd.conf でデフォルトのパスが設定されていると、運用で思わぬ動きをする可能性があります。(特にスキルレベルの異なる複数人のチームの場合)

コロン 1つ(:)で 22/TCP(ssh)のポートを使用することでヒューマンエラーを無くす方がいいと思います。

 

 

 

 

rsync コマンドの使い方

様々な場面での rsync の使い方の例です。

 

【基本】

# rsync <ソース> <デスティネーション>

 

 

【例】リモート(192.168.0.3)の /home/test/test-directory ディレクトリ配下すべてをローカルのカレントディレクトリにコピーする

# rsync -arvP --bwlimit=6250 test001@192.168.0.3:/home/test/test-directory/ .

 

■オプション

 

■bwlimit の例

ネットワークの負荷を考慮し、bwlimit で帯域制限を掛けられます。

bwlimit の単位はKBPS(キロバイト/秒)になるので 8で割った数値を割り当てます。

 

■設定したい数値 bwlimitの設置値

 

【例】20GB のデータを転送するとして、帯域制限を掛けた場合の時間

 

【例】567GB のデータを転送するとして、帯域制限を掛けた場合の時間

 

 

【例】リモート(192.168.0.21)の /tmp ディレクトリから ISO ファイルをローカルの /tmp ディレクトリにコピーする

# rsync -arvP --bwlimit=6250 test001@192.168.0.21:/tmp/Windows2016_64bit.ISO /tmp

 

 

 

【例】リモート(192.168.0.12)の /data ディレクトリ配下をローカルのカレントディレクトリにコピーする

# rsync -arvP --bwlimit=12500 root@192.168.0.12:/data .

 

 

 

【例】リモート(192.168.0.36)の /mnt/test_20181106.sql.gz をローカルのカレントディレクトリにコピーする

# rsync -arvP --bwlimit=12500 test001@192.168.0.36:/mnt/test_20181106.sql.gz .

 

 

 

rsync は /(スラッシュ)の使い方に注意

rsync コマンドは /(スラッシュ)の使い方に注意が必要です。

/(スラッシュ)を付けるか付けないかで思わぬ動きをすることがあります。

 

【例】ローカルの /home/test001/log ディレクトリをディレクトリごとコピーして /tmp/test_log ディレクトリの下に持っていく

# rsync -arvpP /home/test001/log /tmp/test_log

 

 

上記のコマンドを実行すると、/tmp/test_log/log ディレクトリが作成されて log ディレクトリの下に各種ログファイルがあるという形になります。

 

 

ミスを起こさないための対策案

私の場合は、ディレクトリごとコピーしたいのか、それともディレクトリはコピーしなくてディレクトリ配下のファイルだけコピーしたいのかにより以下のように使い分けています。

 

 

【例】ディレクトリごとまるっと持っていきたい

# rsync -arvpP /home/test001/log /tmp/test_log

 

コマンド結果は /tmp/test_log/log ディレクトリが作成されて log ディレクトリの下に各種ログファイルがあるという形になります。

 

 

【例】特定のディレクトリ配下のファイル群・ディレクトリ群をまるっと持っていきたい

# rsync -arvpP /home/test001/log/* /tmp/test_log

 

ポイントは /* とスラッシュ(/)プラスアスタリスク(*)を付けていることです。

こうすることで明示的にディレクトリは持っていかないけど、そのディレクトリ配下のファイル群・ディレクトリ群は持っていくということが分かるようになります。

 

 

 

rsync は dry-run を利用できる

rsync は dry-run(ドライラン)を利用できます。

 

■ドライラン

# rsync -anrvP --bwlimit=12500 root@192.168.0.12:/data .

 

 

 

 

rsync コマンドトラブルシューティング

■以下のエラー

[root@test01 ~]# rsync -arvP --bwlimit=6250 test001@192.168.0.21:/tmp/Windows2008R2_64bit.ISO /tmp 
 
The authenticity of host '192.168.0.21 (192.168.0.21)' can't be established. 
ECDSA key fingerprint is xx:xx:xx:xx:xx:xx. 
Are you sure you want to continue connecting (yes/no)? yes 
Warning: Permanently added '192.168.0.21' (ECDSA) to the list of known hosts. 
test001@192.168.0.21's password: 
bash: rsync: command not found 
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver] 
rsync error: remote command not found (code 127) at io.c(605) [Receiver=3.0.9] 
 
[root@test01 ~]#

 

原因はリモートのサーバー上に rsync がインストールされていないことでした。

※rsyncの場合は両サーバー上に rsync がインストールされていないとエラーになります。

 

 

 

 

参考サイト

【rsync】差分同期の仕組みとオプション, ログの見方(フォーマット)

 

 

Posted by 100%レンタルサーバーを使いこなすサイト管理人

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

AlphaOmega Captcha Medica  –  What Do You See?
     
 

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

Secured By miniOrange