bash 스크립트에서 rsync를 사용하여 파일을 몇 대의 서버와 NAS 사이에서 동기화 상태로 유지합니다. 내가 겪었던 한 가지 문제는 rsync 중에 변경된 파일 목록을 생성하는 것입니다.
아이디어는 rsync를 실행할 때 텍스트 파일로 변경된 파일을 메모리의 배열을 더 기대하면서 출력 할 수 있으며 스크립트가 존재하기 전에 변경된 파일 에서만 chown을 실행할 수 있다는 것 입니다.
누구든지 그러한 작업을 수행 할 수있는 방법을 찾았습니까?
# specify the source directory
source_directory=/Users/jason/Desktop/source
# specify the destination directory
# DO NOT ADD THE SAME DIRECTORY NAME AS RSYNC WILL CREATE IT FOR YOU
destination_directory=/Users/jason/Desktop/destination
# run the rsync command
rsync -avz $source_directory $destination_directory
# grab the changed items and save to an array or temp file?
# loop through and chown each changed file
for changed_item in "${changed_items[@]}"
do
# chown the file owner and notify the user
chown -R user:usergroup; echo '!! changed the user and group for:' $changed_item
done
답변
rsync의 --itemize-changes
( -i
) 옵션을 사용하여 다음과 같은 구문 분석 가능한 출력을 생성 할 수 있습니다 .
~ $ rsync src/ dest/ -ai
.d..t.... ./
>f+++++++ newfile
>f..t.... oldfile
~ $ echo 'new stuff' > src/newfile
~ $ !rsync
rsync src/ dest/ -ai
>f.st.... newfile
>
첫 번째 위치에있는 문자가 파일이 업데이트되었음을 나타냅니다, 나머지 문자는 왜, 예를 들어 여기에 표시 s
하고 t
, 파일 크기와 타임 스탬프가 변경된 것을 나타냅니다.
파일 목록을 얻는 빠르고 더러운 방법은 다음과 같습니다.
rsync -ai src/ dest/ | egrep '^>'
분명히 더 고급 구문 분석은 더 깨끗한 출력을 생성 할 수 있습니다 🙂
언제 --itemize-changes
소개되었는지 알아내는 동안이 위대한 링크를 보았습니다 . 매우 유용합니다.
http://andreafrancia.it/2010/03/understanding-the-output-of-rsync-itemize-changes.html (아카이브 링크)
답변
체크섬 플래그 및 플래그와 -n
결합 된 플래그를 사용하십시오 .-c
-i
# rsync -naic test/ test-clone/
>fcst...... a.txt
>fcst...... abcde.txt
>fcst...... b.txt
이 예에서는 파일 자체의 내용에 따라 하나의 파일 만 변경되었습니다. 그러나 -n
플래그로 인해 파일 동기화가 수행되지 않습니다
보너스
변경된 파일에서 chown을 실행하려면 파일을 구문 분석 sed
하거나 유사하게하고 xargs로 처리하십시오. 예를 들면 다음과 같습니다.
rsync -naic test/ test-clone/ | sed 's/............//' | xargs -I+ sudo chown root "test-clone/+"
답변
이 질문은 조금 오래되었지만 추가 할 가치가 있다고 생각합니다.
-i
바로 가기 --out-format=%i%n%L
그리고 %n
수단 파일명 (항목 log format
의 man rsyncd.conf
)
PS rsync 버전 3.1.0