Wednesday, March 26, 2025

Rsync User Guide

Rsync is a powerful file synchronization tool used to transfer files and directories between local and remote servers. It supports incremental transfers, compression, encryption, and file attribute preservation, making it ideal for backups and data migrations.
✅ 1. Basic Syntax

rsync [options] source destination

Source: A local or remote path.

Destination: Also a local or remote path.

Remote Path Format:

user@host:/path/to/directory

✅ 2. Common Options

Option Description (English)
-a Archive mode (preserve permissions, timestamps).
-v Verbose mode for detailed output.
-z Enable compression during transfer.
-e ssh Use SSH for secure transfers.
--progress Show progress during transfer.
--delete Remove extra files at the destination.
-P Enable progress display and resume interrupted transfers.
-r Recursively copy directories.
--bwlimit=KB Limit bandwidth (in KB/s).
--exclude='pattern' Exclude specific files or directories.

✅ 3. Use Cases
📍 Local Sync

rsync -av /source/path/ /destination/path/

Synchronize contents between two local directories.
📍 Local to Remote

rsync -avz /local/path/ user@remote:/remote/path/

Transfer local files to a remote server.
📍 Remote to Local

rsync -avz user@remote:/remote/path/ /local/path/

Download files from a remote server to the local machine.
📍 Remote to Remote

rsync -avz -e ssh user_A@host_A:/path/to/source/ user_B@host_B:/path/to/destination/

Transfer files between two remote servers using your local machine.
📍 Specify SSH Port

rsync -avz -e "ssh -p 2222" /local/path/ user@remote:/remote/path/

Connect using a custom SSH port.
📍 Resume Interrupted Transfers

rsync -avzP user@remote:/remote/path/ /local/path/

Resume interrupted transfers with progress display.
📍 Exclude Files

rsync -avz --exclude='*.log' --exclude='cache/' /local/path/ user@remote:/remote/path/

Exclude .log files and cache directory.
📍 Delete Extra Files on Destination

rsync -avz --delete /local/path/ user@remote:/remote/path/

Delete files on the destination that are no longer in the source.
📍 Limit Bandwidth

rsync -avz --bwlimit=1000 /local/path/ user@remote:/remote/path/

Limit transfer speed to 1000KB/s.