指定されたディレクトリに指定されたrsync除外パターンを適用します。

指定されたディレクトリに指定されたrsync除外パターンを適用します。

以下を使用してrsync一部をミラーリングする設定があります。源泉(リモート)ディレクトリ目的地rsync除外パターンが定義されているいくつかのファイルと場所に加えて、次のものも含まれます。

# Useless files
- thumbs.db
- *.~
- *.tmp
- /*/.cache
- /*/.local/share/trash
# Already rsynced somewhere
- .dropbox
# Medias files
- *.avi
- *.mkv
- *.wav
- *.mp3
- *.bmp
- *.jpg
# System files
- /hiberfil.sys
- /pagefile.sys

スクリプトはWindowsおよびLinuxワークステーションで実行され、パラメータ--deleteを使用してターゲットディレクトリから無関係なファイルを削除する

問題は、除外パターンを「更新」する場合(たとえば、Oggファイルに除外パターンを追加するなど)、ターゲットから既存の*.oggOggファイルを削除するにはrsyncを再実行する必要があることです。

ターゲットディレクトリをクリーンアップするためにソースでrsyncを再実行する必要がないように、特定のディレクトリに除外ルール(一部のルールはワイルドカードを使用するため複雑になる可能性があります)を簡単に適用できるかどうか疑問に思います。

これまで、既定の名前一致を使用してファイルとディレクトリを削除するには、次のことを見つけました。

dirToClean="/var/somedir"
excludeFile="file_to_exclude"

# Delete files and dir of $dirToClean whose basename matches an exclude pattern from $excludeFile
for fileToDelete in $(grep --extended-regexp "^- .*" $excludeFile | sed 's/^- \(.*\)$/\1/'); do
    find "$dirToClean" -iname "$fileToDelete" -exec rm {} \;
done

ソースとターゲットと同じディレクトリを使用してrsyncを実行できますか?

ベストアンサー1

これをしないでください。 rsyncを使用せずにrsyncの包含/除外パターン機能を複製することは非常に悪い考えです。実際、いくつかのパターンは複雑になる可能性があるため、試してはいけません。 rsync自体を使用して一貫した動作を保証し、驚きを最小限に抑えます。

rsyncのマニュアルページから:

--存在、--存在しない場合は無視

          This  tells rsync to skip creating files (including directories)
          that do not exist yet on the destination.   If  this  option  is
          combined  with  the  --ignore-existing  option, no files will be
          updated (which can be useful if all you want to do is to  delete
          extraneous files).

したがって、 --delete --delete-excluded --ignore-existing --ignore-non-existing を使用して実行する必要があり、rsyncは不要なファイルを削除し、他のファイルを更新または削除しません。

おすすめ記事