# **The Complete Rsync Guide: Mastering File Synchronization** Rsync (*Remote Synchronization*) is one of the most powerful and efficient tools for copying and synchronizing files locally or across networks. It’s widely used for backups, mirroring, and deploying files efficiently by only transferring changes. This guide covers: ✔ **Basic to advanced rsync usage** ✔ **Trailing slash rules (critical!)** ✔ **Local & remote sync (SSH)** ✔ **Exclusions, deletions, and permissions** ✔ **Performance optimization** ✔ **Real-world examples & scripts** --- ## **1. Installation & Basic Usage** ### **Installation** - **Linux (Debian/Ubuntu)**: ```sh sudo apt install rsync ``` - **Linux (RHEL/CentOS)**: ```sh sudo yum install rsync ``` - **macOS**: ```sh brew install rsync # via Homebrew ``` - **Windows**: - Use **WSL (Windows Subsystem for Linux)** - Or **cwRsync** (native Windows port) ### **Basic Command Structure** ```sh rsync [OPTIONS] SOURCE DESTINATION ``` - **`SOURCE`**: The files/folders to copy. - **`DESTINATION`**: Where to copy them. --- ## **2. Critical: Trailing Slash Rules** The **trailing slash (`/`)** changes behavior drastically: | Command | Effect | |---------|--------| | `rsync /source /dest` | Copies **entire `/source` folder** into `/dest/source` | | `rsync /source/ /dest` | Copies **only contents** of `/source/` into `/dest/` | **Example:** ```sh rsync -a ~/photos/ /backup/ # Copies files inside ~/photos/ to /backup/ rsync -a ~/photos /backup/ # Creates /backup/photos/ with all files inside ``` **⚠ Always test with `-n` (dry run) first!** --- ## **3. Essential Rsync Options** | Option | Meaning | |--------|---------| | `-a` | Archive mode (recursive + preserve permissions) | | `-v` | Verbose (show progress) | | `-z` | Compress during transfer | | `-h` | Human-readable file sizes | | `-P` | Show progress + resume interrupted transfers | | `--delete` | Delete files in destination not in source | | `-n` | Dry run (simulate without copying) | | `-e ssh` | Use SSH for remote transfers | --- ## **4. Local & Remote File Syncing** ### **Copy Locally** ```sh rsync -avh /source/folder/ /destination/ ``` ### **Copy to Remote Server (Push)** ```sh rsync -avzP -e ssh /local/path/ user@remote-server:/remote/path/ ``` ### **Copy from Remote Server (Pull)** ```sh rsync -avzP -e ssh user@remote-server:/remote/path/ /local/path/ ``` --- ## **5. Advanced Usage** ### **Exclude Files/Folders** ```sh rsync -av --exclude='*.tmp' --exclude='cache/' /source/ /dest/ ``` Or use an **exclude file** (`exclude-list.txt`): ```sh rsync -av --exclude-from='exclude-list.txt' /source/ /dest/ ``` ### **Delete Extraneous Files (`--delete`)** ```sh rsync -av --delete /source/ /dest/ # Removes files in dest not in source ``` ### **Limit Bandwidth (e.g., 1MB/s)** ```sh rsync -avz --bwlimit=1000 /source/ user@remote:/dest/ ``` ### **Partial Transfer Resume** ```sh rsync -avzP /source/ user@remote:/dest/ # -P allows resuming ``` --- ## **6. Real-World Examples** ### **1. Backup Home Directory** ```sh rsync -avh --delete --exclude='Downloads/' ~/ /backup/home/ ``` ### **2. Mirror a Website (Excluding Cache)** ```sh rsync -avzP --delete --exclude='cache/' user@webserver:/var/www/ /local/backup/ ``` ### **3. Sync Large Files with Bandwidth Control** ```sh rsync -avzP --bwlimit=5000 /big-files/ user@remote:/backup/ ``` --- ## **7. Performance Tips** - **Use `-z`** for compression over slow networks. - **Use `--partial`** to keep partially transferred files. - **Avoid `-a` if not needed** (e.g., `-rlt` for lightweight sync). - **Use `rsync-daemon`** for frequent large transfers. --- ## **8. Common Mistakes & Fixes** | Mistake | Fix | |---------|-----| | Accidentally reversing source/dest | **Always test with `-n` first!** | | Forgetting trailing slash | **Check paths before running!** | | `--delete` removing needed files | **Use `--dry-run` before `--delete`** | | Permission issues | Use `--chmod` or `sudo rsync` | --- ## **9. Scripting & Automation** ### **Cron Job for Daily Backup** ```sh 0 3 * * * rsync -avz --delete /important-files/ user@backup-server:/backup/ ``` ### **Logging Rsync Output** ```sh rsync -avzP /source/ /dest/ >> /var/log/rsync.log 2>&1 ``` --- ## **Final Thoughts** Rsync is **incredibly powerful** once mastered. Key takeaways: ✅ **Trailing slash (`/`) matters!** ✅ **Use `-a` for backups, `-z` for slow networks.** ✅ **Test with `-n` before `--delete`.** ✅ **Automate with cron for scheduled syncs.** Want even deeper control? Explore `rsync --daemon` for server setups! 🚀 **Need help with a specific scenario? Ask away!**