When you need to efficiently and securely transfer files between two servers or computers, one of the best options is to use the rsync
tool in combination with SSH (Secure Shell). This method is widely popular among server administrators and developers because of its speed, flexibility, and ability to synchronize data in a secure manner. In this article, we will delve into how file copying using rsync
over SSH works, how to set up each step, and what options this tool offers.
What is rsync and why use it?
rsync
(Remote Sync) is a tool designed to transfer and synchronize files between two locations while optimizing data transfer. Its key advantages include:
- Incremental copying: It only transfers changed files or parts of files, saving both time and bandwidth.
- Flexibility: It can be used for both local and remote copying.
- Security: When combined with SSH, the transfer is encrypted, ensuring data is not intercepted or altered during the process.
- Compression: It offers the option to compress data during transfer, reducing the volume of data sent.
- Automation: Easy integration into scripts and automated tasks.
How does SSH work with rsync?
SSH provides a secure, encrypted channel for data transfer. When you use rsync
with SSH, all transferred files are encrypted, protecting sensitive data from unauthorized access.
The basic syntax for copying files using rsync
over SSH is as follows:
rsync -avz -e ssh source_directory/ user@server:/destination_directory/
Here’s a breakdown of the parameters:
- -a: Archive mode, preserving symbolic links, file permissions, timestamps, and other file attributes.
- -v: Verbose mode, which displays the progress of the copying process in the terminal.
- -z: Compresses data during transfer.
- -e ssh: Specifies that the transfer will occur via SSH.
Practical example of copying files using rsync over SSH
Suppose you have a folder named project
that you want to copy from your local machine to a remote server. On the server, you have a user account named user
, and the destination folder is located at /home/user/projects/
. The command for this transfer would be:
rsync -avz -e ssh ~/project/ user@server:/home/user/projects/
This command:
- Uses
rsync
to recursively copy all files and subdirectories from theproject
folder. - Compresses the data using the
-z
flag. - Transfers the data over SSH, ensuring encryption.
Additional options and rsync parameters
One of the most powerful aspects of rsync
is its wide range of options that you can tailor to your needs. Here are some useful options:
-
--delete: Ensures that files in the destination directory that no longer exist in the source directory are deleted. This is useful for creating an exact mirror of content between servers.
rsync -avz --delete -e ssh ~/project/ user@server:/home/user/projects/
- --progress: If you want to monitor the copying progress, this option will display information about file sizes and transfer speed.
rsync -avz --progress -e ssh ~/project/ user@server:/home/user/projects/
- --exclude: This option allows you to exclude certain files or directories from the copying process. For instance, if you don't want to transfer hidden files (starting with a dot), you can exclude them as follows:
rsync -avz --exclude '.*' -e ssh ~/project/ user@server:/home/user/projects/
Automation and task scheduling with rsync
One of the advantages of rsync
is its ability to easily integrate into scheduled tasks using tools like cron
in Unix-based systems. This way, you can set up automatic backups or file synchronization between servers. Here’s an example of a simple cron job that will perform a backup every night at 2:00 AM:
0 2 * * * rsync -avz -e ssh ~/project/ user@server:/home/user/projects/
Using rsync
in combination with SSH is a powerful tool for secure, efficient, and flexible file copying and synchronization between two computers or servers. With its wide range of parameters, it can be customized to meet specific needs, and its integration into scripts and automated tasks makes it an indispensable tool for data management and backups.