Computer >> 컴퓨터 >  >> 프로그램 작성 >> BASH 프로그래밍

Linux rsync 명령, 구문 및 예 [가이드]

재동기화 (r 이모티콘 동기화 ) 명령은 네트워크는 물론 로컬 스토리지 디스크에서 파일을 동기화할 수 있는 파일 복사 도구입니다.

매우 좋기 때문에 널리 퍼져 있습니다. 일반적으로 파일을 백업하고, 파일 서버를 서로 최신 상태로 유지하고, 웹 앱의 코드와 자산을 서버에 배포하는 데 사용됩니다.

동기화 볼 수 있는 거의 모든 파일을 복사할 수 있고 복사할 수 있으며 파일 크기 및 수정 날짜를 기준으로 동기화하여 변경된 사항과 업데이트해야 할 사항을 확인합니다.

재동기화 명령은 중요하며 이 가이드에서 가장 일반적인 사용 시나리오를 다룰 몇 가지 실용적인 응용 프로그램을 시작하도록 하겠습니다. .

구문

rsync OPTIONS SOURCE DESTINATION

참고:

  • OPTIONS는 공백으로 구분된 아래 목록의 옵션 목록입니다.
  • SOURCE는 동기화할 파일 또는 폴더의 소스 경로이며 원격 호스트에 대한 세부 정보를 포함할 수 있습니다.
  • DESTINATION은 파일 또는 폴더를 동기화하려는 대상의 경로이며 원격 호스트에 대한 세부 정보를 포함할 수 있습니다.
  • SOURCE와 DESTINATION은 순서가 맞아야 합니다!

옵션

다음은 rsync에 일반적으로 사용되는 옵션입니다. , 설명서에서 직접:

복사할 내용:

-r, --recursive             Recurse into directories
-R, --relative              Use relative path names
    --exclude=PATTERN       Exclude files matching PATTERN
    --exclude-from=FILE     Read exclude patterns from FILE
-I, --ignore-times          Don't exclude files that match length and time
    --size-only             Only use file size when determining if a file should be transferred
admin@wsxdn.com  --modify-window=NUM     Timestamp window (seconds) for file match (default=0)
    --include=PATTERN       Don't exclude files matching PATTERN
    --include-from=FILE     Read include patterns from FILE

복사 방법:

-n, --dry-run               Perform a trial run with no changes made
-l, --links                 Copy symlinks as symlinks
-L, --copy-links            Transform symlink into referent file/dir
    --copy-unsafe-links     Only "unsafe" symlinks are transformed
    --safe-links            Ignore links outside the destination tree
    --munge-links           Munge symlinks to make them safer
-H, --hard-links            Preserve hard links
    --devices               Preserve device files (super-user only)
    --specials              Preserve special files
-D, --devices --specials    Preserve devices (super-user only) +files
-g, --group                 Preserve group
-o, --owner                 Preserve owner (super-user only)
-p, --perms                 Preserve permissions
    --remove-source-files   Sender removes synchronized files (non-dir)
-t, --times                 Preserve times
-S, --sparse                Handle sparse files efficiently
-x, --one-file-system       Don't cross filesystem boundaries
-B, --block-size=SIZE       Force a fixed checksum block-size (default 700)
-e, --rsh=COMMAND           Specify rsh replacement
    --rsync-path=PATH       Specify path to rsync on the remote machine
    --numeric-ids           Don't map uid/gid values by user/group name
    --timeout=SECONDS       Set IO timeout in seconds
-W, --whole-file            Copy whole files, no incremental checks

목적지 옵션:

-a, --archive               Archive mode equals -rlptgoD (no -H,-A,-X)
-b, --backup                Make backups (see --suffix & --backup-dir)
    --backup-dir=DIR        Make backups into this directory
-z, --compress              Compress file data during the transfer
-c, --checksum              Skip based on checksum, not mod-time & size
-C, --cvs-exclude           Auto ignore files in the same way CVS does
    --existing              Only update files that already exist
    --delete                Delete files that don't exist on the sending side
    --delete-excluded       Also delete excluded files on the receiving side
    --delete-after          Receiver deletes after transfer, not during
    --force                 Force deletion of directories even if not empty
    --ignore-errors         Delete even if there are IO errors
    --max-delete=NUM        Don't delete more than NUM files
    --log-file-format=FMT   Log file transfers using specified format
    --partial               Keep partially transferred files
    --progress              Show progress during transfer
-P                          Equivalent to --partial --progress
    --stats                 Give some file transfer stats
-T  --temp-dir=DIR          Create temporary files in directory DIR
    --compare-dest=DIR      Also compare destination files relative to DIR
