Hướng dẫn cài đặt rsync trên centos 7 năm 2024

Rsync là một tiện ích để chuyển và đồng bộ hóa các tập tin qua các hệ thống máy tính một cách hiệu quả, bằng cách kiểm tra dấu thời gian và kích thước tệp. Rsync thường được sử dụng để đồng bộ các tập tin và thư mục giữa hai hệ thống khác nhau. Một trong những tính năng nổi bật của rsync là nó chỉ đồng bộ những thay đổi trong file hay folder cần đồng bộ và giữ nguyên được Permissions, Owner, Group, Symlinks, TimeStamp của đối tượng sau khi đồng bộ.

Cài đặt Rsync

– Trên Red Hat/CentOS

yum install rsync

– Trên Debian/Ubuntu

apt-get install rsync

Sử dụng Rsync

Cú pháp câu lệnh:

Máy nguồn

rsync -aHAXxv --numeric-ids --delete --progress -e "ssh -T -c arcfour -o Compression=no -x" user@<source>:<source_dir> <dest_dir>

Máy đích

rsync -aHAXxv --numeric-ids --delete --progress -e "ssh -T -c arcfour -o Compression=no -x" [source_dir] [dest_host:/dest_dir]

Tham số lệnh:

rsync

  • a: copy dữ liệu recursively, đồng thời giữ nguyên được tất cả các thông số của thư mục và file như permissions, owner, group, modification times, symlinks, device files.
  • H: giữ nguyên hard-links
  • A: giữ nguyên ACLs
  • X: giữ nguyên các thuộc tính mở rộng
  • v: tăng hiệu quả
  • –numeric-ids: không ánh xạ giá trị uid/gid theo tên user/group
  • –delete: xóa các tập tin không liên quan ở thư mục đích
  • –progress: hiển thị trạng thái đồng bộ

ssh

  • T: tắt pseudo-tty để giảm tải CPU trên máy đích
  • c arcfour: sử dụng mã hóa SSH yếu nhất nhưng nhanh nhất
  • o Compression=no: tắt chế độ nén trong SSH
  • x: tắt X forwarding nếu nó được bật theo mặc định

Thí dụ chúng ta sẽ đồng bộ dữ liệu trong thư mục /home từ máy chủ gốc (103.255.236.158) tới máy chủ đích (103.255.236.172) vào thư mục /root

Đăng nhập vào 103.255.236.158 và rsync dữ liệu đến 103.255.236.172

rsync -aHAXxv --numeric-ids --delete --progress -e "ssh -T -c arcfour -o Compression=no -x" /home/ [email protected]:/root

Lưu ý: dấu “/” trong câu lệnh trên nghĩa là chỉ đồng bộ nội dung trong thư mục home. Nếu không có dấu “/”, hệ thống sẽ hiểu là lấy cả folder home, sau khi đồng bộ tại root sẽ là root/home/[nội dung home] chứ không phải là root/[nội dung home]

Rsync (Remote Sync) là một công cụ dùng để sao chép và đồng bộ file/thư mục được dùng rất phổ biến. Với sự trợ giúp của rsync, bạn có thể đồng bộ dữ liệu trên local hoặc giữa các server với nhau một cách dễ dàng.

Hướng dẫn cài đặt rsync trên centos 7 năm 2024

I. Tính năng nổi bật của Rsync

  • Rsync hỗ trợ copy giữ nguyên thông số của files/folder như Symbolic links, Permissions, TimeStamp, Owner và Group.
  • Rsync nhanh hơn scp vì Rsync sử dụng giao thức remote-update, chỉ transfer những dữ liệu thay đổi mà thôi.
  • Rsync tiết kiệm băng thông do sử dụng phương pháp nén và giải nén khi transfer.
  • Rsync không yêu cầu quyền super-user.

II. Cài đặt Rsync

Rsync được cài đặt dễ dàng với một dòng lệnh:

– Trên Red Hat/CentOS

yum install rsync

– Trên Debian/Ubuntu

apt-get install rsysnc

