0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

リモートサーバーとのディレクトリ同士の差分があるかどうか判定する方法

Last updated at Posted at 2024-05-26

結論:rsyncコマンドの--dry-runオプションを使います。

コマンド

以下のコマンドの出力結果でファイルのリストが出てきたらdiffがあります。
(rsyncコマンドは同期を行うコマンドで--dry-runオプションをつけていれば、実際に同期は行われません)

rsync --dry-run -av -e ssh {ローカルパス} リモートホスト:{リモートパス}

例:

kubota_akihiro:test20240326 $ rsync --dry-run -av -e ssh /Users/kubota_akihiro/Documents/2019/ root@ubuntu.local:/home/ubuntu/Documents/2019/
building file list ... done
20240526_Sun.txt

sent 135957 bytes  received 26 bytes  38852.29 bytes/sec
total size is 24477449  speedup is 180.00

考察

  • diffの結果を見るには、以下の記事のようにループを回して、1つずつdiffをとる必要がありそうです。
    実務で使えるdiffのまとめ #diff - Qiita
    https://qiita.com/nakamura-tsuyoshi/items/8bc850e8cc13e96d1a12

  • rsync --dry-run ~の有無によって、処理を分岐したい時

file_list=`rsync --dry-run -av -e ssh /Users/kubota_akihiro/Documents/2019/ root@ubuntu.local:/home/ubuntu/Documents/2019/ \
| sed -n "/building file list ... done/,/sent/p" \
| sed "/building file list ... done\|^$\|sent/d"`

のようにするとfile_listに修正するファイルリストが出力されるので、これを用いて、

if [ -n "$file_list" ]; then
  echo "diffがある時の処理"
else
  echo "diffがない時の処理"
fi

として分岐します。

  • rsyncとscp(cp)の違い
    rsyncとscp(cp)変わらないではないかと思っていましたが、この--dry-runコマンドが使える点が大きく異なります。これで、リモートサーバーとのdiffがあるかどうかがわかります。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?