
12가지 실제 예제가 있는 Linux의 Rsync 명령
통사론
rsync [OPTION...] SOURCE... [DESTINATION]
- Here SOURCE can be a local directory like “/opt/backup” or a remote location like “[email protected]:/opt/remotedir/”
- Also the DESTINATION can be refer to a local directory or remote system directory
- Both SOURCE and DESTINATION can refer to a local directory.
- Only one SOURCE or DESTINATION can refer to the remote location. You can’t use both as a remote locations.
재동기화 명령 옵션
Rsync offers a large number of useful options that control each and every aspect of its behavior and make it more flexible for synchronizing files. Below is the list of some frequently used Rsync command options:
-v, -vv -vvv, --verbose
– This option is used for verbose logs on standard output. The default rsync runs silently. Higher the number of “v” increases the logging level.-a, --archive
– Is the most common used option with rsync command. The archive mode is equivalent to the options-rlptgoD
. Basically, it includes all necessary options like, recursively, preserving file permissions, symbolic links, file ownership, and timestamps.-z, -–compress
– Use this option to transfer data in compressed format. Which is useful to save bandwidth.-h, –-human-readable
– Use this option to print all the outputs in a human readable format.--delete
– Use this option only if you need to remove files on destination, which does not exists on source.--exclude=PATTERN
– Exclude files from rsync matching the given pattern.rsync --exclude '*.jpg' SRC/ DEST/
--include=PATTERN
– Include files in rsync matching the given pattern.rsync --include '*.jpg' --include '*.txt' --exclude '*' SRC/ DEST/
재동기화 예
Here are 12 practical examples of the Rsync command in Unix/Linux systems.
- Copy/Sync files and directories local-to-local
- Copy/Sync files and directories local-to-remote
- Copy/Sync files and directories remote-to-local
- Using Rsync over SSH
- Ignore files and directories already exists on destination
Rsync allows synchronizing files between directories on the local system. For example, I have a few backup files on my local machine under the /mnt/backup directory. I want to copy/sync all the files to the USB drive that is mounted on /mnt/disk1. Use the following command to synchronize all files and directories from /opt/backup to /mnt/disk1.
rsync -avhz /opt/backup /mnt/disk1/
sending incremental file list
backup/
backup/.22Mar2022.sql
backup/.22Mar2022.tar.gz
backup/.23Mar2022.sql
backup/.23Mar2022.tar.gz
sent 1.09G bytes received 96 bytes 23.39M bytes/sec
total size is 1.47G speedup is 1.35

Using rsync, we can quickly copy/synchronize files from local to remote systems. For example, I need to transfer a website to a new server. This required website content to be a copy of the remote systems. Use the below command to copy all files from the local /var/www/html directory to remote (192.168.1.100) systems /var/www/html directory.
rsync -avhz /var/www/html [email protected]:/var/www/html/
The above command is helpful for system administrator, who copies files between two systems on regular basis.
The Rsync command also allows the source as a remote directory, but then the destination must be the local. Use the source directory from the remote host and the destination must be the local directory. For example, I need to copy all backup files stored under /backups directory on remote to the local /opt/backups directory.
rsync -avhz [email protected]:/backups /opt/backups
The Rsync connects to remote systems using the Rsync daemon directly via TCP. We can instruct rsync to use Secure shell (SSH) to connect remote systems rather than creating a TCP connection.
Use -e
option to specify the remote shell. The below command will use ssh as a remote shell:
rsync -avhz -e ssh /src [email protected]:/dest/
If the SSH server on destination is running on non-standard port use this tutorial to connect rsync over non-standard port.
Use the Rsync command with --ignore-existing option to ignore all files, that already exist on the destination. For example, you want to schedule a backup script to copy all files to the backup disk daily. In that case, you don't want to copy all files again and again. This option will copy those files, that are not available at the destination.
rsync -avhz --ignore-existing /opt/backups /mnt/disk1
소스가 최신 버전인 경우에만 파일 업데이트
Use --update
최신 버전이 로컬인 경우에만 원격에서 파일을 업데이트하는 옵션. 존재하지 않는 경우 Rsync는 대상에 파일을 생성합니다. 또한 로컬 타임스탬프의 대상에 있는 업데이트 파일은 원격 시스템 파일보다 최신입니다. rsync --update -avhz /opt/backups /mnt/disk1/
동기화 후 소스에서 파일 제거
Use --remove-source-files
대상으로 성공적으로 전송한 후 소스에서 파일을 제거하는 옵션. 예를 들어, 원격 시스템에 대한 로그 파일 백업을 예약해야 합니다. 또한 원격으로 성공적으로 복사한 후 로컬 시스템에서 파일을 원격으로 기록하고 싶었습니다. 이렇게 하면 디스크 공간을 확보하는 데 도움이 됩니다. rsync --remove-source-files -avhz /var/log [email protected]:/backup/logs/
Rsync로 특정 파일 또는 디렉토리 제외
You can specify a file or pattern with --exclude=PATTERN
rsync에서 제외하는 옵션. rsync -avh --exclude '*.zip' --exclude '*.log' /src [email protected]:/dest
Rsync에 특정 파일 포함
The default Rsync includes all files under the source directory tree. Now, you need to include only specific files to synchronize to the destination.
Here you can specify --include=PATTERN
*
를 사용하여 특정 파일을 포함하고 모든 파일을 제외합니다. rsync -avhz -include '*.jpg' --include '*.txt' --exclude '*' /src [email protected]:/dest
Rsync로 진행 상황 표시
Use --progress
Rsync 명령으로 소스와 대상 간의 파일 전송 진행률을 표시하는 옵션. rsync -avh --progress /src [email protected]:/dest
Rsync로 파일 권한 변경
You can instruct the Rsync to change files owner and group owners on destination. Use option --chown=USER:GROUP
Rsync를 사용하여 파일 권한을 변경합니다. rsync -avh --chown=USER:GROUP /src [email protected]:/dest
Similarly, you can use the --chmod=CHMOD option to change file or directory permissions on the destination.
rsync -avh --chmod=755 /src [email protected]:/dest
Rsync 테스트 실행 전용
Use --dry-run
테스트 실행만 실행하는 옵션입니다. 원래 명령과 유사한 결과를 표시하지만 대상에서 아무 것도 업데이트되지 않습니다. 기본적으로 이것을 사용하여 실제로 실행하기 전에 Rsync가 업데이트할 내용을 확인할 수 있습니다. rsync -avh --dry-run /src [email protected]:/dest
결론
Rsync is a great utility for transferring large amounts of data between two machines. You may also use Rsync to back up the entire system at a remote backup location in this article. You've learned some practical Rsync command examples in this article that are great for daily tasks.