The Rsync Guide
Everything you should know about Rsync
Introduction
This guide covers rsync, a fast and versatile command-line utility for synchronizing files / directories between two locations over a network.
You'll learn the basics of rsync, understand it's process, and practical usage scenarios such as data synchronization (transfer & deletion), excluding data during operations, bandwidth limiting and looging setup.
Rsync Basics
- Source: The location of the files or directories you want to copy (e.g. local path or a remote server).
 - Destination: The location where you want to copy the files or directories to (e.g. local path or a remote server).
 - Options: 
rsyncprovides various options to customize the synchronization process, such as preserving file permissions, compressing data, and excluding files. 
Rsync Process
- 
Initiate Connection:
rsynccan operate over SSH to securely transfer data. The connection is established using SSH credentials if specified.
 - 
File Comparison:
rsynccompares files between the source and destination, determining which files need to be transferred based on differences in size, modification time, or checksum.
 - 
Data Transfer:
- Only the differences between files are transferred, minimizing bandwidth usage. This is achieved through a process called delta encoding.
 
 
Rsync Usage
1. Basic file synchronization
1rsync -avz /path/to/source/ /path/to/destination/
2-a: Archive mode, preserves permissions, timestamps, and other attributes.-v: Verbose output, shows the progress of the transfer.-z: Compresses data during transfer for efficiency.
2. Synchronize with a remote server
1rsync -avz -e ssh /path/to/source/ user@remote:/path/to/destination/
2-e ssh: Specifies SSH as the remote shell for secure data transfer.
3. Exclude specific files or directories
Option 1:
1rsync -avz --exclude='*.tmp' /path/to/source/ /path/to/destination/
2--exclude='*.tmp': Excludes all files with the.tmpextension from the transfer.
Option 1:
exclude specific files or directories using a list with .rsyncignore which works similar to .gitignore.
First, create a file named .rsyncignore (or any name you prefer) and add the patterns of files or directories you want to exclude. Each pattern should be on a separate line. For example:
1venv/
2node_modules/
3*.log
4Use the --exclude-from option with rsync to specify this file:
1rsync -avz --exclude-from='.rsyncignore' /path/to/source/ /path/to/destination/
2--exclude-from='.rsyncignore': This tellsrsyncto read the exclusion patterns from the specified file.rsyncignore. This is useful for managing multiple exclusions without cluttering the command line.
4. Delete files in the destination that are not present in the source
1rsync -avz --delete /path/to/source/ /path/to/destination/
2--delete: Removes files from the destination that are not present in the source.
5. Dry run to preview changes
1rsync -avz --dry-run /path/to/source/ /path/to/destination/
2--dry-run: Simulates the synchronization process without making any changes, useful for verifying what will be transferred.
6. Limit bandwidth usage
1rsync -avz --bwlimit=5000 /path/to/source/ /path/to/destination/
2--bwlimit=5000: Limits the transfer speed to 5000 KB/s.
7. Logging transfer details
1rsync -avz --log-file=/path/to/logfile.log /path/to/source/ /path/to/destination/
2--log-file=/path/to/logfile.log: Records the details of the transfer to a specified log file.