Using `rsync` to Synchronize Files with SSH

2 min read .

rsync is a powerful tool for transferring and synchronizing files between local and remote systems. One common method of using rsync is through an SSH connection, which allows for secure file transfers. In this guide, we’ll discuss how to use the rsync command to synchronize files from a local directory to a remote directory over SSH.

The rsync Command

The following command is an example of the rsync syntax used for file synchronization:

rsync -Pvaze "ssh" /home/username/* root@host:/home/username/

Let’s break down the components of this command:

  1. rsync:

    • The main tool used for synchronizing files.
  2. -P:

    • Activates two options: --partial and --progress.
    • --partial ensures that partially transferred files are kept so if the transfer is interrupted, it can be resumed from where it left off.
    • --progress displays information about the progress of the file transfer.
  3. -v:

    • The --verbose option provides detailed information about the file transfer process. It gives additional information about the files being transferred.
  4. -a:

    • The --archive option is a mode that enables several options at once: -r (recursive), -l (copy symlinks), -p (preserve permissions), -t (preserve modification times), -g (preserve group), and -o (preserve owner).
  5. -z:

    • The --compress option enables compression during transfer to reduce the size of the data being sent over the network.
  6. -e "ssh":

    • This option specifies that rsync should use SSH as the protocol for connecting to the remote server, ensuring the data transfer is secure.
  7. /home/username/*:

    • The local source directory containing the files to be synchronized. The * indicates that all files and subdirectories within this directory will be included.
  8. root@host:/home/username/:

    • The remote destination address where the files will be copied. root is the username on the remote server, host is the IP address or hostname of the remote server, and /home/username/ is the destination directory on the remote server.

Example Usage

For example, if you want to synchronize files from the /home/user/documents/ directory on your local system to the /home/user/backup/ directory on a remote server with the address 192.168.1.100, you can use the following command:

rsync -Pvaze "ssh" /home/user/documents/* [email protected]:/home/user/backup/

Benefits of Using rsync with SSH

  • Security: Using SSH ensures that the data being transferred is encrypted, making it secure from potential eavesdropping.
  • Efficiency: rsync only transfers the differences between the source and destination, reducing the amount of data that needs to be sent.
  • Flexibility: With various available options, rsync can be configured to meet your specific needs.

Conclusion

rsync is a powerful tool for efficiently and securely transferring and synchronizing files. By using options like -P, -v, -a, -z, and -e "ssh", you can optimize your file transfer process and ensure that your data is securely transmitted over SSH. Be sure to adjust the rsync command to suit your specific needs for the best results.

Tags:
Linux

See Also

chevron-up