Cheatsheet & Examples: rsync
rsync is a powerful command-line utility for synchronizing files and directories between locations, efficiently copying only the changes and supporting compression, encryption, and more.
Basic File Copy
Example Usage:rsync -av /source/directory/ /destination/directory/
What it does:
Copies files from the source to the destination, preserving permissions, timestamps, and other attributes.
Command-line Arguments Explained:
-a: Archive mode, which preserves permissions, ownership, timestamps, symbolic links, and other file attributes.-v: Verbose mode, providing detailed output during the transfer.
Synchronize with a Remote Server
Example Usage:rsync -avz /local/path/ user@remote:/remote/path/
What it does:
Transfers files to a remote server using SSH (via -z compression) and preserves attributes.
Command-line Arguments Explained:
-a: Archive mode for preserving file properties.-v: Verbose output for tracking progress.-z: Compress file data during transfer to reduce bandwidth usage.
Mirror Source to Destination (Delete Extra Files)
Example Usage:rsync -av --delete /source/directory/ /destination/directory/
What it does:
Mirrors the source directory to the destination, removing files in the destination that aren't in the source.
Command-line Arguments Explained:
-a: Archive mode for preserving attributes.-v: Verbose output.--delete: Deletes files in the destination that don't exist in the source.
Recursive File Copy (Include Subdirectories)
Example Usage:rsync -r /source/directory/ /destination/directory/
What it does:
Copies all files and subdirectories recursively from the source to the destination.
Command-line Arguments Explained:
-r: Recursive mode to copy directories and their contents.
Exclude Specific Files or Directories
Example Usage:rsync -av --exclude='*.tmp' /source/ /destination/
What it does:
Copies files from the source to the destination while excluding files matching the specified pattern (e.g., .tmp files).
Command-line Arguments Explained:
-a: Archive mode.-v: Verbose output.--exclude='pattern': Skips files/directories matching the pattern during transfer.
Show Progress During Transfer
Example Usage:rsync -av --progress /source/ /destination/
What it does:
Displays real-time progress of the file transfer, including percentage, speed, and estimated time.
Command-line Arguments Explained:
-a: Preserves file attributes.-v: Verbose output.--progress: Shows progress details for each file.
Dry Run (Test Without Actual Copy)
Example Usage:rsync -av --dry-run /source/ /destination/
What it does:
Simulates the sync operation without copying any files, helping verify the command's behavior.
Command-line Arguments Explained:
-a: Preserves attributes.-v: Verbose output.--dry-run: Performs a trial run without modifying the destination.
Use Checksum for Exact File Comparison
Example Usage:rsync -av --checksum /source/ /destination/
What it does:
Ensures files are copied based on checksums (not just size or timestamp) for precise synchronization.
Command-line Arguments Explained:
-a: Preserves attributes.-v: Verbose output.--checksum: Compares files using checksums instead of size/timestamp.
Backup Existing Files on Destination
Example Usage:rsync -av --backup /source/ /destination/
What it does:
Copies files to the destination and backs up existing files with a suffix (e.g., .orig) if they are overwritten.
Command-line Arguments Explained:
-a: Preserves attributes.-v: Verbose output.--backup: Creates backups of destination files before overwriting.
Compress Data During Transfer
Example Usage:rsync -avz /local/path/ user@remote:/remote/path/
What it does:
Transfers files to a remote location while compressing data to save bandwidth.
Command-line Arguments Explained:
-a: Archive mode.-v: Verbose output.-z: Compresses data during transfer.
Synchronize with a Remote Server via SSH
Example Usage:rsync -av -e 'ssh -p 2222' /local/path/ user@remote:/remote/path/
What it does:
Uses SSH with a custom port (e.g., 2222) to securely synchronize files with a remote server.
Command-line Arguments Explained:
-a: Preserves file attributes.-v: Verbose output.-e 'ssh -p port': Specifies the remote shell (SSH) and port for the connection.
Transfer Files to a Remote Server with Compression
Example Usage:rsync -avz /local/path/ user@remote:/remote/path/
What it does:
Copies files to a remote server with compression enabled, optimized for network transfers.
Command-line Arguments Explained:
-a: Archive mode.-v: Verbose output.-z: Compresses data during transfer.
Synchronize with a Remote Server Using Rsync Protocol
Example Usage:rsync -av /local/path/ user@remote::module/
What it does:
Transfers files to a remote server using the rsync protocol (not SSH), assuming the remote has a defined module.
Command-line Arguments Explained:
-a: Preserves attributes.-v: Verbose output.user@remote::module: Specifies the remote host and rsync module to use.
Exclude Multiple Patterns
Example Usage:rsync -av --exclude='logs/' --exclude='*.tmp' /source/ /destination/
What it does:
Copies files while excluding multiple specified directories and file types.
Command-line Arguments Explained:
-a: Preserves attributes.-v: Verbose output.--exclude='pattern': Excludes files/directories matching the pattern (can be used multiple times).
Use Checksum and Backup Together
Example Usage:rsync -av --checksum --backup /source/ /destination/
What it does:
Copies files using checksum validation and backs up existing destination files.
Command-line Arguments Explained:
-a: Preserves attributes.-v: Verbose output.--checksum: Compares files via checksum.--backup: Creates backups of overwritten files.

