Oct 16, 2023 4 min read

How to Transfer Files with Rsync over SSH

Transfer Files with Rsync over SSH with our step-by-step tutorial. The rsync is used for backups, and transferring files between systems.

Transfer Files with Rsync over SSH
Table of Contents

Introduction

Before we begin talking about how to transfer files with Rsync over SSH, let's briefly understand – What is rsync ?

The rsync can be used for a variety of tasks, including data mirroring, incremental backups, and file transferring between systems. There are other SSH file transfer utilities, such as scp and sftp are similar, but none of them have all the features that rsync does.

Linux and Unix users have a number of alternatives for transferring data between networked systems.

The most extensively used data transfer protocols are SSH and FTP. While FTP is widely used, it is always preferable to use SSH because it is the most secure method of file sharing.

In this tutorial, you will learn to transfer files with rsync over SSH. We will also address a few FAQs on how to transfer files with rsync over SSH.

Advantages of rsync

  1. Efficient file transfer: Rsync only transfers the changes in files, minimizing bandwidth usage and speeding up synchronization.
  2. Network-friendly: It uses compression and delta transfer algorithms, reducing network load during data replication.
  3. Versatile: Supports local and remote synchronization, making it suitable for both local backups and syncing files across different systems.
  4. Incremental backups: Rsync can create incremental backups, allowing you to easily restore previous versions of files.
  5. Secure transfer: It can use SSH as a secure transport, ensuring the confidentiality and integrity of data during transmission.

Requirements

  • The rsync utility must be installed on both the destination and source computers. Use your distribution's package manager to install it if it isn't already there:

Ubuntu and Debian:

sudo apt install rsync

CentOS and Fedora:

sudo yum install rsync
  • Access to the remote machine through SSH.
  • The rsync command's user and the remote SSH user must both have permission to read and write files.

Using rsync to Transfer Files over SSH

You can use rsync to move files and directories from and to remote servers using SSH.

The following is the standard syntax for using rsync to transfer files:

Local to Remote: rsync [OPTION]... -e ssh [SRC]... [USER@]HOST:DEST
Remote to Local: rsync [OPTION]... -e ssh [USER@]HOST:SRC... [DEST]

SRC is the source directory, and DEST is the destination directory. HOST is the remote SSH host or IP address, and USER is the remote SSH username.

You can skip the -e ssh argument because recent versions of rsync are configured to utilize SSH as the default remote shell.

For example, to copy a single file /opt/file.zip from a local machine to the /var/www/ directory on a remote machine with IP 12.12.12.12, type:

rsync -a /opt/file.zip [email protected]:/var/www/

The -a option recursively syncs folders transfer special and block devices and preserve symbolic links, modification times, groups, ownership, and rights.

If you haven't set up a passwordless SSH login to the remote machine, you'll be prompted for the user password.

If the file already exists on the remote server, it will be overwritten. If you want to save the file under a different name, give it a new name:

rsync -a /opt/file.zip [email protected]:/var/www/file2.zip

Use the distant location as the source and the local location as the destination to transfer data from a remote to a local machine:

rsync -a [email protected]:/var/www/file.zip /opt/

Moving folders with rsync over SSH is the same as transferring files.

It's critical to realize that source directories with a trailing slash / are treated differently by rsync. When the source directory has a trailing slash, rsync will only copy the contents of the source directory to the destination directory. If the closing slash is omitted, the source directory will be copied into the destination directory.

Type the below command to relocate the /opt/website/images/ directory from a local machine to a distant machine's /var/www/images/ directory, for example.

rsync -a /opt/website/images/ [email protected]:/var/www/images/

If you want to synchronize the local and remote directories, use the --delete option. Be warned that if files do not exist in the source directory, this option will erase them from the destination directory.

rsync -a --delete /home/vega/images/ [email protected]:/var/www/images/

Use the -e option to specify a port other than 22 on the remote host if SSH is listening on a port other than 22. For example, if SSH is listening on port 3322, you would type:

rsync -a -e "ssh -p 3322" /home/vega/images/ [email protected]:/var/www/images/

It's recommended to run the rsync command in a screen session or use the -P option, which tells rsync to show a progress bar when transferring large volumes of data and keep the files that haven't been transferred yet:

rsync -a -P /home/vega/images/ [email protected]:/var/www/images/

FAQs to Transfer Files with Rsync over SSH

Why should I use Rsync over SSH? 

Using SSH adds an extra layer of security to the file transfer process, encrypting data and protecting it from unauthorized access.

How can I monitor the progress of an ongoing Rsync transfer? 

Rsync provides a progress bar by default during file transfers. Additionally, the --progress option can be used to display detailed progress information in real-time.

What are the advantages of using Rsync over other file transfer methods? 

Rsync only transfers the changes in files, making it efficient and reducing network bandwidth usage. It also supports incremental backups and can resume interrupted transfers.

Can I transfer files between different operating systems with Rsync over SSH? 

Yes, Rsync is platform-agnostic and can be used to transfer files seamlessly between different operating systems, including Windows, macOS, and Linux.

Does Rsync verify file integrity during transfer? 

Yes, Rsync ensures file integrity by using checksums. It compares checksums of the source and destination files to verify if any changes occurred during the transfer.

Can I automate Rsync file transfers? 

Absolutely! You can set up cron jobs or schedule scripts to automate Rsync transfers, ensuring regular and seamless synchronization.

Does Rsync over SSH only transfer new or modified files? 

Yes, Rsync intelligently transfers only the changes in files, reducing transfer time and bandwidth usage.

Conclusion

We've taught you how to copy and synchronize files and directories using rsync over SSH in this tutorial.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.