
宛先ディレクトリに既に存在するサブディレクトリの内容のみを rsync する必要があるディレクトリが 2 つあります。例:
ソースディレクトリ:
- フォルダA
- フォルダB
- フォルダC
- フォルダD
- フォルダE
宛先ディレクトリ:
- フォルダB
- フォルダD
- フォルダZ
フォルダー B とフォルダー D の内容のみをソースから宛先に rsync する必要があります (フォルダー Z はソースに存在しないため、無視する必要があります)。同様に、フォルダー A、C、E を宛先ディレクトリにコピーする必要はありません。
本質的には、「宛先のすべてのサブディレクトリについて、ソースに同じサブディレクトリが存在する場合は、ソースからそのサブディレクトリの内容を rsync する」ということです。
参考になれば、これらはすべてローカル ディレクトリです。
それが意味を成すといいのですが。ご協力ありがとうございます!
答え1
このようなスクリプトを使用できます。
(
cd destination &&
for d in *
do
[ -d "$d" -a -d source/"$d" ] && rsync -a source/"$d" .
done
)
( ... )
スタンドアロンの場合は、ディレクトリの変更をローカライズするためだけに存在するため、括弧は必要ありません。
ソースにファイルがなくなったときに宛先のファイルを削除する場合は、--delete
を追加します。rsync
答え2
次のbash
スクリプトを作成し、ソース ディレクトリと宛先ディレクトリへのパスを変更して実行します。
#!/bin/bash
source=/path_to/source_dir
destination=/path_to/destination_dir
shopt -s nullglob
{
for dir in "$destination/"*/; do
dirname=$(basename "$dir")
if [ -d "$source/$dirname" ]; then
printf '+ /%s/***\n' "$dirname"
fi
done
printf -- '- *\n'
} > "$source/filter.rules"
#rsync -av --filter="merge $source/filter.rules" "$source"/ "$destination"
filter.rules
これにより、ソース ディレクトリに次の内容のファイルが作成されます。
+ /folder B/***
+ /folder D/***
- *
最初の行+ /folder B/***
は、
+ /folder B/
ディレクトリを含める+ /folder B/**
ファイルとサブディレクトリを含める
- *
ルート内のファイルとディレクトリを除外します。
ルールが期待どおりである場合は、最後の行のコメントを解除し、rsync
マージ フィルターを使用してディレクトリに対してスクリプトを再度実行します。
答え3
--existing
探しているのはフラグです。man ページから引用します:
--existing, --ignore-non-existing
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 delete extraneous files).
This option is a transfer rule, not an exclude, so it doesn’t affect the data that
goes into the file-lists, and thus it doesn’t affect deletions. It just limits
the files that the receiver requests to be transferred.