-u, --update                Update only (don't overwrite newer files)

기타 옵션:

    --address=ADDRESS       Bind to the specified address
    --blocking-io           Use blocking IO for the remote shell
    --bwlimit=KBPS          Limit I/O bandwidth, KBytes per second
    --config=FILE           Specify alternate rsyncd.conf file (daemon)
    --daemon                Run as a rsync daemon
    --no-detach             Do not detach from the parent (daemon)
    --password-file=FILE    Get daemon-access password from FILE
    --port=PORT             Specify alternate rsyncd port number
-f, --read-batch=FILE       Read batch file
-F, --write-batch=FILE      Write batch file
    --version               Print version number
-v, --verbose               Increase verbosity
-q, --quiet                 Decrease verbosity
-4, --ipv4                  Prefer IPv4
-6, --ipv6                  Prefer IPv6
-h, --help                  Show this help screen

전체 옵션 목록은 rsync를 참조하세요. 실행하여 사용 설명서:

man rsync

예시

아래에서 각 옵션의 의미를 반복하지 않겠습니다. 옵션이 무엇을 의미하는지 확실하지 않은 경우 위의 목록을 다시 참조하십시오. 잘못된 명령을 실행하여 데이터가 손실되는 것을 원하지 않습니다!

로컬에서 로컬로(단일 파일)

다음은 rsync를 사용하여 로컬 시스템의 한 디렉토리에서 다른 디렉토리로 단일 파일을 동기화하는 방법입니다. :

rsync -zvh /path/to/my-file.tar /path/to/destination/

여기에서 모든 예제는 rsync의 가장 일반적인 사용 시나리오인 동기화 디렉터리에 중점을 둡니다.

로컬에서 로컬로(디렉토리)

한 디렉토리의 모든 파일을 동일한 시스템의 다른 디렉토리로 동기화:

rsync -avzh /path/to/source /path/to/destination

-avzh는 무엇입니까? 병합된 옵션입니다. -a -v -z -h를 입력하는 더 빠른 방법입니다.

로컬에서 원격으로(디렉토리)

로컬 소스 디렉토리의 모든 파일을 네트워크를 통해 원격 컴퓨터의 대상으로 동기화:

rsync -avz /path/to/source/ admin@wsxdn.com:/path/at/destination/

참고:

  • 사용자 이름 원격 시스템의 사용자입니다.
  • /path/to/source/ 로컬 컴퓨터의 소스 디렉토리 경로입니다.
  • 192.168.1.11 원격 시스템의 IP 주소 – IP 주소 또는 호스트 이름을 사용할 수 있습니다.
  • /path/at/destination/ 원격 파일 시스템의 경로입니다.
  • 동기화는 rsync를 사용하여 발생합니다. 데몬

SSH(디렉토리)를 사용하여 로컬에서 원격으로

 - 옵션을 사용하면 rsync에서 사용하는 프로토콜을 지정할 수 있습니다. SSH를 사용하여 안전하게 파일을 전송하도록 지정할 수 있습니다.

rsync -avzh -e ssh /path/to/source/ admin@wsxdn.com:/path/at/destination/

로컬로 원격(디렉토리)

네트워크를 통해 원격 컴퓨터의 원본 디렉터리에서 로컬 컴퓨터의 대상으로 모든 파일 동기화:

rsync -avzh admin@wsxdn.com:/path/to/source /path/to/destination

참고:

  • 사용자 이름 원격 시스템의 사용자입니다.
  • 192.168.1.11 원격 시스템의 IP 주소 – IP 주소 또는 호스트 이름을 사용할 수 있습니다.
  • /경로/대상/소스 원격 파일 시스템의 경로입니다.
  • /경로/목적지 로컬 컴퓨터의 대상입니다.
  • 동기화는 rsync를 사용하여 발생합니다. 데몬

SSH를 사용하여 로컬로 원격(디렉토리)

rsync -avzh -e ssh admin@wsxdn.com:/path/to/source /path/to/destination

디렉토리에 표시된 것처럼 로컬에서 원격으로, 원격에서 로컬로, SSH를 사용하여 단일 파일을 동기화할 수도 있습니다.

동기화 시 고려해야 할 기타 사항

대역폭 고려

인터넷을 통해 동기화하는 경우 네트워크의 다른 서비스가 느려지지 않도록 사용되는 대역폭을 제한할 수 있습니다.

rsync -avz --bwlimit 512 /path/to/source/ admin@wsxdn.com:/path/at/destination/

삭제 고려

파일 또는 폴더가 원본에서 제거되었지만 대상에는 존재하는 경우 원본의 정확한 미러가 되도록 대상에서 파일 또는 폴더를 삭제할 수 있습니다. 이 옵션을 사용하기 전에 항상 삭제를 원하는지 확인하십시오.

rsync -avz --delete /path/to/source/ admin@wsxdn.com:/path/at/destination/

파일 포함 또는 제외

파일 패턴을 일치시켜 파일 포함 및 제외 – 동기화하고 싶지 않은 파일이 있는 경우 제외할 수 있습니다.

rsync -avz -e ssh --exclude '*' --include 'keep*' /path/to/source/ admin@wsxdn.com:/path/at/destination/

이 rsync 명령은 'keep-'로 시작하는 파일과 디렉터리만 동기화하고 다른 모든 파일과 디렉터리(*)는 제외합니다.

테스트 실행 고려

테스트 실행 변경될 파일을 인쇄하지만 실제로는 하지 않습니다. 모든 것 – 파일이 동기화되거나 변경되지 않으므로 다양한 옵션을 실험하고 효과를 확인할 수 있습니다.

rsync -avz --dry-run /path/to/source/ admin@wsxdn.com:/path/at/destination/

결론

시스템에서 많은 수의 파일을 이동할 때 Enter 키를 누르기 전에 입력을 확인하십시오. 잘못된 방향으로 동기화하고 중요한 파일을 제거하는 것을 원하지 않습니다!

Linux Shell 명령에 대한 다른 기사를 확인하십시오!