Rsync là gì?Rsync ( Remote Sync) là một lệnh giúp khả năng truyền file gia tăng nhanh từ máy chủ này sang máy chủ khác. Trong bài viết này, mình sẽ hướng dẫn bạn sử dụng lệnh Rsync cho các trường hợp sử dụng khác nhau. Rsync chỉ sao chép sự khác biệt của các tệp đã thực sự thay đổi, được nén thông qua ssh. Nó rất phổ biến và được sử dụng rộng rãi để di chuyển các tệp hoặc thư mục trên Linux/Unix với nhau. Các tính năng quan trọng của lệnh rsync
Cài đặt RsyncCác Option
5. Rsync file từ server local đến server remote qua SSH Với Centosyum install -y rsync Với Ubuntuapt install -y rsync Cú pháp thực hiện# copy file/folder từ server hiện tại tới 1 remote server rsync [-options] <source_path> <your_user>@<destination_server>:<destination_path> # Copy file/folder từ 1 remote Server tới server hiện tại rsync [-options] <your_user>@<source_server>:<source_path> <destination_path> VD:rsync -azvh /home/admin/admin_backups/admin.root.admin.tar.gz :/home/file_backup // Mặc định port 22 rsync -avz -e "ssh -p $portNumber" user@remoteip:/path/to/files/ /local/path/ // Thêm vào vào 1. Sync a File on a Local Computer2. Rsync thư mục từ Local Server đến Server Remote [root@localhost]# rsync -zvh test.txt /home/test1/ test.txt sent 87.76M bytes received 31 bytes 35.11M bytes/sec total size is 87.74M speedup is 1.00 3. Rsync thư mục từ xa với máy server local/home/test1/ 4. Rsync thông qua SSH 2. Rsync thư mục từ Local Server đến Server Remote3. Rsync thư mục từ xa với máy server local [root@localhost]$ rsync -avzh public_html/ :/home/ 's password: sending incremental file list public_html/ public_html/cgi-bin public_html/index.php sent 72 bytes received 30 bytes 140.80 bytes/sec total size is 1M speedup is 1.00 3. Rsync thư mục từ xa với máy server local4. Rsync thông qua SSH/home/public_html trên máy chủ từ xa đang được sao chép trong server local của bạn trong /home/test. [root@localhost]# rsync -avzh :/home/public_html /home/test/ 's password: receiving incremental file list created directory /home/test public_html/ public_html/cgi_bin public_html/cgi_bin/index.php sent 91 bytes received 4.99M bytes 322.16K bytes/sec total size is 4.99M speedup is 1.00 4. Rsync thông qua SSH5. Rsync file từ server local đến server remote qua SSH rsync -avzh kblinux.zip -e 'ssh -p 2020' /home/ 5. Rsync file từ server local đến server remote qua SSH[root@localhost]# rsync -avzhe ssh public_html/ :/home 's password: sending incremental file list public_html/ sent 16.56M bytes received 45 bytes 1.28M bytes/sec total size is 2M speedup is 1.10 6. Rsync file và thư mục từ server remote đến server local với các option7. Rsync file và thư mục từ server local đến server remote với option –exclude apt install -y rsync0 7. Rsync file và thư mục từ server local đến server remote với option –exclude8. Rsync file và thư mục từ server local đến server remote với option –delete apt install -y rsync1 8. Rsync file và thư mục từ server local đến server remote với option –delete9. Rsync file và thư mục từ server local đến server remote hiển thị progress apt install -y rsync2 9. Rsync file và thư mục từ server local đến server remote hiển thị progress10. Giớ hạn kích thước Rsync apt install -y rsync3 10. Giớ hạn kích thước Rsync11. Tự động xoá nguồn sau khi Rsync hoàn tất Trong ví dụ này, nó sẽ đồng bộ các tệp 100k cũng ít hơn 100k tệp. apt install -y rsync4 11. Tự động xoá nguồn sau khi Rsync hoàn tấtLệnh này sẽ tự động xoá nguồn sau khi rsync hoàn tất với option ‘–remove-source-files‘ apt install -y rsync5 12. Đặt giới hạn Bandwidth Limit và Transfer FileBạn có thể đặt giới hạn băng thông trong khi truyền dữ liệu từ máy chủ này sang máy chủ khác với sự trợ giúp của tùy chọn ‘ – bwlimit’. Tùy chọn này giúp chúng tôi giới hạn băng thông I / O.– bwlimit’. Tùy chọn này giúp chúng tôi giới hạn băng thông I / O. apt install -y rsync6 13. Rsync với DryNếu bạn chưa quen với rsync và không biết chính xác đầu ra là gì nếu bạn chạy bất kỳ lệnh rsync nào, thì lệnh này chắc chắn sẽ giúp bạn kiểm tra đầu ra trước. Các lệnh rsync thực sự có thể làm rối tung mọi thứ trong thư mục đích của bạn. Tùy chọn này sẽ không thực hiện bất kỳ thay đổi nào chỉ thực hiện một lệnh dry và hiển thị đầu ra của lệnh. Lệnh rsync này giúp bạn kiểm tra đầu ra trước khi bạn thực thi trong môi trường thực. apt install -y rsync7 Khi bạn chạy các lệnh rsync, nó có thể yêu cầu mật khẩu trong khi thực hiện rsync từ máy chủ cục bộ đến máy chủ từ xa. Nếu bạn muốn chạy rsync tự động mà không cần nhắc mật khẩu, bạn phải tạo và sử dụng xác thực dựa trên khóa SSH. |