When managing Linux servers, transferring files between systems is a common task. rsync is one of the most powerful and efficient tools for this purpose, allowing you to sync entire folders with subfolders seamlessly.

In this guide, we’ll:

  1. Give a general description of rsync and why it’s useful.
  2. Show how to transfer a folder between two Linux servers (e.g., EC2 instances).
  3. Cover SSH options for authentication.

1. What is rsync?

rsync (Remote Sync) is a fast and versatile command-line tool for synchronizing files and directories. It’s commonly used for:

  • Backing up data.
  • Syncing directories across servers.
  • Efficiently transferring files by copying only the changes (delta transfer).

Why Use rsync?

  • Bandwidth-efficient: Transfers only modified portions of files.
  • Preserves file attributes: Maintains permissions, ownership, and timestamps.
  • Versatile: Works over SSH for secure transfers.

2. How to Use rsync to Transfer Folders Between Servers

Let’s say we need to transfer the folder /home/user/data from Server A to Server B.

Basic Syntax

rsync -avz /source/directory/ user@destination:/path/to/destination/
  • -a: Archive mode (preserves permissions, timestamps, symbolic links, etc.).
  • -v: Verbose (displays progress).
  • -z: Compresses data during transfer.

Example: Transferring a Folder

To transfer the folder /home/ubuntu/data from one EC2 instance to another:

rsync -avz /home/ubuntu/data/ ec2-user@destination-server:/home/ec2-user/data/

3. Using SSH for Authentication

rsync uses SSH by default for secure transfers. Let’s cover common SSH options.

Key-Based Authentication

  1. Generate an SSH key pair (if not already done):

    ssh-keygen -t rsa -b 4096
    
  2. Copy the public key to the destination server:

    ssh-copy-id ec2-user@destination-server
    
  3. Test SSH access:

    ssh ec2-user@destination-server
    

Now, rsync will work without requiring a password.


Using a Custom SSH Port

If the destination server uses a non-standard SSH port (e.g., 2222), specify it with the -e option:

rsync -avz -e "ssh -p 2222" /home/ubuntu/data/ ec2-user@destination-server:/home/ec2-user/data/

Using an SSH Key File

If your SSH key file isn’t in the default location (~/.ssh/id_rsa), specify it with the -e option:

rsync -avz -e "ssh -i /path/to/private-key.pem" /home/ubuntu/data/ ec2-user@destination-server:/home/ec2-user/data/

4. Practical Example: Syncing EC2 Instances

Imagine you want to transfer logs from an application running on Server A to Server B.

  1. Source Server (Server A):

    • Folder: /var/logs/myapp
    • SSH User: ubuntu
    • IP: 192.168.1.100
  2. Destination Server (Server B):

    • Destination: /backups/logs/
    • SSH User: ubuntu
    • IP: 192.168.1.101

Command:

rsync -avz -e "ssh -i /path/to/private-key.pem" /var/logs/myapp/ ubuntu@192.168.1.101:/backups/logs/

5. Recap

  • rsync is a powerful tool for transferring and syncing files between Linux servers.
  • It works efficiently over SSH for secure communication.
  • You can customize SSH authentication using key pairs, custom ports, or specific key files.

Conclusion

With rsync, you can handle file transfers and backups efficiently, whether it’s between two EC2 instances or any Linux servers. Its flexibility and security make it an indispensable tool for system administrators.

Let me know if you have additional use cases or tips to share. Happy syncing! 🚀

Disclaimer: At MojaLab, we aim to provide accurate and useful content, but hey, we’re human (well, mostly)! If you spot an error, have questions, or think something could be improved, feel free to reach out—we’d love to hear from you. Use the tutorials and tips here with care, and always test in a safe environment. Happy learning!