여전히 rsync. –filter 와 –include/exclude 의 차이점은??

rsync 는 따로 책이 한권 나와도 이상하지 않을 듯한 프로그램인데, 아직 rsync 만 설명한 책이 출판된 책은 없나보다. 다만, Backup/Recovery 를 설명하면서 rsync 도 언급된 책은 있긴 한 모양인데.. 나온지는 꽤 됐다.

아무튼간에, 며칠 째 계속 rsync 와 씨름을 하고 있는데, 이리 저리해서 방법을 알아냈구나! 싶으면, 사실 그 내용이 man page 에 모두 써있음을 발견하게 된다. 이 작업이 계속 반복되고 있는데..
단지 내 실력(영어와 리눅스 모두)이 부족해서라기 보다는, man page 가 너무나 ‘그 들만의 언어’로 씌여있는게 아닐지?

자. 또 알아낸 사항을 적어보자.


지난 번에 글을 쓸 때, --include-from--exclude-from 의 차이가 과연 있는건지 모르겠다는 내용을 언급한 적이 있다. (근데 지금 찾아보니 어디였는지 찾을 수가 없다.)
좀 더 rsync 를 파다가, include/exclude 는 결국 filter 를 좀 더 쉽게 사용하게 만든 형태라는 사실을 알게 되었다.

Note that the –include/–exclude command-line options do not allow the full range of rule parsing as described above — they only allow the specification of include/exclude patterns plus a “!” token to clear the list (and the normal comment parsing when rules are read from a file). If a pattern does not begin with “- ” (dash, space) or “+ ” (plus, space), then the rule will be interpreted as if “+ ” (for an include option) or “- ” (for an exclude op‐ tion) were prefixed to the string. A –filter option, on the other hand, must always con‐ tain either a short or long rule name at the start of the rule.

rsync man page

‘describe above’ 에는 include(+), exclude(-) 말고도 merge, hide, show 등등 다른 여러가지 요소들이 있다.
이 중, Protect 도 있는데 이것을 include 에 포함시키면 오류가 발생하지는 않지만 작동하질 않는다.

예를 들어, –include–file 에 다음과 같은 내용을 포함하는 파일을 연결했다고 가정한다.

P *~
- 다운로드/*/
- 다운로드/.*
+ 다운로드/
+ 다운로드/*
+ 공부/***
+ 문서/***
+ 비디오/***
+ 사진/***
+ 음악/***
+ 읽을거리/***
+ 작업대/***
- *

– 와 + 는 각각 Exclude, Include 이므로 잘 작동한다. 그러나 맨 위에 있는 ‘P *~’ 는 전혀 반응이 없다.
P 는 Protect 로, ~ 로 끝나는 모든 파일을 삭제되지 않도록 보호하라는 의미가 된다. 제대로 동작한다면, rsync 에서 이런 문구를 출력해야 한다.

[generator] protecting file 공부/프로그래밍/언어연습/5.3.1.py~ because of pattern *~
[generator] protecting file 공부/프로그래밍/언어연습/생성자.py~ because of pattern *~
[generator] protecting file 공부/프로그래밍/언어연습/중복연산자.py~ because of pattern *~

그러나 그냥 모두 포함되어 버린다.
이유는 한가지. include/exclude 는 저 명령을 이해하지 못하기 때문.

따라서, – 와 + 를 제외한 다른 명령을 사용하려한다면 --filter 를 사용해줘야 한다.

그런데.. filter 로는 include-from 처럼 외부파일을 쓸 수 없을까?
물론! 쓸 수 있다.
그게 바로 merge 이다.


명령 형식은 이렇다.

rsync --filter "merge /some/path/file.txt"

/some/path/file.txt 에 원하는 내용을 넣는다. 위에 있는 내용이면 충분하다.
또는, man page 에 언급은 없지만, 여기서도 include/exclude-from 과 마찬가지로 Standard Input 을 사용할 수 있다.

rsync --filter="merge -"

위 명령은 파일이 아닌 Std. Input 으로부터 필터 규칙을 읽어오라는 의미가 된다.

알고 나니 간단한데.. 여기까지 오는 길이 결코 순탄하지만은 않았네.

Author: 아무도안

안녕하세요. 글 남겨주셔서 고맙습니다.