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?

rsyncの個人的備忘録

0
Posted at

内容

最近はLinuxをメインマシンとして使っています。ストレージのバックアップを、rsyncコマンドを定期的に叩いて行うことにしました。ファイルバックアップ時に使用したコマンドを自分なりにまとめました。

  • 実行する前にrsync -aivh --delete --dry-run <source> <destination>のように、どのファイルが削除されるか、転送されるのか確認できる
    • -iオプションを使うことでどのファイルに何が起こるのか出力する
      • <でリモートサーバへの転送、>はローカル(rsyncを実行する側)へのファイル転送を表す
      • ※>が<source> -> <destination>のようにディレクトリ間の転送方向を表すわけではない
  • rsyncは必ず<source> -> <destination>の方向にファイルをコピーする
    • 例えば、<source>/test.txtファイルより <destination>/test.txtファイルのほうが、ファイル更新日時が新しかったとしても<source>/test.txtが<destination>/test.txtを上書きする(これを防止したければ--updatedオプションを使う)
  • 転送されるファイルのowner:groupは転送元のUID:GIDをそのままコピーする
    • これもまた変更するコマンドがあり--chown=user:groupのようにします
    • dockerのマウントディレクトリをバックアップするときなどに気をつける必要あり

基本的な使い方

ssh設定していればそのホストのファイル同期が簡単にできます 以下のようにホスト名を設定していたとすると、以下のように指定できます。

cat ~/.ssh/config
# ssh設定例
# Host myserver
#   HostName 192.168.1.123
#   Port 22
#   User myuser

rsync -a --delete backup/ myserver:backup/

実行前の確認方法

しばらくrsyncを使っていて、どちらのディレクトリをメインにバックアップしていたのかわからなくなってしまって、コマンドの間違いなどに気が付きました。間違えやすい仕様や、まずは、--dry-runオプション等を使用して確認することが大事です。さらに-iオプションで、今回何が起こるのか、どのファイルを転送してどのファイルが消えるのかを出力することができます。

# 仮実行
rsync -aiv --delete --dry-run
# 出力例
# sending incremental file list
# *deleting   test1.txt
# .d..t...... ./
# >f..tp..... perms.txt
# >f+++++++++ test2.txt
#
# sent 114 bytes  received 41 bytes  310.00 bytes/sec
# total size is 0  speedup is 0.00 (DRY RUN)

-iオプション

この-iオプションで出力される記号の意味は以下になります。

YXcstpoguax
位置 文字 意味
1 Y 更新方法(>、<、. など)
2 X ファイル種別(f, d, L など)
3 c checksum / 内容
4 s size
5 t modification time
6 p permissions
7 o owner
8 g group
9 u reserved
10 a ACL
11 x extended attributes

位置3〜11は、どのような理由(差分があって)でファイルの差分が判定されているのかを表しています。
また、1の更新方法については勘違いしやすいです。

文字 意味
> ファイルが受信側(ローカル側、rsyncを実行している側)へ送られる、ファイルをリモートサーバから受け取る
< ファイルが送信側(リモートサーバ)へ送られるファイルをリモートサーバへ送信する
c ローカルで変更・作成が行われる
h ハードリンクが作成される
. ファイルデータ自体は転送されず、属性だけ変更される
* 通常とは異なるメッセージ(削除など)

-iオプション ローカル同士の場合

ローカルからローカルの転送の場合は>固定のようです。以下、確認してみました。

mkdir local1/
touch local1/test.txt
mkdir local2/
touch local2/test.txt

rsync -aivh --delete --dry-run local1/ local2/
# sending incremental file list
# .d..t...... ./
# >f..t...... test.txt
# 
# sent 90 bytes  received 22 bytes  224.00 bytes/sec
# total size is 0  speedup is 0.00 (DRY RUN)

# <source>と<destination>を逆にしてみる
rsync -aivh --delete --dry-run local2/ local1/
# sending incremental file list
# .d..t...... ./
# >f..t...... test.txt
# 
# sent 88 bytes  received 22 bytes  220.00 bytes/sec
# total size is 0  speedup is 0.00 (DRY RUN)

どちらも以下のような表記になりました。

>f..t...... test.txt

-iオプション リモートへの転送

# ローカルにて以下のコマンドを実行
mkdir -p localsample/
touch -d "2020-01-01 00:00:01" localsample/test.txt

# サーバにて以下のコマンドを実行
mkdir -p serversample/
touch -d "2020-01-01 00:00:00" serversample/test.txt

# ローカルにて以下のコマンドを実行
rsync -avhi --dry-run localsample/ server:serversample/
rsync -avhi --dry-run localsample/ tuna:serversample/
# sending incremental file list
# .d..t...... ./
# <f..t...... test.txt
# 
# sent 84 bytes  received 22 bytes  12.47 bytes/sec
# total size is 0  speedup is 0.00 (DRY RUN)

test.txtは2つのディレクトリ間で更新日時が異なるため、転送の対象となりました。転送方向は<source> -> <dest>なので、localsample/ -> server:serversample/です。よって、リモートサーバ側に送られます。<という表記になりました。

ここで、転送方向を逆にしてみます。

rsync -avhi --dry-run tuna:serversample/ localsample/ 
# receiving incremental file list
# .d..t...... ./
# >f..t...... test.txt
# 
# sent 30 bytes  received 83 bytes  17.38 bytes/sec
# total size is 0  speedup is 0.00 (DRY RUN)

転送方向はserver:serversample/ -> localsample/ です。よって、test.txtはローカル側(このrsyncを実行しているマシン)が受け取ります。>という表記になりました。

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?