rsync 대상의 기존 하위 디렉터리만

rsync 대상의 기존 하위 디렉터리만

두 개의 디렉터리가 있고 대상 디렉터리에 이미 존재하는 하위 디렉터리의 콘텐츠만 동기화해야 합니다. 예를 들어:

소스 코드 디렉토리:

  • 폴더 A
  • 폴더 B
  • 폴더 C
  • 폴더D
  • 폴더E

대상 디렉터리:

  • 폴더 B
  • 폴더D
  • 폴더

폴더 B와 폴더 D의 고유 콘텐츠를 소스에서 대상으로 동기화해야 합니다(그리고 폴더 Z는 소스에 존재하지 않으므로 무시해야 합니다). 마찬가지로 폴더 A, C, E를 복사하는 데 대상 디렉터리가 필요하지 않습니다.

기본적으로 "대상의 모든 하위 디렉터리에 대해 동일한 하위 디렉터리가 소스에 존재하는 경우 소스에서 해당 하위 디렉터리의 내용을 동기화합니다."

도움이 된다면 이것은 로컬 디렉토리입니다.

이것이 의미가 있기를 바랍니다. 당신의 도움을 주셔서 감사합니다!

답변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.

관련 정보