
대상 디렉터리에 이미 존재하는 하위 디렉터리의 내용만 재동기화하는 데 필요한 두 개의 디렉터리가 있습니다. 예를 들어:
소스 디렉터리:
- 폴더 A
- 폴더 B
- 폴더 C
- 폴더 D
- 폴더E
대상 디렉터리:
- 폴더 B
- 폴더 D
- 폴더 Z
폴더 B와 폴더 D의 내용만 소스에서 대상으로 재동기화해야 합니다(폴더 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
당신이 찾고있는 것입니다. 매뉴얼 페이지에서:
--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.