rsync ย่อมาจาก remote sync
rsync ใช้สำหรับดำเนินการสำรองข้อมูลใน UNIX / Linux
rsync utility ใช้ซิงโครไนซ์ไฟล์และไดเรกทอรีจากที่หนึ่งไปอีกที่อย่างมีประสิทธิภาพ สถานที่สำรองข้อมูลอาจอยู่บนเซิร์ฟเวอร์ท้องถิ่นหรือเซิร์ฟเวอร์ระยะไกล
คุณสมบัติสำคัญของ rsync
- ความเร็ว: ครั้งแรก rsync ทำซ้ำเนื้อหาทั้งหมดระหว่าง source และ destination directories ครั้งต่อไป rsync ถ่ายโอนเฉพาะบล็อกหรือไบต์ที่เปลี่ยนแปลงไปยัง destination ทำให้ถ่ายโอนเร็วมาก
- ความปลอดภัย: rsync อนุญาตให้เข้ารหัสข้อมูลด้วยโปรโตคอล ssh ระหว่างการถ่ายโอน
- แบนด์วิดท์น้อย: rsync ใช้การบีบอัดและบีบอัดข้อมูลบล็อกต่อบล็อกที่ปลายส่งและปลายรับตามลำดับ แบนด์วิดท์ที่ rsync ใช้น้อยกว่าโปรโตคอลถ่ายโอนไฟล์อื่นๆ เสมอ
- สิทธิ์: ไม่ต้องมีสิทธิ์พิเศษในการติดตั้งและรัน rsync
Syntax
$ rsync options source destination
Source และ destination อาจเป็น local หรือ remote กรณี remote ให้ระบุ login name, remote server name และ location
ตัวอย่างที่ 1. ซิงโครไนซ์สองไดเรกทอรีในเซิร์ฟเวอร์ท้องถิ่น
เพื่อซิงโครไนซ์สองไดเรกทอรีในคอมพิวเตอร์ท้องถิ่น ใช้คำสั่ง rsync -zvr ตามด้านล่าง
$ rsync -zvr /var/opt/installation/inventory/ /root/temp building file list ... done sva.xml svB.xml . sent 26385 bytes received 1098 bytes 54966.00 bytes/sec total size is 44867 speedup is 1.63 $
ในตัวอย่าง rsync ข้างต้น:
- -z เพื่อเปิดใช้งานการบีบอัด
- -v verbose
- -r หมายถึง recursive
ตอนนี้มาดู timestamp บนไฟล์หนึ่งที่คัดลอกจาก source ไป destination ตามที่เห็นด้านล่าง rsync ไม่ได้ preserve timestamps ระหว่าง sync
$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml -r--r--r-- 1 bin bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml -r--r--r-- 1 root bin 949 Sep 2 2009 /root/temp/sva.xml
ตัวอย่างที่ 2. รักษา timestamps ระหว่าง Sync ด้วย rsync -a
ตัวเลือก rsync -a หมายถึง archive mode ตัวเลือก -a ทำดังนี้
- โหมด Recursive
- Preserves symbolic links
- Preserves permissions
- Preserves timestamp
- Preserves owner and group
ตอนนี้ รันคำสั่งเดียวกับตัวอย่างที่ 1 (แต่กับตัวเลือก rsync -a) ตามด้านล่าง:
$ rsync -azv /var/opt/installation/inventory/ /root/temp/ building file list ... done ./ sva.xml svB.xml . sent 26499 bytes received 1104 bytes 55206.00 bytes/sec total size is 44867 speedup is 1.63 $
ตามที่เห็นด้านล่าง rsync รักษา timestamps ระหว่าง sync
$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /root/temp/sva.xml
ตัวอย่างที่ 3. ซิงโครไนซ์เฉพาะไฟล์เดียว
เพื่อคัดลอกไฟล์เดียว ให้ระบุชื่อไฟล์ให้คำสั่ง rsync ตามด้านล่าง
$ rsync -v /var/lib/rpm/Pubkeys /root/temp/ Pubkeys sent 42 bytes received 12380 bytes 3549.14 bytes/sec total size is 12288 speedup is 0.99
ตัวอย่างที่ 4. ซิงโครไนซ์ไฟล์จากท้องถิ่นไประยะไกล
rsync อนุญาตให้ซิงโครไนซ์ไฟล์/ไดเรกทอรีระหว่างระบบท้องถิ่นและระยะไกล
$ rsync -avz /root/temp/ thegeekstuff@192.168.200.10:/home/thegeekstuff/temp/ Password: building file list ... done ./ rpm/ rpm/Basenames rpm/Conflictname sent 15810261 bytes received 412 bytes 2432411.23 bytes/sec total size is 45305958 speedup is 2.87
ขณะซิงโครไนซ์กับเซิร์ฟเวอร์ระยะไกล ต้องระบุ username และ ip-address ของเซิร์ฟเวอร์ระยะไกล ควรระบุ destination directory บนเซิร์ฟเวอร์ระยะไกลด้วย รูปแบบคือ username@machinename:path
ตามที่เห็นข้างต้น จะถาม password ขณะทำ rsync จากท้องถิ่นไประยะไกล
บางครั้งไม่อยากใส่ password ขณะสำรองไฟล์จากท้องถิ่นไประยะไกล เช่น หากมีสคริปต์สำรองข้อมูลที่คัดลอกไฟล์จากท้องถิ่นไประยะไกลด้วย rsync ต้องมีความสามารถ rsync โดยไม่ต้องใส่ password
เพื่อทำเช่นนั้น ให้ตั้งค่า ssh password less login ตามที่อธิบายก่อนหน้านี้
ตัวอย่างที่ 5. ซิงโครไนซ์ไฟล์จากระยะไกลมาท้องถิ่น
เมื่อต้องการซิงโครไนซ์ไฟล์จากระยะไกลมาท้องถิ่น ให้ระบุ remote path ใน source และ local path ใน target ตามด้านล่าง
$ rsync -avz thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ rpm/Basenames . sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec total size is 45305958 speedup is 2.87
ตัวอย่างที่ 6. Remote shell สำหรับการซิงโครไนซ์
rsync อนุญาตให้ระบุ remote shell ที่ต้องการใช้ สามารถใช้ rsync ssh เพื่อเปิดใช้งานการเชื่อมต่อระยะไกลที่ปลอดภัย
ใช้ rsync -e ssh เพื่อระบุ remote shell ที่จะใช้ ในกรณีนี้ rsync จะใช้ ssh
$ rsync -avz -e ssh thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ rpm/Basenames sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec total size is 45305958 speedup is 2.87
ตัวอย่างที่ 7. อย่าทับไฟล์ที่แก้ไขแล้วที่ปลายทาง
ในสถานการณ์ซิงโครไนซ์ทั่วไป หากไฟล์ถูกแก้ไขที่ปลายทาง อาจไม่อยากทับไฟล์ด้วยไฟล์เก่าจาก source
ใช้ตัวเลือก rsync -u เพื่อทำอย่างนั้น (คือ อย่าทับไฟล์ที่ปลายทาง หากถูกแก้ไข) ในตัวอย่างต่อไปนี้ ไฟล์ชื่อ Basenames ถูกแก้ไขที่ปลายทางแล้ว ดังนั้นจะไม่ถูกทับด้วย rsync -u
$ ls -l /root/temp/Basenames total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames $ rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ sent 122 bytes received 505 bytes 114.00 bytes/sec total size is 45305958 speedup is 72258.31 $ ls -lrt total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames
ตัวอย่างที่ 8. ซิงโครไนซ์โครงสร้างไดเรกทอรีเพียงอย่างเดียว (ไม่รวมไฟล์)
ใช้ตัวเลือก rsync -d เพื่อซิงโครไนซ์โครงสร้างไดเรกทอรีจาก source ไป destination ตัวอย่างด้านล่าง ซิงโครไนซ์โครงสร้างไดเรกทอรีแบบ recursive เท่านั้น ไม่รวมไฟล์ในไดเรกทอรี
$ rsync -v -d thegeekstuff@192.168.200.10:/var/lib/ . Password: receiving file list ... done logrotate.status CAM/ YaST2/ acpi/ sent 240 bytes received 1830 bytes 318.46 bytes/sec total size is 956 speedup is 0.46
ตัวอย่างที่ 9. ดูความคืบหน้า rsync ระหว่างการถ่ายโอน
เมื่อใช้ rsync สำหรับสำรองข้อมูล อาจอยากทราบความคืบหน้าของการสำรอง เช่น จำนวนไฟล์ที่คัดลอก อัตราการคัดลอก เป็นต้น
ตัวเลือก rsync –progress แสดงความคืบหน้ารายละเอียดของการรัน rsync ตามด้านล่าง
$ rsync -avz --progress thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... 19 files to consider ./ Basenames 5357568 100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19) Conflictname 12288 100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19) . . . sent 406 bytes received 15810211 bytes 2108082.27 bytes/sec total size is 45305958 speedup is 2.87
คุณยังสามารถใช้ rsnapshot utility (ซึ่งใช้ rsync) เพื่อสำรองเซิร์ฟเวอร์ Linux ท้องถิ่น หรือสำรองเซิร์ฟเวอร์ Linux ระยะไกล
ตัวอย่างที่ 10. ลบไฟล์ที่สร้างขึ้นที่ปลายทาง
หากไฟล์ไม่อยู่ที่ source แต่อยู่ที่ target อาจต้องการลบไฟล์ที่ target ระหว่าง rsync
ในกรณีนั้น ใช้ตัวเลือก –delete ตามด้านล่าง ตัวเลือก rsync delete ลบไฟล์ที่ไม่อยู่ใน source directory
# Source และ target ในสภาพซิงโครไนซ์ ตอนนี้สร้างไฟล์ใหม่ที่ target $ > new-file.txt $ rsync -avz --delete thegeekstuff@192.168.200.10:/var/lib/rpm/ . Password: receiving file list ... done deleting new-file.txt ./ sent 26 bytes received 390 bytes 48.94 bytes/sec total size is 45305958 speedup is 108908.55
Target มีไฟล์ใหม่ชื่อ new-file.txt เมื่อซิงโครไนซ์กับ source ด้วย –delete option มันลบไฟล์ new-file.txt
ตัวอย่างที่ 11. อย่าสร้างไฟล์ใหม่ที่ปลายทาง
หากต้องการ คุณสามารถอัปเดต (ซิงโครไนซ์) เฉพาะไฟล์ที่มีอยู่แล้วที่ target หาก source มีไฟล์ใหม่ที่ไม่มีที่ target สามารถหลีกเลี่ยงการสร้างไฟล์ใหม่เหล่านั้นที่ target หากต้องการฟีเจอร์นี้ ใช้ตัวเลือก –existing กับคำสั่ง rsync
ก่อนอื่น เพิ่ม new-file.txt ที่ source
[/var/lib/rpm ]$ > new-file.txt
ต่อไป รัน rsync จาก target
$ rsync -avz --existing root@192.168.1.2:/var/lib/rpm/ . root@192.168.1.2's password: receiving file list ... done ./ sent 26 bytes received 419 bytes 46.84 bytes/sec total size is 88551424 speedup is 198991.96
หากดูเอาต์พุตข้างต้น จะไม่ได้รับไฟล์ใหม่ new-file.txt
ตัวอย่างที่ 12. ดูความแตกต่างระหว่าง Source และ Destination
ตัวเลือกนี้มีประโยชน์สำหรับดูความแตกต่างของไฟล์หรือไดเรกทอรีระหว่าง source และ destination
ที่ source:
$ ls -l /var/lib/rpm -rw-r--r-- 1 root root 5357568 2010-06-24 08:57 Basenames -rw-r--r-- 1 root root 12288 2008-05-28 22:03 Conflictname -rw-r--r-- 1 root root 1179648 2010-06-24 08:57 Dirnames
ที่ destination:
$ ls -l /root/temp -rw-r--r-- 1 root root 12288 May 28 2008 Conflictname -rw-r--r-- 1 bin bin 1179648 Jun 24 05:27 Dirnames -rw-r--r-- 1 root root 0 Sep 3 06:39 Basenames
ในตัวอย่างข้างต้น ระหว่าง source และ destination มีความแตกต่างสองประการ แรก owner และ group ของไฟล์ Dirname ต่างกัน ต่อไป size ต่างกันสำหรับไฟล์ Basenames
ตอนนี้มาดูว่า rsync แสดงความแตกต่างนี้อย่างไร ตัวเลือก -i แสดงการเปลี่ยนแปลงของ item
$ rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done >f.st.... Basenames .f....og. Dirnames sent 48 bytes received 2182544 bytes 291012.27 bytes/sec total size is 45305958 speedup is 20.76
ในเอาต์พุตแสดงตัวอักษร 9 ตัวหน้าชื่อไฟล์หรือไดเรกทอรีบ่งชี้การเปลี่ยนแปลง
ในตัวอย่างของเรา ตัวอักษรหน้า Basenames (และ Dirnames) หมายถึง:
> ระบุว่าไฟล์กำลังถ่ายโอนไปยังโฮสต์ท้องถิ่น f แทนว่าเป็นไฟล์ s แทนว่ามีการเปลี่ยนแปลงขนาด t แทนว่ามีการเปลี่ยนแปลง timestamp o owner เปลี่ยน g group เปลี่ยน
ตัวอย่างที่ 13. รวมและตัดออก Pattern ระหว่างการถ่ายโอนไฟล์
rsync อนุญาตให้ระบุ pattern ที่ต้องการ include และ exclude ไฟล์หรือไดเรกทอรีขณะซิงโครไนซ์
$ rsync -avz --include 'P*' --exclude '*' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done ./ Packages Providename Provideversion Pubkeys sent 129 bytes received 10286798 bytes 2285983.78 bytes/sec total size is 32768000 speedup is 3.19
ในตัวอย่างข้างต้น รวมเฉพาะไฟล์หรือไดเรกทอรีที่ขึ้นต้นด้วย 'P' (ใช้ rsync include) และ exclude ไฟล์อื่นๆ ทั้งหมด (ใช้ rsync exclude '*')
ตัวอย่างที่ 14. อย่าถ่ายโอนไฟล์ขนาดใหญ่
คุณสามารถบอก rsync ให้ไม่ถ่ายโอนไฟล์ที่ใหญ่กว่าขนาดเฉพาะ โดยใช้ตัวเลือก rsync –max-size
$ rsync -avz --max-size='100K' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done ./ Conflictname Group Installtid Name Sha1header Sigmd5 Triggername sent 252 bytes received 123081 bytes 18974.31 bytes/sec total size is 45305958 speedup is 367.35
max-size=100K ทำให้ rsync ถ่ายโอนเฉพาะไฟล์ที่น้อยกว่าหรือเท่ากับ 100K คุณสามารถระบุ M สำหรับ megabytes และ G สำหรับ gigabytes
ตัวอย่างที่ 15. ถ่ายโอนไฟล์ทั้งไฟล์
คุณสมบัติหลักของ rsync คือ ถ่ายโอนเฉพาะบล็อกที่เปลี่ยนแปลงไปยัง destination แทนที่จะส่งไฟล์ทั้งหมด
หากแบนด์วิดท์เครือข่ายไม่ใช่ปัญหา (แต่ CPU เป็นปัญหา) คุณสามารถถ่ายโอนไฟล์ทั้งไฟล์ โดยใช้ตัวเลือก rsync -W จะเร่งกระบวนการ rsync เพราะไม่ต้องคำนวณ checksum ที่ source และ destination
# rsync -avzW thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp Password: receiving file list ... done ./ Basenames Conflictname Dirnames Filemd5s Group Installtid Name sent 406 bytes received 15810211 bytes 2874657.64 bytes/sec total size is 45305958 speedup is 2.87
บทเรียน rsync เพิ่มเติม
- How to Setup Rsync with SSH on UNIX / Linux (rsync without password)
- 6 rsync Examples to Exclude Multiple Files and Directories using exclude-from