[Linux]너무 쉽고 완벽한 파일 시스템 동기화 하기(rsync)
rsync
는 Remote Synch의 약자로 Delta-transfer 알고리즘을 사용하여 리눅스 및 유닉스 계열 시스템에서 파일 및 디렉터리를 동기화하고 복사하는 명령어입니다. 주로 로컬 및 원격 시스템 간 파일 전송 및 백업에 사용됩니다.
rsync
를 cron
작업과 결합하여 정기적으로 자동 실행하면, 지정된 파일이나 디렉터리의 변경 사항을 실시간으로 탐지하고 자동으로 백업 및 동기화를 수행합니다. 이로써 데이터 손실을 예방하고 중요한 파일을 안전하게 보호할 수 있습니다. 또한 미러링(원본과 대상이 동일하도록 유지)을 쉽게 수행할 수 있는데, 대상 디렉터리에서 소스에 없는 파일을 자동으로 삭제하도록 설정할 수 있습니다. 이는 대상을 소스와 정확히 일치하도록 유지할 수 있는 강력한 기능입니다. 이와 함께 변경된 파일만 전송하는 기능을 제공하며, 이는 대역폭을 절약하고 네트워크 부하를 줄여줍니다. 이 기능은 미러링을 효과적으로 지원하고 중복 파일 전송을 최소화하는 데 도움을 줍니다.
rsync
의 장점이 많지만 주요 옵션들을 살펴보면서 다양한 조합으로 장점을 더욱 빛나게 할 수 있습니다. 특히 기본적으로 ssh 프로토콜을 사용하기 때문에 ssh의 키 기반 인증을 사용하고 계시다면 비밀번호 입력없이 실행시킬 수 있습니다.
기본 구문 :
rsync [옵션] [소스] [대상]
주요 옵션
-a, --archive : -rlptgoD 옵션과 동일
-r, --recursive : 하위 디렉터리까지 실행
-l, --links : 심볼릭 링크를 보존(-L 옵션 비교)
-p, --perms : 권한을 보존
-t, --times : 변경시간을 보존
-g, --group : 소유그룹을 보존
-o, --owner : 소유권을 보존
-D : --devices --specials 옵션과 동일
--devices : character device와 block device 전송
--specials : socket과 fifo와 같은 특별한 파일들도 전송
-b, --backup : 별도의 디렉토리(--backup-dir=DIR)나 suffix(--suffix=SUFFIX)를 가지고 백업 생성
-c, --checksum : 파일 크기/변경 시간를 비교하지 않고 checksum을 비교, 안전하지만 더 느리고 더 많은 자원을 사용
-e : 사용할 remote shell을 지정, 지정하지 않으면 디폴트로 ssh를 사용
-u, --update : 업데이트된 파일만 전송, 소스 파일보다 대상 파일이 더 최신이면 건너뛴다
-n, --dry-run : 변경 사항을 반영하지 않고 테스트로 실행
-L, --copy-links : 심볼릭 링크가 참조하고 있는 원본 파일이나 디렉토리로 변환
-I, --ignore-times: 파일 크기와 변경 시간이 동일한 파일도 덮어쓰기
--progress : 전송하는 동안에 진척상황을 출력
-v, --verbose : 자세히 출력
-z, --compress : 전송할 때 압축
--exclude=PATTERN : 패턴과 부합하는 파일들을 제외
--exclude-from=FILE : 지정한 파일로부터 제외시킨 패턴을 읽음
--include=PATTERN : 패턴과 부합하는 파일을 제외하지 않음
--include-from=FILE : 지정한 파일로부터 포함시킬 패턴을 읽음
--file-from=FILE : 지정한 파일로부터 원본 파일이름을 읽음
--delete : /원본/에서 삭제된 파일을 /대상/에서도 삭제
사용법 :
- 로컬 디렉토리 동기화:
rsync -av /원본/ /대상/
## 로컬 명령 및 결과 출력 ## [root@centos8 ~]# rsync -av testdir/ testdir_backup/ sending incremental file list created directory testdir_backup ./ file0-1.text file0-2.text dir1/ dir1/file1-1.txt dir1/file1-2.txt dir2/ sent 400 bytes received 144 bytes 1,088.00 bytes/sec total size is 28 speedup is 0.05 [root@centos8 ~]# ls -ld test* drwxr-xr-x. 4 root root 70 Aug 15 00:48 testdir drwxr-xr-x. 4 root root 70 Aug 15 00:48 testdir_backup
- 로컬에서 원격 호스트로 파일 전송:
rsync -av /원본/ 사용자@원격호스트:/대상/
## 로컬 명령 및 결과 출력 ## [root@centos8 ~]# rsync -av testdir/ root@rocky9:/root/ sending incremental file list ./ file0-1.text file0-2.text dir1/ dir1/file1-1.txt dir1/file1-2.txt dir2/ sent 412 bytes received 107 bytes 346.00 bytes/sec total size is 28 speedup is 0.05 ## 원격 호스트 결과 확인 ## [root@rocky9 ~]# ls -l testdir total 8 drwxr-xr-x. 2 root root 44 Aug 15 00:49 dir1 drwxr-xr-x. 2 root root 6 Aug 15 00:47 dir2 -rw-r--r--. 1 root root 6 Aug 15 00:46 file0-1.text -rw-r--r--. 1 root root 6 Aug 15 00:46 file0-2.text
- 원격 호스트에서 로컬로 파일 복사:
rsync -av 사용자@원격호스트:/원본/ /대상/
## 원격 호스트 변경 : dir3 디렉토리 생성 ## [root@rocky9 testdir]# ll total 8 drwxr-xr-x. 2 root root 44 Aug 15 00:49 dir1 drwxr-xr-x. 2 root root 6 Aug 15 00:47 dir2 drwxr-xr-x. 2 root root 6 Aug 19 01:42 dir3 -rw-r--r--. 1 root root 6 Aug 15 00:46 file0-1.text -rw-r--r--. 1 root root 6 Aug 15 00:46 file0-2.text ## 로컬 명령 및 결과 확인 ## [root@centos8 ~]# rsync -av root@rocky9:/root/testdir/ testdir/ receiving incremental file list ./ dir3/ sent 33 bytes received 235 bytes 178.67 bytes/sec total size is 28 speedup is 0.10 [root@centos8 ~]# ls -l testdir total 8 drwxr-xr-x. 2 root root 44 Aug 15 00:49 dir1 drwxr-xr-x. 2 root root 6 Aug 15 00:47 dir2 drwxr-xr-x. 2 root root 6 Aug 19 2023 dir3 -rw-r--r--. 1 root root 6 Aug 15 00:46 file0-1.text -rw-r--r--. 1 root root 6 Aug 15 00:46 file0-2.text
- 특정 파일 또는 디렉토리를 제외하고 동기화:
rsync -av --exclude=PATTERN /원본/ /대상/
rsync -av --exclude-from=FILE /원본/ /대상/
## 제외할 패턴 등록 ## [root@centos8 ~]# touch ./testdir/file0-3.text [root@centos8 ~]# rsync -av --exclude='file0-3.*' /root/testdir/ root@rocky9:/root/testdir/ sending incremental file list ./ sent 216 bytes received 22 bytes 476.00 bytes/sec total size is 0 speedup is 0.00 ## 제외할 패턴을 등록한 파일 지정 ## [root@centos8 ~]# echo "file0-3.*" >> ./excludedPattern.txt [root@centos8 ~]# rsync -av --exclude-from='/root/excludedPattern.txt' /root/testdir/ root@rocky9:/root/testdir/ sending incremental file list sent 209 bytes received 15 bytes 448.00 bytes/sec total size is 0 speedup is 0.00
- 동기화시킬 패턴 지정:
rsync -av --include=PATTERN /원본/ /대상/
rsync -av --include-from=FILE /원본/ /대상/
–--exclude="*"
옵션를 포함시켜야 원하는 패턴만 동기화된다.## 동기화에 포함할 패턴 등록 ## [root@centos8 ~]# touch ./testdir/file0-4.text; [root@centos8 ~]# rsync -av --include="file0-4.*" --exclude="*" /root/testdir/ root@rocky9:/root/testdir/ sending incremental file list file0-4.text sent 235 bytes received 38 bytes 546.00 bytes/sec total size is 0 speedup is 0.00 ## 동기화에 포함할 패턴을 등록한 파일 지정 ## [root@centos8 ~]# touch ./testdir/file0-1.txt ./testdir/file0-2.txt ./testdir/file0-3.txt ./testdir/file0-4.txt [root@centos8 ~]# echo "*.txt" >> ./includedPattern.txt [root@centos8 ~]# rsync -av --include-from="/root/includedPattern.txt" --exclude="*" /root/testdir/ root@rocky9:/root/testdir/ sending incremental file list ./ file0-1.txt file0-2.txt file0-3.txt file0-4.txt sent 280 bytes received 95 bytes 750.00 bytes/sec total size is 0 speedup is 0.00
- 지정한 파일 리스트만 동기화:
rsync -av --files-from=FILE /원본/ /대상/
[root@rocky9 ~]# ll ./testdir/ total 0 drwxr-xr-x. 2 root root 44 Aug 28 10:12 dir1 drwxr-xr-x. 2 root root 6 Aug 28 10:12 dir2 drwxr-xr-x. 2 root root 6 Aug 28 10:12 dir3 [root@centos8 ~]# echo -e "./file0-1.txt\n./file0-3.txt" > fileFrom.txt [root@centos8 ~]# rsync -av --files-from="/root/fileFrom.txt" /root/testdir/ root@rocky9:/root/testdir/ building file list ... done ./ file0-1.txt file0-3.txt sent 164 bytes received 57 bytes 442.00 bytes/sec total size is 0 speedup is 0.00
rsync 명령어를 사용해 동기화시키는 쉘 스크립트 사례
vi /files_list.exclude
/source/file1
/source/file2
/source/file3
vi /rsync_exec.sh
#!/bin/bash
# get date
NOW=$(date +"%Y-%m-%d")
EXCLUDE="/files_list.exclude"
# create output file name
OUTPUT="/rsync_exec.log"
exec 3>> $OUTPUT
echo "==============================================" >&3
echo "rsync Date : ${NOW} " >&3
echo "==============================================" >&3
rsync -av --delete --exclude-from=${EXCLUDE} root@dest_host:/source/ /destnation/ >&3
crontab -e
00 06 * * * /rsync_exec.sh