New FAMILUG

The PyMiers

Sunday 23 June 2013

rsync - công cụ copy siêu đẳng

bạn sẽ cần tool này, vì chỉ cần học đúng 1 câu lệnh là nó đã có thể tiết kiệm cho bạn hàng đống thời gian!

hvn@archhvn: ~ () $ whatis rsync
rsync (1) - a fast, versatile, remote (and local) file-copying tool 

rsync là một công cụ dùng để copy trên UNIX-like OSes, nó nổi tiếng nhờ thuật toán delta-copy (tức chỉ copy phần khác nhau), bạn có thể cài nó và dùng thay lệnh `cp` 


Rsync có 2 mode, 1 là dùng như lệnh `cp` bình thường, 2 là chạy deamon (hay gọi là service). Rsync daemon mặc định bind đến cổng 873

Cú pháp copy:
rsync -zav source_path dest_path

Các option:
a : archive, bảo toàn các thông tin về file như permission, user, groups ... của các file
v : verbose, in ra quá trình copy
z : nén trước khi copy để giảm kích cỡ truyền tải

hvn@archhvn: ~ () $ rsync -zav /home/hvn/mails /tmp
sending incremental file list
mails/
mails/famihug
mails/hvnc2k8
mails/hvnsweeting
mails/foo/ mails/foo/bar
sent 1400 bytes received 96 bytes 2992.00 bytes/sec total size is 2111 speedup is 1.41 

rsync tự copy các thư mục con luôn, không cần phải thêm option -r như lệnh `cp`

Copy từ remote machine:
rsync -zav root@192.168.122.114:/etc/secret_file.conf /tmp
Và nhập password root của máy remote để được quyền copy

Deamon mode
khi chạy ở daemon mode, rsync có thể hoạt động giống 1 ftp server, tức cho phép download file public. Config rsync daemon được thực hiện trong file rsyncd.conf, gõ `man rsyncd.conf` để xem chi tiết

Ngoài ra nó còn có thể định trước một danh sách user passwd và cho phép người dùng xác thực bằng user passwd đó (không cần có tài khỏan user trên máy chủ)

Các bước tối thiểu để chạy rsync deamon:
  1. Tạo 1 file /etc/rsyncd.conf  # không cần chứa nội dung gì cũng ok
  2. chạy lệnh sudo rsync --daemon
  3. Xong!
Tips:
bạn có thể `alias rs=rsync -zav` để gọi rsync khi gõ rs - ngắn như câu lệnh `cp`

6 comments:

  1. Updated: các bước chạy rsync deamon

    ReplyDelete
  2. sửa lại câu lệnh thành rsync -zav cho các JAV fan dễ nhớ :3

    ReplyDelete
  3. Hình như remote + local cũng cài rsync mới chạy đc.

    ReplyDelete
    Replies
    1. cái hình như này không đúng nhé

      VD:
      hvn@archhvn: /tmp/tmp.5876FLt7gx () $ rsync -zav /home/hvn/emacstut.txt .
      sending incremental file list
      emacstut.txt

      sent 16272 bytes received 31 bytes 32606.00 bytes/sec
      total size is 46285 speedup is 2.84

      Copy đến remote dest (qua SSH - tức remote đang chạy ssh server):
      $ rsync -avz emacstut.txt hvn@192.168.122.114:/home/hvn
      sending incremental file list
      emacstut.txt

      sent 16272 bytes received 31 bytes 32606.00 bytes/sec
      total size is 46285 speedup is 2.84

      $ rsync -avz emacstut.txt root@192.168.122.114:/tmp
      root@192.168.122.114's password:
      sending incremental file list
      emacstut.txt

      sent 16272 bytes received 31 bytes 6521.20 bytes/sec
      total size is 46285 speedup is 2.84

      Delete
    2. This comment has been removed by the author.

      Delete