rsync over SSH: incremental backups at no cost

Published on 03/05/2026 Linux
The Firmware Factory
Advertisement
The Firmware Factory — custom firmware development for embedded devices
Learn more

rsync is an old but indestructible Unix tool. It transfers only the differences between source and destination, supports SSH as its transport, and with the --link-dest option it allows Time Machine-style incremental snapshots without duplicating unchanged files.

1. Basic backup to a NAS

rsync -avzP /home/marco/ marco@nas.local:/volume1/backups/marco/

What the flags mean:

  • -a archive: preserves permissions, timestamps, symlinks and so on.
  • -v verbose: shows what it is doing.
  • -z compresses during the transfer (useful over Wi-Fi/WAN).
  • -P progress plus resuming of partial transfers.

2. Exclusions

You do not want to back up caches, builds or node_modules:

rsync -avz \
  --exclude '.cache/' \
  --exclude 'node_modules/' \
  --exclude '*.tmp' \
  /home/marco/ marco@nas.local:/volume1/backups/marco/

For long exclusion lists, put them in a file:

# ~/backup-excludes.txt
.cache/
node_modules/
.git/objects/pack/
*.iso
*.tmp

# Usage:
rsync -avz --exclude-from=~/backup-excludes.txt \
  /home/marco/ marco@nas.local:/volume1/backups/marco/

3. Incremental snapshots with --link-dest

The idea: every night you create a new folder for the current date, but the unchanged files are hard links to the previous night. Space cost: only the files that changed.

#!/bin/bash
DEST=marco@nas.local:/volume1/backups/marco
TODAY=$(date +%Y-%m-%d)
LATEST=$(ssh marco@nas.local "readlink /volume1/backups/marco/latest" 2>/dev/null || echo "")

rsync -avz --delete \
  --link-dest=../$LATEST \
  --exclude-from=~/backup-excludes.txt \
  /home/marco/ \
  $DEST/$TODAY/

# Update the "latest" symlink
ssh marco@nas.local "cd /volume1/backups/marco && rm -f latest && ln -s $TODAY latest"

The result after 30 days: 30 dated folders, each of which looks like a full backup, but which together take up little more than a single "latest copy" plus the accumulated differences.

4. Checking

To verify that the hard links are working:

ssh marco@nas.local "du -sh /volume1/backups/marco/2026-05-08"
ssh marco@nas.local "du -shc /volume1/backups/marco/*"

The total from the second command will NOT equal the sum of the individual ones — the overlap is shared.

5. Restoring

To restore the backup from a specific date:

rsync -avz marco@nas.local:/volume1/backups/marco/2026-05-08/ ~/restore-2026-05-08/

6. Scheduling

Add it to a systemd timer (or cron):

# /etc/systemd/system/backup-home.service
[Unit]
Description=Daily home directory backup
[Service]
Type=oneshot
User=marco
ExecStart=/home/marco/scripts/backup.sh

# /etc/systemd/system/backup-home.timer
[Unit]
Description=Daily backup
[Timer]
OnCalendar=*-*-* 04:00:00
Persistent=true
[Install]
WantedBy=timers.target

7. Cleaning up old snapshots

After a few months you will need to remove snapshots older than 90 days. Add this at the end of the script:

ssh marco@nas.local "cd /volume1/backups/marco && find . -maxdepth 1 -type d -mtime +90 -name '20*' -exec rm -rf {} +"
Be careful with --delete: it removes from the backup any file removed from the source. That is what you want for an "up-to-date mirror", not for "I want to recover a file I deleted 2 months ago". That is what the snapshots are for.