Rsync doesn’t actually copy files; it intelligently compares file checksums and only transfers the differences between them.

Imagine you have a directory ~/Documents on your local machine and you want to back it up to a remote server at user@remote.server.com in the directory /backup/documents.

Here’s how you’d do it with rsync:

rsync -avz --delete ~/Documents/ user@remote.server.com:/backup/documents/

Let’s break down this command:

  • -a (archive mode): This is a shorthand for several options (-rlptgoD). It means "recurse into directories, copy symlinks as symlinks, preserve permissions, preserve modification times, preserve group, preserve owner, and preserve device files." This is usually what you want for backups.
  • -v (verbose): Shows you what files are being transferred. Useful for monitoring.
  • -z (compress): Compresses file data during the transfer, saving bandwidth.
  • --delete: This is the critical part for a true mirror. It tells rsync to delete files on the destination that no longer exist on the source. Without this, your backup would only ever grow, never truly reflecting the current state of your source directory.

The trailing slashes are important!

  • ~/Documents/ (with a slash): Tells rsync to copy the contents of ~/Documents into the destination directory.
  • ~/Documents (without a slash): Tells rsync to copy the Documents directory itself into the destination directory, resulting in /backup/documents/Documents/.

The full command would create a directory /backup/documents on remote.server.com (if it doesn’t exist) and then populate it with the contents of your local ~/Documents directory, making sure to delete anything on the remote that you’ve removed locally.

This efficiency comes from rsync’s "delta-transfer algorithm." When rsync connects to the destination, it doesn’t just start copying. It first builds a list of files at the source and destination. For each file, it calculates checksums (like MD5 or SHA-1) of blocks within the file. It then compares these checksums between the source and destination. Only the blocks that have changed are sent over the network. This is why subsequent backups are so much faster than the initial one.

You have fine-grained control over what rsync does. For example:

  • --exclude 'pattern': Skip files matching pattern. E.g., --exclude '*.tmp' to skip temporary files.
  • --include 'pattern': Don’t exclude files matching pattern. This is useful when combined with a broader exclude rule.
  • --progress: Shows a progress bar for each file.
  • --bwlimit=KBPS: Limits the transfer speed to KBPS kilobytes per second.

Let’s look at a more complex scenario: backing up your entire home directory (~) to a different remote server (backup@storage.lan) but excluding your Downloads and VirtualBox VMs directories.

rsync -avz --delete --exclude 'Downloads/' --exclude 'VirtualBox VMs/' ~ backup@storage.lan:/home/backup/my_home_directory/

Here, ~ represents your entire home directory. We’re excluding specific subdirectories. Notice the destination path is /home/backup/my_home_directory/. If this directory doesn’t exist on storage.lan, rsync won’t create it by default unless you use the -R (relative path) or -a (which implies recursion, thus creating intermediate directories if needed). However, it’s generally safer to ensure the parent directories exist.

A common pitfall when using --delete is accidentally wiping out data. Always, always run rsync with the -n (dry run) option first to see exactly what rsync would do without actually doing it.

rsync -avn --delete ~/Documents/ user@remote.server.com:/backup/documents/

This will print a list of files that would be added, deleted, or changed. Review this output carefully before committing to the actual sync.

The rsync daemon mode is another way to use rsync, where the rsync server runs as a background process, listening on a specific port (default 873). This allows for more efficient, non-SSH based transfers, especially for frequent, automated backups. You’d configure a rsyncd.conf file on the server and then connect using rsync rsync://user@remote.server.com/backup/documents/.

The power of rsync lies in its incremental nature and robust options. It’s not just a file copy; it’s a sophisticated synchronization tool.

The next step in mastering rsync is understanding its filtering rules and how to construct complex include/exclude patterns that can even depend on file attributes or directory depth.

Want structured learning?

Take the full Linux & Systems Programming course →