How to Use rsync for Efficient File Transfers and Backups

rsync remains the standard tool for exactly this, efficiently transferring only what’s actually changed rather than copying everything from scratch ever…

rsync

Restic () handles encrypted, deduplicated backups well, but plenty of simpler homelab tasks — mirroring a directory to a NAS, syncing a website’s files, moving data between servers — call for something more direct. rsync remains the standard tool for exactly this, efficiently transferring only what’s actually changed rather than copying everything from scratch every time.

What Makes rsync Different from a Simple Copy

Unlike cp or a basic file copy, rsync specifically calculates the differences between source and destination, transferring only the changed portions of files rather than the entire file every time — a process called delta transfer. For repeated syncs of largely similar data (a growing log directory, a slowly changing website), this makes subsequent runs dramatically faster than a full copy would be.

Basic rsync Syntax

rsync -av /source/directory/ /destination/directory/
  • -a (archive mode) – preserves permissions, timestamps, symbolic links, and recursively copies directories, generally the flag combination you want for most real backup or mirroring purposes
  • -v (verbose) – shows what’s being transferred, useful for confirming behavior before relying on it unattended

The Trailing Slash Matters

A commonly confusing rsync detail: whether the source path ends with a trailing slash changes behavior significantly:

rsync -av /source/folder /destination/     # Creates /destination/folder/
rsync -av /source/folder/ /destination/    # Copies contents into /destination/ directly

Getting this wrong is a common source of “why did rsync create an extra nested folder” confusion — always double-check the trailing slash matches your actual intent.

Syncing to a Remote Server

rsync works seamlessly over SSH, making it straightforward to sync directly between servers:

rsync -av /local/directory/ user@remote-server:/remote/directory/

This uses the same SSH authentication already configured throughout this series (key-based auth from the hardening guide), requiring no separate credential setup.

Deleting Files That No Longer Exist in Source

By default, rsync only adds and updates files, never removing anything from the destination even if it’s been deleted from the source. For real mirroring where the destination should exactly match the source:

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

Use --delete carefully — a source directory issue could propagate as unintended deletions on the destination, so consider testing with --dry-run first for anything genuinely important.

Testing Before Running for Real

rsync -av --dry-run /source/directory/ /destination/directory/

This shows exactly what rsync would do without actually transferring or deleting anything, a valuable habit particularly before running any command including --delete against truly important data.

Excluding Specific Files or Patterns

rsync -av --exclude='*.log' --exclude='cache/' /source/ /destination/

Useful for excluding temporary files, caches, or anything you specifically don’t want included in a given sync operation.

Using rsync for Website or Application Deployment

Combined with the bash scripting and cron/systemd timer automation covered in earlier guides, rsync is commonly used to deploy updated files to a server:

bash

#!/bin/bash
rsync -av --delete /home/user/website-build/ user@webserver:/var/www/html/

Bandwidth Limiting for Large Transfers

For large transfers where you don’t want to saturate your connection (particularly relevant if you’re also running other services depending on network availability):

rsync -av --bwlimit=5000 /source/ user@remote:/destination/

This limits transfer to roughly 5000 KB/s, leaving headroom for other network activity during the transfer.

Combining rsync with cron for Scheduled Mirroring

For a directory that should stay continuously mirrored to a secondary location:

0 */6 * * * rsync -av --delete /important/data/ /mnt/backup-drive/data-mirror/

This runs every 6 hours, keeping the backup drive’s mirror reasonably current without needing manual intervention.

rsync vs Restic: When to Use Which

rsync excels at straightforward mirroring and efficient file transfer where you want the destination to really match the source (or a filtered subset of it). Restic () is the better choice specifically when you need encryption, deduplication across multiple point-in-time snapshots, or the ability to restore to a specific previous state rather than just the most recent mirror — the two tools solve related but actually distinct problems.

Final Thoughts

rsync for file transfers and backups remains one of the most fundamentally useful tools for a homelab, efficiently handling everything from simple directory mirroring to server-to-server synchronization and deployment automation. Understanding its efficient delta-transfer behavior, the trailing slash nuance, and safe practices like --dry-run testing makes it a reliable foundation for countless file movement tasks referenced implicitly throughout this series.

Related Posts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *