Rsync Remote sync - mrolarik/simple-iot GitHub Wiki
Rsync
Rsync (Remote sync) เป็นคำสั่งที่ใช้สำหรับการคัดลอก (Copy) ไฟล์ (File) และโฟลเดอร์ (Folder) โดยสามารถทำได้ทั้ง copy จากโฟลเดอร์หนึ่งไปยังอีกโฟลเดอร์หนึ่ง หรือ copy ข้ามเครื่องคอมพิวเตอร์ หรือคอมพิวเตอร์เซิร์ฟเวอร์ก็ได้ ขอให้รู้ ip address, username และ password
คำสั่ง rsync
$ rsync [options] [source] [destination]
-
[source] คือ ที่อยู่ของไฟล์ต้นทางที่จะทำการ copy
-
[destination] คือ ที่อยู่ของโฟลเดอร์ปลายทางที่จะ copy ไฟล์ไปไว้
-
ดังนั้น หาก Linux ไม่ได้ติดตั้ง
rsyncมาด้วยสามารถติดตั้งได้ด้วยคำสั่ง
$ sudo apt-get install rsync
- การเรียกดู options ต่าง ๆ ของ
rsyncสามารถทำได้โดยพิมพ์
$ rsync
ตัวอย่างของผลลัพธ์ที่ได้ แสดงดังต่อไปนี้
rsync version 3.1.1 protocol version 31
Copyright (C) 1996-2014 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
append, ACLs, xattrs, iconv, symtimes, prealloc
rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you
are welcome to redistribute it under certain conditions. See the GNU
General Public Licence for details.
rsync is a file transfer program capable of efficient remote update
via a fast differencing algorithm.
Usage: rsync [OPTION]... SRC [SRC]... DEST
or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
or rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
or rsync [OPTION]... [USER@]HOST:SRC [DEST]
or rsync [OPTION]... [USER@]HOST::SRC [DEST]
or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect
to an rsync daemon, and require SRC or DEST to start with a module name.
Options
-v, --verbose increase verbosity
--info=FLAGS fine-grained informational verbosity
--debug=FLAGS fine-grained debug verbosity
--msgs2stderr special output handling for debugging
-q, --quiet suppress non-error messages
--no-motd suppress daemon-mode MOTD (see manpage caveat)
-c, --checksum skip based on checksum, not mod-time & size
-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
--no-OPTION turn off an implied OPTION (e.g. --no-D)
-r, --recursive recurse into directories
-R, --relative use relative path names
--no-implied-dirs don't send implied dirs with --relative
-b, --backup make backups (see --suffix & --backup-dir)
--backup-dir=DIR make backups into hierarchy based in DIR
--suffix=SUFFIX set backup suffix (default ~ w/o --backup-dir)
-u, --update skip files that are newer on the receiver
--inplace update destination files in-place (SEE MAN PAGE)
--append append data onto shorter files
--append-verify like --append, but with old data in file checksum
ตัวอย่างการทำงานของ rsync
ตัวอย่างที่หนึ่ง - การ sync/copy ไฟล์เดียว
- ติดตั้งโปรแกรม
treeโดยใช้คำสั่งต่อไปนี้
$ sudo apt-get install tree
- พิมพ์คำสั่ง
treeเพื่อดูโครงสร้างของไฟล์และโฟลเดอร์
$ tree
.
├── ws
│ ├── RPi-pir-01.jpg
│ └── RPi-pir-02.jpg
├── ws1
└── ws2
- ในกรณีที่ต้องการคัดลอกเพียงไฟล์เดียว เช่น ต้องการคัดลอกไฟล์
ws/RPI-pir-01.jpgไปไว้ที่โฟลเดอร์ws2สามารถทำได้ดังนี้
$ rsync ws/RPi-pir-01.jpg ws2
- เมื่อใช้คำสั่ง
rsyncเสร็จเรียบร้อย จากนั้นให้พิมพ์treeเพื่อดูข้อมูล
$ tree
.
├── ws
│ ├── RPi-pir-01.jpg
│ └── RPi-pir-02.jpg
├── ws1
└── ws2
└── RPi-pir-01.jpg
- ข้อมูลไฟล์
RPi-pir-01.jpgจะถูก copy ไปไว้ที่ตำแหน่งโฟลเดอร์ws2
ตัวอย่างที่สอง - การ sync/copy ไฟล์ทั้งหมดในโฟลเดอร์
- เริ่มต้นด้วยการพิมพ์คำสั่ง
pwdเพื่อดูตำแหน่งปัจจุบัน
$ pwd
/home/mrolarik/Desktop/cocobot
- จะเห็นได้ว่าตอนนี้อยู่ที่ตำแหน่ง
/home/mrolarik/Desktop/cocobot - จากนั้นใช้คำสั่ง
treeเพื่อดูข้อมูลที่จะทดสอบ
$ tree
.
├── ws
│ ├── RPi-pir-01.jpg
│ └── RPi-pir-02.jpg
└── ws1
2 directories, 2 files
-
จากตัวอย่างข้างต้นเมื่อใช้คำสั่ง
treeจะพบว่ามีโฟลเดอร์จำนวน 2 โฟลเดอร์คือ ws และ ws1 -
ในโฟลเดอร์ ws จะมีไฟล์อยู่ 2 ไฟล์ คือ RPi-pir-01.jpg และ RPi-pir-02.jpg
-
ในโฟลเดอร์ ws1 ไม่มีไฟล์ปรากฎอยู่
-
หากต้องการ sync/copy ทั้งสองไฟล์ใน ws ไปไว้ที่ ws1 โดยใช้
rsyncสามารถทำได้โดย
$ rsync ws/* ws1
- จากนั้นพิมพ์คำสั่ง
treeเพื่อดูผลลพัธ์
$ tree
.
├── ws
│ ├── RPi-pir-01.jpg
│ └── RPi-pir-02.jpg
└── ws1
├── RPi-pir-01.jpg
└── RPi-pir-02.jpg
2 directories, 4 files
- จากคำสั่ง การพิมพ์
ws/*หมายถึงเลือกไฟล์ทั้งหมด (*) ที่อยู่ในโฟลเดอร์ ws - ดังนั้นคำสั่ง
rsync ws/* ws1จึงหมายถึง copy ทุกไฟล์ที่อยู่ในโฟลเดอร์ ws ไปไว้ที่ ws1
ตัวอย่างที่สาม - การ sync/copy ไฟล์และตั้งชื่อใหม่
- พิมพ์คำสั่ง
treeเพื่อดูโครงสร้างของไฟล์และโฟลเดอร์
$ tree
.
├── ws
│ ├── RPi-pir-01.jpg
│ └── RPi-pir-02.jpg
├── ws1
└── ws2
└── RPi-pir-01.jpg
3 directories, 3 files
- ในกรณีนี้ต้องการที่จะ sync/copy ไฟล์ชื่อ
ws/RPi-pir-02.jpgไปไว้ในโฟลเดอร์ws1และเปลี่ยนชื่อไฟล์ใหม่เป็นRPI.jpg - สามารถทำได้โดยพิมพ์คำสั่ง
$ rsync ws/RPi-pir-02.jpg ws1/RPI.jpg
- พิมพ์คำสั่ง
treeเพื่อดูผลลัพธ์
$ tree
.
├── ws
│ ├── RPi-pir-01.jpg
│ └── RPi-pir-02.jpg
├── ws1
│ └── RPI.jpg
└── ws2
└── RPi-pir-01.jpg
3 directories, 4 files
ตัวอย่างที่สี่ - การ sync/copy ไฟล์และสร้างโฟลเดอร์
- ในกรณีนี้คือต้องการที่จะ sync/copy ไฟล์ และนำไปวางไว้ในตำแหน่งต่าง ๆ โดยทำการสร้างโฟลเดอร์ขึ้นมาใหม่ และนำไฟล์ที่ copy นั้นไปไว้ในโฟลเดอร์ที่สร้างขึ้นมาใหม่
- เริ่มต้นด้วยการพิมพ์คำสั่ง
treeเพื่อดูข้อมูล
$ tree
.
├── ws
│ ├── RPi-pir-01.jpg
│ └── RPi-pir-02.jpg
├── ws1
│ └── RPI.jpg
└── ws2
└── RPi-pir-01.jpg
3 directories, 4 files
- จากนั้นให้พิมพ์คำสั่งดังต่อไปนี้
$ rsync ws/* ws3/
- จากนั้นพิมพ์คำสั่ง
treeเพื่อดูผลลัพธ์
$ tree
.
├── ws
│ ├── RPi-pir-01.jpg
│ └── RPi-pir-02.jpg
├── ws1
│ └── RPI.jpg
├── ws2
│ └── RPi-pir-01.jpg
└── ws3
├── RPi-pir-01.jpg
└── RPi-pir-02.jpg
4 directories, 6 files
- จากคำสั่ง
rsync ws/* ws3/หมายถึงการ copy/sync ข้อมูลทั้งหมดที่อยู่ในโฟลเดอร์ ws (ws/*) ไปไว้ตำแหน่งใหม่คือws3/โดยห้ามลืมเครื่องหมาย backslash () เครื่องหมาย backslash หมายถึงโปรแกรมจะทำการสร้างโฟลเดอร์ชื่อws3 - ดังนั้น ผลลัพธ์ที่ได้คือจะเกิดโฟลเดอร์ใหม่ขึ้นชื่อ
ws3โดยบรรจุไฟล์ที่ copy มาจากโฟลเดอร์wsจำนวน 2 ไฟล์
ตัวอย่างที่ห้า - การ sync/copy ไฟล์โดยใช้คำสั่ง rsync ไปยังเครื่อง Server (Remote Server)
การใช้คำสั่ง rsync เพื่อ copy/sync ข้อมูลไปยัง Remote Server ต้องแน่ใจก่อนว่าเครื่อง Server นั้นติดตั้งโปรแกรม ssh หากยังไม่ติดตั้งต้องติดตั้งให้เรียบร้อย
$ sudo apt-get install openssh-server
-
อ่านรายละเอียดเพิ่มเติม การควบคุม Raspberry Pi ผ่าน ssh
-
ภาพต่อไปนี้แสดง
treeของข้อมูลที่อยู่ในเครื่องคอมพิวเตอร์ (Local Computer)
-
ภาพดังต่อไปนี้แสดง
treeของข้อมูลที่อยู่ในเครื่องเซิร์ฟเวอร์ (Remote Server)
-
ในกรณีนี้ต้องการที่จะ copy ข้อมูลทั้งหมดที่อยู่ใน
ws/*ในเครื่อง Local Computer ไปไว้ยังเครื่อง Remote Server โดยจะ copy ไปเก็บไว้ยังตำแหน่ง/home/ros/Desktopโดยทำการสร้างโฟลเดอร์ใหม่ชื่อw3และข้อมูลทั้งหมดจะถูกเก็บไว้ในโฟลเดอร์w3 -
คำสั่งที่ใช้ในการ
copy/syncแสดงดังต่อไปนี้
$ rsync ws/* [email protected]:/home/ros/Desktop/w3/
[email protected]'s password:
โดย
rosคือชื่อของ user ที่อยู่ในเครื่อง Remote Server192.168.1.10คือ ip address ของเครื่อง Remote Server/home/ros/Desktop/w3/คือตำแหน่งใน Remote Server ที่จะทำการ copy/sync ข้อมูล- จากนั้นระบบจะให้กรอก password ของเครื่อง Remote Server
ผลลัพธ์ที่ได้แสดงดังตัวอย่างต่อไปนี้

ตัวอย่างที่หก - การ sync/copy ไฟล์จากเครื่อง Server (Remote Server) กลับมายังเครื่องคอมพิวเตอร์ (Local Computer)
การใช้คำสั่ง rsync นอกจากจะ sync/copy ข้อมูลจาก Local Computer ไปยัง Remote Server ทั้งนี้ ยังสามารถ sync/copy ข้อมูลจาก Remote Server กลับมายัง Local Computer ได้อีกด้วย สามารถทำได้ดังนี้
- ในฝั่งของ Remote Server สามารถแสดงข้อมูลแบบ
treeได้ดังนี้
- และทางฝั่งของ Local Computer สามารถแสดงข้อมูล
treeได้ดังนี้
- เมื่อทราบข้อมูลของทั้ง 2 ฝั่งเป็นที่เรียบร้อย ดังนั้น ในกรณีนี้จะทำการ sync/copy ข้อมูลจากฝั่ง Remote Server คือไฟล์ index.php ที่อยู่ใน
/home/ros/Desktop/ws/กลับมายังฝั่ง Local Computer สามารถทำได้ดังนี้
$ rsync [email protected]:/home/ros/Desktop/ws/index.php /home/mrolarik/Desktop/cocobot/ws4/
[email protected]'s password:
โดย
[email protected]คือชื่อของผู้ใช้rosของทางฝั่ง Remote Server ที่มี ip address คือ192.168.1.10/home/ros/Desktop/ws/index.phpคือตำแหน่งของข้อมูลจากทางฝั่งของ Remote Server ที่ต้องการ sync/copy/home/mrolarik/Desktop/cocobot/ws4/คือตำแหน่งของ Local Computer ที่ต้องการจัดเก็บข้อมูล- จากนั้นระบบจะให้กรอก password ของเครื่อง Remote Server
ผลลัพธ์ที่ได้ แสดงดังต่อไปนี้
