Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม BASH

คำสั่ง Linux rsync ไวยากรณ์และตัวอย่าง [คำแนะนำ]

rsync ( การซิงโครไนซ์ ) คำสั่งเป็นเครื่องมือคัดลอกไฟล์ที่สามารถซิงโครไนซ์ไฟล์ระหว่างดิสก์จัดเก็บข้อมูลในเครื่องและบนเครือข่ายได้

เป็นที่แพร่หลายเพราะมันดีมาก โดยทั่วไปจะใช้สำหรับการสำรองไฟล์ ทำให้เซิร์ฟเวอร์ไฟล์อัปเดตซึ่งกันและกัน และสำหรับการปรับใช้โค้ดและทรัพย์สินสำหรับเว็บแอปไปยังเซิร์ฟเวอร์

rsync สามารถและจะคัดลอกเกือบทุกไฟล์ที่มองเห็น และจะซิงโครไนซ์ตามขนาดไฟล์และวันที่แก้ไข เพื่อดูว่ามีอะไรเปลี่ยนแปลงและต้องอัปเดตอะไรบ้าง

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

ไวยากรณ์

rsync OPTIONS SOURCE DESTINATION

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

  • OPTIONS คือรายการตัวเลือกจากรายการด้านล่าง คั่นด้วยการเว้นวรรค
  • 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

ตัวอย่าง

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

Local to Local (ไฟล์เดียว)

ต่อไปนี้คือวิธีการซิงโครไนซ์ไฟล์เดียวจากไดเรกทอรีหนึ่งไปยังอีกไดเรกทอรีหนึ่งในเครื่องท้องถิ่นโดยใช้ rsync :

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

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

Local to Local (ไดเรกทอรี)

ซิงโครไนซ์ไฟล์ทั้งหมดจากไดเร็กทอรีหนึ่งไปยังไดเร็กทอรีอื่นในเครื่องเดียวกัน:

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 (ไดเร็กทอรี)

ที่ -e ตัวเลือกช่วยให้คุณระบุโปรโตคอลที่ใช้โดย 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 หรือชื่อโฮสต์
  • /path/to/source เป็นเส้นทางบนระบบไฟล์ระยะไกล
  • /path/to/destination เป็นปลายทางบนคอมพิวเตอร์ท้องถิ่น
  • การซิงโครไนซ์จะเกิดขึ้นโดยใช้ 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-' และไม่รวมไฟล์และไดเร็กทอรีอื่นๆ ทั้งหมด (*)

พิจารณาการวิ่งแบบแห้ง

วิ่งแห้ง จะพิมพ์ไฟล์ที่จะมีการเปลี่ยนแปลง แต่จริงๆ แล้วไม่ใช่ do อะไรก็ได้ – จะไม่มีการซิงโครไนซ์หรือแก้ไขไฟล์ ดังนั้นคุณจึงสามารถทดลองกับตัวเลือกต่างๆ และดูเอฟเฟกต์ได้:

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

บทสรุป

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

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