III. Sử dụng Rsync

Câu lệnh căn bản của rsync:

rsync options source destination

Trong đó:

  • Source: dữ liệu nguồn
  • Destination: dữ liệu đích
  • Options: một số tùy chọn thêm

Các tham số cần biết khi dùng Rsync

  • apt-get install rsysnc 7: hiển thị trạng thái kết quả
  • apt-get install rsysnc 8: copy dữ liệu recursively, nhưng không đảm bảo thông số của file và thư mục
  • apt-get install rsysnc 9: cho phép copy dữ liệu recursively, đồng thời giữ nguyên được tất cả các thông số của thư mục và file
  • rsync options source destination 0: nén dữ liệu khi transfer, tiết kiệm băng thông tuy nhiên tốn thêm một chút thời gian
  • rsync options source destination 1: human-readable, output kết quả dễ đọc
  • rsync options source destination 2: xóa dữ liệu ở destination nếu source không tồn tại dữ liệu đó.
  • rsync options source destination 3: loại trừ ra những dữ liệu không muốn truyền đi, nếu bạn cần loại ra nhiều file hoặc folder ở nhiều đường dẫn khác nhau thì mỗi cái bạn phải thêm rsync options source destination 3 tương ứng.

Rsync không tự động chạy nên thường được dùng kết hợp với crontab. Tiếp theo mình sẽ giới thiệu một số ví dụ thường dùng với Rsync và kết thúc bài là script mình dùng để tự động backup toàn bộ VPS hàng ngày.

Khi lần đầu chạy rsync, toàn bộ dữ liệu nguồn sẽ được copy đến server đích, từ lần chạy sau trở đi chỉ những dữ liệu chưa được copy mới được transfer – đây là quá trình đồng bộ dữ liệu. Do đó, bạn có thể hiểu rsync thực hiện việc copy hoặc đồng bộ đều đúng. Trong bài viết mình sẽ sử dụng duy nhất khái niệm Copy cho ngắn gọn nhé.

1. Copy file và thư mục trên local

Copy file trên local

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10

Ví dụ trên copy file backup.tar sang thư mục /tmp/backups/ trên cùng một máy. Như bạn thấy thư mục đích chưa có nên rsync tự động tạo trước khi copy.

Copy thư mục trên local

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/ sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 4.99M bytes received 92 bytes 3.33M bytes/sec total size is 4.99M speedup is 1.00

Câu lệnh trên copy toàn bộ file từ thư mục

rsync options source destination

5 đến thư mục

rsync options source destination

6 trên cùng một máy.

2. Copy file và thư mục giữa các server

Copy thư mục từ Local lên Remote Server

