rsync - arosh/arosh.github.com GitHub Wiki

まとめ

rsync -rlpt --safe-links --delete --modify-window=-1 --compress --compress-choice=zstd -P --stats --human-readable

末尾のスラッシュの意味

A trailing slash on the source changes this behavior to avoid creating an additional directory level at the destination. You can think of a trailing / on a source as meaning "copy the contents of this directory" as opposed to "copy the directory by name", but in both cases the attributes of the containing directory are transferred to the containing directory on the destination. In other words, each of the following commands copies the files in the same way, including their setting of the attributes of /dest/foo:

転送元パスの末尾のスラッシュは、転送先で追加のディレクトリ階層が作成されるのを避けるようにこの動作を変更します。転送元の末尾の / は、「ディレクトリを名前でコピーする 」という意味ではなく、「このディレクトリの内容をコピーする 」という意味と考えることができます。

rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo

複雑怪奇な include, exclude の解釈

https://tech.nitoyon.com/ja/blog/2013/03/26/rsync-include-exclude/

シンボリックリンクの転送

--links --safe-links を指定すると、シンボリックリンクは原則シンボリックリンクのままコピー、絶対パス指定のシンボリックリンクや親ディレクトリから抜け出すシンボリックリンクはスキップする。

https://download.samba.org/pub/rsync/rsync.1#SYMBOLIC_LINKS

-a

-rlptgoD の意味である。

  • --recursive, -r recurse into directories
  • --links, -l copy symlinks as symlinks
  • --perms, -p preserve permissions
  • --times, -t preserve modification times
  • --group, -g preserve group
  • --owner, -o preserve owner (super-user only)
  • -D same as --devices --specials

重複排除

  • --fuzzy: 同じディレクトリの似たファイルを使ってネットワーク帯域の節約を試みる
  • --compare-dest=DIR: DIR に同じファイルがあれば転送しない
  • --copy-dest=DIR: DIR に同じファイルがあれば、そこからコピーする
  • --link-dest=DIR: DIR に同じファイルがあればハードリンクする

--link-dest など使うとハードリンクを活用して重複排除可能

https://download.samba.org/pub/rsync/rsync.1#opt--link-dest

ナノ秒タイムスタンプ

--modify-window を設定すると、この秒数以下のタイムスタンプの差は無視する、という意味になる。デフォルトではゼロ秒の意味。--modify-window=-1 は特別な値で、新しめのファイルシステムでサポートされているナノ秒単位のタイムスタンプを利用する。

ナノ秒単位のタイムスタンプが利用できるファイルシステムを使っているなら気にせず --modify-window=-1 を使ってよい。これがデフォルトになっていない理由としては、

  • ext4 はナノ秒タイムスタンプをサポートしているが、ext3 はサポートしていない。ext4 から ext3 に rsync するとナノ秒部分がゼロになるので、実際にはファイルが書き換わっていなくても、常にタイムスタンプの差が誤って検出されてしまう
  • この変更は非互換をもたらす

https://github.com/RsyncProject/rsync/commit/839dbff2aaf0277471e1986a3cd0f869e0bdda24

ctime はコピーするべきか?

以下のような理由があってデフォルトや -a ではコピーしないようになっている。

Your OS & filesystem must support the setting of arbitrary creation (birth) times for this option to be supported.

使いそうなオプション一覧

--checksum, -c           skip based on checksum, not mod-time & size
--archive, -a            archive mode is -rlptgoD (no -A,-X,-U,-N,-H)
--no-OPTION              turn off an implied OPTION (e.g. --no-D)
--recursive, -r          recurse into directories

--inplace                update destination files in-place
--append                 append data onto shorter files
--append-verify          --append w/old data in file checksum

--links, -l              copy symlinks as symlinks
--copy-links, -L         transform symlink into referent file/dir
--copy-unsafe-links      only "unsafe" symlinks are transformed
--safe-links             ignore symlinks that point outside the tree
--munge-links            munge symlinks to make them safe & unusable
--copy-dirlinks, -k      transform symlink to dir into referent dir
--keep-dirlinks, -K      treat symlinked dir on receiver as dir
--hard-links, -H         preserve hard links

--perms, -p              preserve permissions
--chmod=CHMOD            affect file and/or directory permissions
--owner, -o              preserve owner (super-user only)
--group, -g              preserve group

--devices                preserve device files (super-user only)
--copy-devices           copy device contents as a regular file
--write-devices          write to devices as files (implies --inplace)
--specials               preserve special files
-D                       same as --devices --specials

--times, -t              preserve modification times
--atimes, -U             preserve access (use) times
--open-noatime           avoid changing the atime on opened files
--crtimes, -N            preserve create times (newness)

--preallocate            allocate dest files before writing
--whole-file, -W         copy files whole (w/o delta-xfer algorithm)
--dry-run, -n            perform a trial run with no changes made

--del                    an alias for --delete-during
--delete                 delete extraneous files from dest dirs
--delete-before          receiver deletes before xfer, not during
--delete-during          receiver deletes during the transfer
--delete-delay           find deletions during, delete after
--delete-after           receiver deletes after transfer, not during
--delete-excluded        also delete excluded files from dest dirs

--force                  force deletion of dirs even if not empty

--numeric-ids            don't map uid/gid values by user/group name
--usermap=STRING         custom username mapping
--groupmap=STRING        custom groupname mapping
--chown=USER:GROUP       simple username/groupname mapping

--ignore-times, -I       don't skip files that match size and time
--modify-window=NUM, -@  set the accuracy for mod-time comparisons

--fuzzy, -y              find similar file for basis if no dest file
--compare-dest=DIR       also compare destination files relative to DIR
--copy-dest=DIR          ... and include copies of unchanged files
--link-dest=DIR          hardlink to files in DIR when unchanged

--compress, -z           compress file data during the transfer
--skip-compress=LIST     skip compressing files with suffix in LIST

--copy-as=USER[:GROUP]   specify user & optional group for the copy
--stats                  give some file-transfer stats
--human-readable, -h     output numbers in a human-readable format

--list-only              list the files instead of copying them
--bwlimit=RATE           limit socket I/O bandwidth
--fsync                  fsync every written file

未読