MAGAZINE

キャリテク!マガジン

コラム

転ばぬ先のバックアップ (8) 外部ホストへファイルをバックアップしよう – rsync編 2

こんにちは。株式会社パイプラインの濱田です。
前回はrsyncを利用し始めるまでの手順を中心に取り上げましたが、今回はrsyncコマンドを利用したファイル転送の詳細について取り上げます。

rsyncによるファイル転送

今回は2つのホスト「www-01」から「www-02」へ /home/nullpopopo/BACKUP ディレクトリ以下を転送します。rsyncのコマンドはどちらから実行してもよいのですが、www-02で実行することにします。
まずはwww-01の /home/nullpopopo/BACKUP ディレクトリの中身を確認してみましょう。

[nullpopopo@www-01 ~]$ cd ${HOME}/BACKUP/
[nullpopopo@www-01 BACKUP]$ ll
total 40
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-00.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-01.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-02.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-03.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-04.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-05.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-06.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-07.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-08.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-09.txt

test-00.txt から test-09.txt ファイルまでがあります。

それでは、www-02でrsyncを実行します。rsyncを行う前に、 /home/nullpopopo/BACKUP ディレクトリを作成し、作成したディレクトリへカレントディレクトリを移動しています。以下例ではwww-01のIPアドレスを10.0.2.1と表記していますが、実環境に合わせて適宜読み替えてください。(コマンド行は折り返されて複数行になっている場合もありますが、実際には一行です)

[nullpopopo@www-02 ~]$mkdir -p ${HOME}/BACKUP ; cd ${HOME}/BACKUP ; rsync -auv --rsync-path='sudo rsync' --delete -e "ssh -o
StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${HOME}/.ssh/id_rsa" nullpopopo@10.0.2.1:$(pwd)/ .

はじめてrsyncする、すなわちwww-02の /home/nullpopopo/BACKUP ディレクトリの中身が空の状態でのコマンド出力結果は以下の通りです。

receiving incremental file list
./
test-00.txt
test-01.txt
test-02.txt
test-03.txt
test-04.txt
test-05.txt
test-06.txt
test-07.txt
test-08.txt
test-09.txt
 
sent 217 bytes  received 729 bytes  1,892.00 bytes/sec
total size is 49  speedup is 0.05

www-02側でファイル一覧を見てみましょう。

[nullpopopo@www-02 BACKUP]$ll

出力結果は以下の通りです。

total 28
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-00.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 0 Feb 15 23:15 test-01.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 0 Feb 15 23:15 test-02.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 0 Feb 15 23:15 test-03.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-04.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-05.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-06.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-07.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-08.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-09.txt

再度www-02側でrsyncコマンドを実行します。(コマンド行は折り返されて複数行になっている場合もありますが、実際には一行です)

[nullpopopo@www-02 BACKUP]$mkdir -p ${HOME}/BACKUP ; cd ${HOME}/BACKUP ; rsync -auv --rsync-path='sudo rsync' --delete -e "ssh -o
StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${HOME}/.ssh/id_rsa" nullpopopo@10.0.2.1:$(pwd)/ .

出力結果は以下の通りです。

receiving incremental file list  

sent 20 bytes  received 255 bytes  550.00 bytes/sec
total size is 49  speedup is 0.18

www-01 www-02ともにファイルの更新がないため、ファイルは1つも転送されませんでした。

rsyncによる差分ファイル転送

次に、rsyncがディレクトリ内の差分のみを転送することを確認するため、www-01側でいくつかのファイル test-01.txt test-02.txt test-03.txtを削除してみます。

[nullpopopo@www-01 ~]$cd ${HOME}/BACKUP/ ; rm -f test-0{1..3}.txt ; ll

出力結果は以下の通りです。

total 28
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-00.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-04.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-05.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-06.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-07.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-08.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-09.txt

ふたたび、www-02側でrsyncを実行します。(コマンド行は折り返されて複数行になっている場合もありますが、実際には一行です)

[nullpopopo@www-02 ~]$mkdir -p ${HOME}/BACKUP ; cd ${HOME}/BACKUP ; rsync -auv --rsync-path='sudo rsync' --delete -e "ssh -o
StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${HOME}/.ssh/id_rsa" nullpopopo@10.0.2.1:$(pwd)/ .

出力結果は以下の通りです。

receiving incremental file list
deleting test-03.txt
deleting test-02.txt
deleting test-01.txt
./  

sent 27 bytes  received 191 bytes  145.33 bytes/sec
total size is 49  speedup is 0.22

www-01側で削除された3つのファイルが削除されています。

www-02側でファイル一覧を見てみましょう。

[nullpopopo@www-02 BACKUP]$ll

出力結果は以下の通りです。

total 28
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-00.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-04.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-05.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-06.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-07.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-08.txt
-rw-rw-r-- 1 nullpopopo nullpopopo 7 Feb 15 20:54 test-09.txt

www-01側で削除された3つのファイルが削除されています。これはrsyncコマンドの「--delete」オプションをつけている効果なのです。

rsyncは単なる片方向ファイルコピーだけではなく、同期元と同期先のファイルに差分がなければ無駄な転送をせず、差分発生時には差分のみを転送してくれる、ネットワークにやさしいコマンドであることがおわかりいただけたかと思います。

まずはエンジニアデビューしたい、という方は、3ヶ月間学びながらお給料が貰える AltX キャリテク!の門を叩いてみてはいかがでしょう。
https://www.kcct.co.jp/careetec/seminar/tokyo/

過去の連載記事

  1. 転ばぬ先のバックアップ (7) 外部ホストへファイルをバックアップしよう - rsync編 1
  2. 転ばぬ先のバックアップ (6) 外部ホストへファイルをバックアップしよう - sftp 自動実行編
  3. 転ばぬ先のバックアップ (5) 外部ホストへファイルをバックアップしよう - sftp 編
  4. 転ばぬ先のバックアップ (4) 複数のファイルを圧縮・解凍・確認する – zip編
  5. 転ばぬ先のバックアップ (3) 複数のファイルを圧縮・解凍・確認する – tar.gz 編
  6. 転ばぬ先のバックアップ (2) testコマンドとbashの条件分岐 補足解説
  7. 転ばぬ先のバックアップ (1) 作業前のバックアップ取得を習慣づけよう
facebook シェアシェア
LINE シェアシェア