[root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/ [email protected]'s password: sending incremental file list ./ httpd-2.2.3-82.el5.centos.i386.rpm mod_ssl-2.2.3-82.el5.centos.i386.rpm nagios-3.5.0.tar.gz nagios-plugins-1.4.16.tar.gz sent 4993369 bytes received 91 bytes 399476.80 bytes/sec total size is 4991313 speedup is 1.00

Lệnh trên copy thư mục

rsync options source destination

7 từ Local lên Remote Server có IP

rsync options source destination

8, lưu ở thư mục

rsync options source destination

9

Copy thư mục từ Remote Server về Local

[root@hocvps]# rsync -avzh [email protected]:/home/tarunika/rpmpkgs /tmp/myrpms [email protected]'s password: receiving incremental file list created directory /tmp/myrpms rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 91 bytes received 4.99M bytes 322.16K bytes/sec total size is 4.99M speedup is 1.00

Lệnh trên sẽ copy dữ liệu ở thư mục

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10

0 trên Remote Server

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10

1 về máy Local lưu ở thư mục

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10

2

3. Rsync qua SSH

Với Rsync, bạn có thể transfer qua giao thức SSH, qua đó dữ liệu được bảo mật an toàn hơn.

Copy file từ Remote Server về Local Server qua SSH

Để xác định giao thức sẽ sử dụng với rsync, bạn cần thêm tùy chọn

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10

3 cùng với tên giao thức, ở đây là

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10

4.

[root@hocvps]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/ [email protected]'s password: receiving incremental file list install.log sent 30 bytes received 8.12K bytes 1.48K bytes/sec total size is 30.74K speedup is 3.77

Lệnh trên copy file

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10

5 trên Remote Server

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10

1 về thư mục

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10

7 trên máy Local.

Copy file từ Local lên Remote Server qua SSH

[root@hocvps]# rsync -avzhe ssh backup.tar [email protected]:/backups/ [email protected]'s password: sending incremental file list backup.tar sent 14.71M bytes received 31 bytes 1.28M bytes/sec total size is 16.18M speedup is 1.10

Nếu sử dụng port SSH custom, không phải port tiêu chuẩn 22, bạn cần chỉ rõ port muốn dùng trong câu lệnh. Ví dụ với port 2222 của HocVPS Script câu lệnh sẽ như sau:

[root@hocvps]# rsync -avzhe "ssh -p 2222" [email protected]:/root/install.log /tmp/

4. Hiển thị tiến trình trong khi transfer dữ liệu với rsync

Để hiển thị tiến độ transfer dữ liệu, bạn có thể sử dụng tùy chọn

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10

8

apt-get install rsysnc

0

5. Sử dụng tùy chọn –include và –exclude

Hai tùy chọn này cho phép chúng ta thêm hoặc bớt file hoặc thư mục trong quá trình đồng bộ dữ liệu.

apt-get install rsysnc

1

Ở ví dụ trên, Rsync include toàn bộ những file hoặc thư mục có tên bắt đầu bởi ký tự ‘R’ và exclude toàn bộ những file hoặc thư mục còn lại.

6. Sử dụng tùy chọn –delete

Nếu muốn xóa một file hoặc thư mục không có ở thư mục nguồn, mà lại xuất hiện ở thư mục đích trong quá trình transfer, bạn hãy sử dụng tùy chọn

rsync options source destination

2.

apt-get install rsysnc

2

Server đích đã có file

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/ sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 4.99M bytes received 92 bytes 3.33M bytes/sec total size is 4.99M speedup is 1.00

0, trong quá trình đồng bộ với option

rsync options source destination

2, file sẽ bị xóa.

7. Giới hạn dung lượng tối đa của file được đồng bộ

Để giới hạn những file lớn được đồng bộ, bạn có thể sử dụng option

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/ sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 4.99M bytes received 92 bytes 3.33M bytes/sec total size is 4.99M speedup is 1.00

2

apt-get install rsysnc

3

8. Tự động xóa dữ liệu nguồn sau khi đồng bộ thành công

Để rsync tự động xóa dữ liệu sau khi đồng bộ lên server đích thành công, bạn có thể sử dụng lựa chọn

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/ sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 4.99M bytes received 92 bytes 3.33M bytes/sec total size is 4.99M speedup is 1.00

3

apt-get install rsysnc

4

9. Chạy thử nghiệm Rsync

Nếu bạn không chắc câu lệnh có thực hiện chính xác những gì mong muốn hay không, hãy thêm tùy chọn

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/ sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 4.99M bytes received 92 bytes 3.33M bytes/sec total size is 4.99M speedup is 1.00

4.

Rsync lúc này sẽ không thay đổi gì dữ liệu cả mà chỉ show output mà thôi. Nếu mọi thứ hoạt động ổn, hãy bỏ tùy chọn

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/ sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 4.99M bytes received 92 bytes 3.33M bytes/sec total size is 4.99M speedup is 1.00

4 ra khỏi câu lệnh.

apt-get install rsysnc

5

10. Giới hạn bandwidth

apt-get install rsysnc

6

IV. Tổng kết

Ứng dụng của Rsync có rất nhiều, bạn có thể đồng bộ hóa file giữa các thư mục, giữa các server qua đó backup server sang một server khác hoặc synchronize real time. Tùy nhu cầu mà bạn hãy ứng dụng Rsync cho hiệu quả.