DevOps Blog

A Beginner's Guide to Rsync: The Ultimate File Synchronization Tool

What is Rsync?

rsync (remote sync) is a fast and versatile file-copying tool that can:

  • Synchronize files between two locations.
  • Copy files locally (like cp but with more control).
  • Transfer files over SSH securely.
  • Perform incremental backups (only copying changed files).
  • Preserve permissions, ownership, and timestamps.

Unlike a simple cp or scp command, rsync only transfers the differences between files, making it much faster for large datasets.


Basic Rsync Syntax

The general structure of an rsync command is:

rsync [options] source destination
  • Source: The file(s) or directory you want to copy.
  • Destination: Where you want to copy them to.

Common Rsync Options

| Option | Description | |--------|-------------| | -a | Archive mode (preserves permissions, ownership, timestamps) | | -v | Verbose output (shows progress) | | -z | Compress data during transfer (faster over networks) | | -h | Human-readable output (shows sizes in KB, MB, etc.) | | -P | Shows progress and allows resuming interrupted transfers | | -n | Dry run (simulates what would happen without making changes) | | --delete | Deletes files in destination that don’t exist in source | | -e ssh | Uses SSH for remote transfers (secure) |


Practical Rsync Examples

1. Copying Files Locally

To copy a directory (/home/user/docs) to a backup location (/backup):

rsync -av /home/user/docs /backup

2. Syncing Files Over SSH

To sync a local folder to a remote server:

rsync -avz -e ssh /local/path/ user@remote-server:/remote/path/

3. Deleting Extra Files in Destination

If you want the destination to exactly match the source (deleting extra files):

rsync -av --delete /source/ /destination/

4. Excluding Certain Files

To skip specific files or directories (e.g., node_modules or .tmp files):

rsync -av --exclude='node_modules' --exclude='*.tmp' /source/ /destination/

5. Limiting Bandwidth Usage

To avoid saturating your network (e.g., limit to 500 KB/s):

rsync -avz --bwlimit=500 /source/ user@remote:/destination/

6. Dry Run (Test Before Running)

To see what rsync would do without actually copying anything:

rsync -avn /source/ /destination/

Conclusion

rsync is an incredibly powerful tool for file synchronization and backups. Whether you're managing local backups, deploying files to a remote server, or keeping directories in sync, rsync provides efficiency and flexibility.

Pro Tip: Combine rsync with cron to automate regular backups!

Have you used rsync before? What’s your favorite use case? Let me know in the comments!