Synchronizing Directories between Servers with Rsync

In the realm of server management, there comes a time when you need to synchronize or transfer data between servers. Whether it’s for backup, migration, or redundancy, rsync is an invaluable tool for this purpose. In this tutorial, we’ll walk through syncing specific directories from one server to another using rsync.

Prerequisites:

  • Two servers: a source server and a destination server.
  • rsync installed on the source server.
  • SSH access to both servers.

Example Scenario:

  • Source Server Directory: /var/example/source-directory
  • Destination Server IP: 203.0.113.10
  • Destination Directory: /var/example/destination-directory
  • User for transfer: user123

Step-by-Step Guide:

Prepare the Destination Server: Before initiating the rsync transfer, ensure that the target directory exists on the destination server. If not, you’ll need to create it:

SSH into the destination server (203.0.113.10):

ssh user123@203.0.113.10

Once logged in, create the directory:

sudo mkdir -p /var/example/destination-directory

Exit back to your source server:

exit

Synchronize Using Rsync:

  1. From your source server, execute the following command to initiate the transfer:

    rsync -avz /var/example/source-directory/ user123@203.0.113.10:/var/example/destination-directory/

    Flags explained:

    • -a: This ensures that file permissions, timestamps, symbolic links, and other attributes are preserved.
    • -v: Enables verbose mode to display files being transferred.
    • -z: Compresses data during transfer, beneficial for large datasets.

    Remember to include the trailing slash at the end of the source directory to ensure the contents are copied, not the directory itself.

  2. Optional – Set Up SSH Key Authentication: For frequent data transfers or automations, consider establishing SSH key-based authentication to avoid manual password inputs during transfers. This ensures smoother and potentially automated sync operations.

Conclusion:

With rsync, synchronizing directories between servers becomes a breeze. It’s efficient, only transferring differences, and can be combined with SSH keys for added convenience. Whether you’re moving data for backups, migrations, or other reasons, rsync is an indispensable tool in a server admin’s toolkit.

Leave a Reply

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