Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

เชี่ยวชาญ Linux rsync: คำสั่ง ไวยากรณ์ & ตัวอย่างปฏิบัติจริง – คู่มือสมบูรณ์

คำสั่ง rsync (remote synchronization) เป็นเครื่องมือ copy ไฟล์ที่สามารถ synchronize ไฟล์ข้าม local storage disks และผ่าน network

มันได้รับความนิยมเพราะมันดีมาก ใช้กันทั่วไปสำหรับ backup ไฟล์ รักษา file servers ให้ up-to-date กับกัน และ deploy code และ assets สำหรับ web apps ไป servers

rsync จะ copy ไฟล์เกือบทุกไฟล์ที่มองเห็น และ synchronize ตามขนาดไฟล์และ modification date เพื่อดูว่าอะไรเปลี่ยนและอะไรต้อง update

คำสั่ง rsync ใหญ่มาก และ เราจะเริ่มต้นด้วยการประยุกต์ใช้งานที่ครอบคลุมสถานการณ์ทั่วไปที่สุดในคู่มือนี้

ไวยากรณ์

rsync OPTIONS SOURCE DESTINATION

โปรดทราบว่า:

  • OPTIONS คือรายการตัวเลือกจากรายการด้านล่าง คั่นด้วยช่องว่าง
  • SOURCE คือ path ไปยัง source ของไฟล์/โฟลเดอร์ที่จะ synchronize และสามารถรวมรายละเอียด remote host ได้
  • DESTINATION คือ path ไปยัง destination ที่ต้องการ synchronize และสามารถรวมรายละเอียด remote host ได้
  • SOURCE และ DESTINATION ต้องเรียงลำดับถูกต้อง!

ตัวเลือก (Options)

นี่คือตัวเลือกที่ใช้บ่อยสำหรับ rsync จาก manual:

สิ่งที่จะ copy:

-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

วิธีการ copy:

-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

Destination options:

-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)

Misc options:

 --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 user manual โดยรัน:

man rsync

ตัวอย่าง

ผมจะไม่ซ้ำความหมายของแต่ละตัวเลือกด้านล่าง – ตรวจสอบกลับไปที่รายการด้านบนหากไม่แน่ใจ – คุณไม่ต้องการรันคำสั่งผิดและสูญเสียข้อมูล!

Local to Local (Single File)

นี่คือวิธี synchronize ไฟล์เดี่ยวจาก directory หนึ่งไปอีก directory บน local machine ด้วย rsync:

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

จากนี้ ตัวอย่างทั้งหมดจะเน้น sync directories เนื่องจากนั่นคือสถานการณ์ใช้งานทั่วไปที่สุดสำหรับ rsync

Local to Local (Directory)

Synchronize ไฟล์ทั้งหมดจาก source directory ไป destination directory บนเครื่องเดียวกัน:

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

อะไรคือ -avzh? มันคือชุดตัวเลือกรวมกัน แค่พิมพ์ -a -v -z -h เร็วกว่า

Local to Remote (Directory)

Synchronize ไฟล์ทั้งหมดจาก local source directory ไป destination บน remote computer ผ่าน network:

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

โปรดทราบว่า:

  • username คือ user บน remote system
  • /path/to/source/ คือ path ไป source directory บน local computer
  • 192.168.1.11 คือ IP address ของ remote system – ใช้ IP หรือ hostname ได้
  • /path/at/destination/ คือ path บน remote file system
  • Synchronization จะเกิดขึ้นผ่าน rsync daemon

Local to Remote ใช้ SSH (Directory)

ตัวเลือก -e ให้ระบุ protocol ที่ rsync ใช้ ทำให้เราระบุได้ว่าต้องการ transfer ไฟล์ด้วย SSH อย่างปลอดภัย:

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

Remote to Local (Directory)

Synchronize ไฟล์ทั้งหมดจาก source directory บน remote computer ไป destination บน local computer ผ่าน network:

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

โปรดทราบว่า:

  • username คือ user บน remote system
  • 192.168.1.11 คือ IP address ของ remote system – ใช้ IP หรือ hostname ได้
  • /path/to/source คือ path บน remote file system
  • /path/to/destination คือ destination บน local computer
  • Synchronization จะเกิดขึ้นผ่าน rsync daemon

Remote to Local ใช้ SSH (Directory)

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

คุณยังสามารถ sync ไฟล์เดี่ยว local to remote, remote to local, และใช้ SSH ได้เหมือนที่แสดงกับ directories

สิ่งอื่นที่ควรพิจารณาเมื่อ Synchronizing

พิจารณา Bandwidth

หาก synchronize ผ่าน Internet คุณอาจต้องการจำกัด bandwidth เพื่อไม่ให้ชะลอ services อื่นบน network:

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

พิจารณา Deletions

หากไฟล์/โฟลเดอร์ถูกลบที่ source แต่มีอยู่ที่ destination คุณอาจต้องการลบที่ destination เพื่อให้เป็น exact mirror ของ source ตรวจสอบให้แน่ใจว่าคุณแน่ใจก่อนใช้ตัวเลือกนี้:

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

Include หรือ Exclude ไฟล์

Include และ exclude ไฟล์โดยจับคู่ file patterns – หากมีไฟล์ไม่ต้องการ sync ให้ปล่อยไว้:

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

คำสั่ง rsync นี้จะ sync ไฟล์และ directories ที่เริ่มต้นด้วย ‘keep-’ และ exclude ไฟล์/ไดเรกทอรีอื่นๆ (*)

พิจารณา Dry Run

Dry run จะพิมพ์ไฟล์ที่จะเปลี่ยนแปลง แต่ไม่ ทำ จริง – ไม่มีไฟล์ถูก synchronize หรือ altered จึงสามารถลองตัวเลือกต่างๆ และดูผลได้:

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

สรุป

เมื่อย้ายไฟล์จำนวนมากบนระบบของคุณ ตรวจสอบ input ก่อนกด enter – คุณไม่ต้องการ sync ทิศทางผิดและลบไฟล์สำคัญ!

ดูบทความอื่นๆ ของเราสำหรับ Linux Shell commands!