Transferring Files Between Servers on Linux Using rsync
Transferring files between servers is a common task in Linux system administration. rsync
is a powerful command-line tool that allows you to synchronize files and directories between two locations efficiently. It provides features such as resuming interrupted transfers, preserving file permissions, and minimizing data transfer.
Prerequisites:
- Access to both source and destination servers via SSH.
rsync
installed on both servers. If not installed, you can typically install it using your distribution's package manager (apt
,yum
, etc.).
Step 1: Open Terminal and Connect to Source Server
Launch a terminal window (e.g., using PuTTY) and establish an SSH connection to the source server where the files you want to transfer are located.
ssh username@source_server_ip
Replace username
with your username on the source server and source_server_ip
with the IP address or hostname of the source server.
Step 2: Run rsync Command
Once connected to the source server, use the rsync
command to transfer files to the destination server. Here's the basic syntax:
rsync [options] source_files_or_directories destination_server_ip:destination_directory
Replace source_files_or_directories
with the files or directories you want to transfer, destination_server_ip
with the IP address or hostname of the destination server, and destination_directory
with the directory where you want to save the files on the destination server.
For example, to transfer a file named example.txt
to the /home/user/
directory on the destination server:
rsync example.txt username@destination_server_ip:/home/user/
Step 3: Resuming Interrupted Transfers
If the connection drops during the transfer, you can resume from where it left off by simply re-running the same rsync
command. rsync
will compare the source and destination files and transfer only the differences.
Additional Options:
- -avz: Archive mode (preserves permissions and other attributes), verbose output, and compresses data during transfer.
- --progress: Displays progress information during the transfer.
- --partial: Allows partial transfers, so if the transfer is interrupted,
rsync
can resume from where it left off. - --exclude: Excludes specific files or directories from the transfer.
Example Command with Options:
rsync -avz --progress --partial --exclude='*.log' /path/to/source username@destination_server_ip:/path/to/destination
Step 4: Monitor Transfer Progress
During the transfer, rsync
will display progress information, including the number of files transferred, the transfer speed, and the estimated time remaining.
Step 5: Disconnect from Source Server
Once the transfer is complete, you can exit the SSH session by typing exit
in the terminal and pressing Enter.
exit
Congratulations! You've successfully transferred files from one server to another using rsync
. This method ensures efficient and reliable file synchronization between Linux servers, even in the event of connection interruptions.