Cách copy file giữa 2 server Linux sử dụng command


Bài viết này sẻ chia sẻ cho bạn 2 cách copy và đồng bộ file giữa các Server Linux, bằng cách sử dụng 2 command là scprsync

Sử dụng lệnh scp

Lệnh scp được built-in trong phần lớn các hệ điều Linux/Unix

Cú pháp scp

scp [option] <source_path> <your_user>@<destination_server>:<destination_path>

Trong đó:

  • [option] là tham số của lệnh scp, VD: -r để copy 1 thư mục
  • <source_path> là file/thư mục trên Server nguồn
  • <your_user> là 1 user trên Server đích
  • <destination_server> là địa chỉ IP hoặc domain name của Server đích
  • <destination_path> là đường dẫn của file/folder trên Server đích mà ta copy tới.

VD1: Copy file có tên là test.txt trên Server A tới Server B có địa chỉ IP là 10.0.2.2 và lưu nó tại thư mục /tmp, chúng ta dùng command sau:

scp test.txt user@10.0.2.2:/tmp

VD2: Copy thư mục /home/vinasupport/database trên Server A tới Server B

scp -r /home/vinasupport/database user@10.0.2.2:/tmp

Sử dụng lệnh Rsync

Nếu phiên bản HDH Linux/Unix của bạn chưa có command này thì hãy sử dụng lệnh sau để cài đặt:

# For Ubuntu/Debian
sudo apt-get install rsync

# For CentOS/RHEL/Fedora
sudo yum install -y rsync

Cú pháp rsync

# 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>

VD1: Copy file test.txt từ Server A tới Server B, chạy lệnh rsync trên Server A như sau:

rsync -av test.txt user@10.0.2.2:/tmp

Với tham số -v để hiển thị nhiều thông tin khi di chuyển file, -a là bật Archive Mode

VD2: Sau đó copy ngược lại file text từ Server B về Server A, và lưu nó tại đường dẫn /home/vinasupport/database, chạy lệnh rsync trên Server A như sau:

rsync -av user@10.0.2.2:/tmp/test.txt /home/vinasupport/database

Nguồn: vinasupport.com

 

             
SHARE

Bài viết liên quan

mode_edit Bình luận của bạn

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

account_circle
web