rsync là công cụ đồng bộ dữ liệu mạnh nhất trong Linux, dùng để copy file/thư mục với khả năng:
- chỉ copy phần thay đổi (delta transfer)
- giữ nguyên metadata
- hỗ trợ exclude/filter
- resume khi gián đoạn
- sync local và remote
Trong môi trường server (như hệ thống Drupal + backup nhiều lớp của bạn), rsync gần như là tool chuẩn thay cho cp.
1. Cú pháp cơ bản
rsync [options] source destination2. Nguyên lý hoạt động (quan trọng)
Khác cp, rsync:
- so sánh timestamp + size
- chỉ copy file thay đổi
- có thể dùng checksum (
-c) nếu cần chính xác tuyệt đối
👉 Vì vậy:
- nhanh hơn rất nhiều khi sync lặp lại
- giảm IO + bandwidth
3. Option quan trọng nhất
3.1. -a (archive mode) – mặc định dùng
rsync -a source dest
Bao gồm:
-r(recursive)-l(symlink)-p(permissions)-t(timestamp)-g(group)-o(owner)
👉 tương đương cp -a nhưng mạnh hơn
3.2. -v (verbose)
rsync -av source dest
3.3. -z (compress – dùng khi qua mạng)
rsync -avz source user@remote:/path
3.4. --progress
rsync -av --progress source dest
3.5. --delete (cực kỳ quan trọng)
rsync -av --delete source/ dest/
👉 Xóa file ở dest nếu không còn ở source
→ dùng khi cần mirror 1:1
⚠️ Nguy hiểm nếu dùng sai
3.6. --exclude
rsync -av --exclude='files' source/ dest/
3.7. --dry-run (test trước khi chạy thật)
rsync -av --dry-run source/ dest/
👉 best practice
3.8. -u (update)
rsync -avu source dest
→ chỉ copy nếu file mới hơn
3.9. -h (human readable)
rsync -avh source dest
4. Ý nghĩa dấu / (rất quan trọng)
Không có /
rsync -av dir1 dir2
→ copy cả thư mục dir1 vào dir2
Có /
rsync -av dir1/ dir2/
→ copy nội dung bên trong dir1
👉 Đây là lỗi phổ biến nhất khi dùng rsync
5. Các tình huống thực tế
5.1. Copy local (thay cp)
rsync -av source/ dest/
5.2. Clone Drupal site (chuẩn nhất)
rsync -a \
--exclude='files' \
sites/icu/ sites/plan/
--exclude='files' \
sites/icu/ sites/plan/
5.3. Backup
rsync -a /var/www /backup/www
5.4. Backup incremental
rsync -a --delete /data /backup/data
5.5. Sync qua SSH
rsync -avz /data user@192.168.1.10:/backup
5.6. Pull dữ liệu từ remote
rsync -avz user@remote:/data /local/
5.7. Giữ log
rsync -av --log-file=backup.log source dest
6. Filter nâng cao
6.1. Nhiều exclude
rsync -a \
--exclude='files' \
--exclude='private' \
--exclude='*.log' \
source/ dest/
--exclude='files' \
--exclude='private' \
--exclude='*.log' \
source/ dest/
6.2. File exclude list
rsync -a --exclude-from=exclude.txt source dest
6.3. Include + exclude
rsync -a \
--include='*.php' \
--exclude='*' \
source/ dest/
--include='*.php' \
--exclude='*' \
source/ dest/
7. Hiệu năng & tối ưu
7.1. Không cần checksum (mặc định nhanh)
rsync -a
7.2. Dùng checksum (chậm nhưng chính xác)
rsync -ac
7.3. Bỏ nén nếu local
rsync -a # không cần -z
8. So sánh cp vs rsync
| Tiêu chí | cp | rsync |
|---|---|---|
| Copy cơ bản | ✅ | ✅ |
| Incremental | ❌ | ✅ |
| Exclude | ❌ | ✅ |
| Resume | ❌ | ✅ |
| Remote | ❌ | ✅ |
| Hiệu năng lớn | ❌ | ✅ |
9. Best practice (rất quan trọng cho bạn)
9.1. Luôn test trước
rsync -av --dry-run source/ dest/
9.2. Luôn dùng dấu / đúng
source/ → nội dung
source → cả thư mục
source → cả thư mục
9.3. Khi clone Drupal
rsync -a \
--exclude='files' \
--exclude='private' \
sites/icu/ sites/plan/
--exclude='files' \
--exclude='private' \
sites/icu/ sites/plan/
9.4. Khi backup
rsync -a --delete source/ backup/
10. Các lỗi nguy hiểm
10.1. Dùng --delete sai
→ có thể xóa toàn bộ dữ liệu
10.2. Sai dấu /
→ sai cấu trúc thư mục
10.3. Không test trước
→ overwrite dữ liệu
11. Kết luận
rsynclà tool chuẩn cho mọi hệ thống server- Thay thế hoàn toàn
cptrong:- backup
- deploy
- multisite
- Quan trọng nhất:
-a--exclude--delete--dry-run
- Đăng nhập để gửi ý kiến