How to Use Rsync to Copy Files Over SSH

Posted on

How to Use Rsync to Copy Files Over SSH

How to Use Rsync to Copy Files Over SSH

Introduction

Rsync, short for Remote Sync, is a powerful and versatile command-line utility designed for efficient file and directory transfer. It excels in synchronizing data between local and remote locations, making it an indispensable tool for mirroring, backups, and data migration to other servers. Its speed and efficiency stem from its ability to replicate only the changes from the source, minimizing data transfer and maximizing performance. Moreover, Rsync offers extensive customization options, allowing users to tailor its behavior to specific needs.

This article will explore how to use Rsync to securely copy files over SSH, ensuring data integrity and confidentiality during the transfer process. We’ll cover the standard method outlined in the original article and then delve into alternative approaches that offer different advantages.

Set up an SSH connection with the destination server.

Rsync over SSH leverages the secure communication channel provided by SSH. This means you can use a standard login with a password or, for enhanced security, a Private key for SSH authentication. Using a private key eliminates the need to enter a password each time, streamlining the process and reducing the risk of password-related vulnerabilities.

In this example, we’ll copy a file from Server A (192.168.182.130) located in /root/file-to-send.zip to Server B (192.168.182.131) and save it to /root/new-file.zip.

Step 1 : Connect to server A and locate the file.

First, establish an SSH connection to Server A and verify the existence of the file you intend to transfer.

$ ssh root@ServerA_ip
$ ls
Output:
root@local:~# ssh root@192.168.182.130
root@192.168.182.130's password:
root@ServerA:~# ls
file-to-send.zip

Step 2 : Use rsync to transfer the file.

Now, use Rsync to transfer the file from Server A to Server B over SSH.

$ rsync -avz /root/file-to-send.zip root@ServerB_ip:/root/new-file.zip
Output:
root@ServerA:~# rsync -avz /root/file-to-send.zip root@192.168.182.131:/root/new-file.zip
sending incremental file list
file-to-send.zip
sent 10,600 bytes received 35 bytes 21,270.00 bytes/sec
total size is 131,604 speedup is 12.37

For large files, adding a progress bar with the --progress option enhances the user experience.

Our command will become :

$ rsync -avz /root/file-to-send.zip root@ServerB_ip:/root/new-file.zip --progress
Output:
root@ServerA:~# rsync -avz /root/file-to-send.zip root@192.168.182.131:/root/new-file.zip --progress
sending incremental file list
file-to-send.zip
131,604 100% 94.26MB/s 0:00:00 (xfr#1, to-chk=0/1)
sent 10,600 bytes received 35 bytes 7,090.00 bytes/sec
total size is 131,604 speedup is 12.37

Let’s break down the Rsync options used:

  • <kbd>-v, –verbose:</kbd> This option provides detailed information about the transfer process, showing which files are being transferred and any errors that occur.
  • <kbd>-a, –archive:</kbd> This option enables archive mode, which preserves file permissions, ownership, timestamps, and symbolic links during the transfer. It’s equivalent to using -rlptgoD.
  • <kbd>-z, –compress:</kbd> This option compresses the data during transfer, reducing bandwidth usage and potentially speeding up the process, especially over slow network connections.

Step 3 : Checking the transfer of file.

After the transfer completes, verify that the file has been successfully copied to Server B.

$ ssh root@ServerB_ip
$ ls
Output :
root@ServerA:~# ssh root@192.168.182.131
root@ServerB:~# ls
new-file.zip snap

This confirms that new-file.zip has been successfully transferred to Server B. This is how to use Rsync.

Alternative Solutions for Copying Files Over SSH

While Rsync is an excellent tool, alternative methods exist for copying files over SSH, each with its own strengths and weaknesses. Let’s explore two such alternatives: scp and sftp.

1. Using scp (Secure Copy)

scp is a command-line utility that provides a simple and secure way to copy files between hosts over an SSH connection. It’s part of the SSH suite and is often readily available on most Unix-like systems.

Explanation:

scp relies on SSH for authentication and encryption, ensuring the confidentiality and integrity of the transferred data. It’s generally simpler to use than Rsync for basic file transfers, particularly when you don’t need the advanced features like incremental transfer and synchronization. However, scp lacks Rsync’s efficiency in handling incremental updates, as it always copies the entire file, even if only parts of it have changed.

Code Example:

To copy the same file from Server A to Server B using scp, the command would be:

$ scp /root/file-to-send.zip root@ServerB_ip:/root/new-file.zip

or

$ scp /root/file-to-send.zip root@192.168.182.131:/root/new-file.zip

This command directly copies file-to-send.zip from Server A to the /root/new-file.zip location on Server B. You will be prompted for the password for the root user on Server B (unless you have SSH key-based authentication configured).

Pros of scp:

  • Simplicity: Easier to use for basic file transfers.
  • Availability: Typically pre-installed on most Unix-like systems.
  • Security: Leverages SSH for secure data transfer.

Cons of scp:

  • Inefficiency: Always copies the entire file, even for small changes.
  • Limited Features: Lacks advanced features like incremental transfer and synchronization.

2. Using sftp (Secure File Transfer Protocol)

sftp is an interactive file transfer program, also part of the SSH suite, that provides a secure and interactive environment for managing files on remote servers. It’s similar to FTP but operates over an encrypted SSH connection.

Explanation:

sftp allows you to connect to a remote server, browse its file system, and upload or download files using interactive commands. This can be useful for more complex file management tasks, such as transferring multiple files or directories, creating directories, and changing file permissions. Like scp, sftp relies on SSH for security.

Code Example:

To use sftp to transfer the file, you would first connect to Server B:

$ sftp root@ServerB_ip

or

$ sftp root@192.168.182.131

You’ll be prompted for the password for the root user on Server B (unless you have SSH key-based authentication configured). Once connected, you can use the put command to upload the file:

sftp> put /root/file-to-send.zip /root/new-file.zip

To exit sftp, use the exit command:

sftp> exit

Pros of sftp:

  • Interactive: Allows for interactive file management and browsing.
  • Security: Leverages SSH for secure data transfer.
  • Flexibility: Supports various file management tasks.

Cons of sftp:

  • More Complex: Requires learning sftp commands.
  • Less Suitable for Automation: Primarily designed for interactive use.

Conclusion

Rsync, scp, and sftp each provide ways to securely copy files over SSH. Rsync excels in efficiency, especially for incremental updates and synchronization. scp offers simplicity for basic file transfers, while sftp provides an interactive environment for more complex file management. The best choice depends on your specific needs and the complexity of the task at hand. When you transfer large files, Rsync is a good choice. Understanding the strengths and weaknesses of each tool allows you to select the most appropriate method for your file transfer needs. Ultimately, knowing how to use Rsync is a valuable skill.

Leave a Reply

Your email address will not be published. Required fields are marked *