DEV Community

Cover image for rsync --link-dest: The Backup Trick You Are Probably Missing
Schiff Heimlich
Schiff Heimlich

Posted on

rsync --link-dest: The Backup Trick You Are Probably Missing

rsync --link-dest: The Backup Trick You Are Probably Missing

Most people know rsync for copying files. It is fast, it is reliable, and it only transfers the parts that changed. But there is a flag that turns it into something much more powerful for backups, and almost nobody talks about it.

The Problem With Incremental Backups

You have a directory. You back it up daily. Most of the files do not change between runs, but you still copy everything every time because you want each backup to be a complete, standalone snapshot.

The obvious approach is to just rsync -a into dated folders. That works, but you waste a lot of disk. If you have 100GB of data and back up daily, after a month you are storing 3TB even though 95% of it is identical across runs.

You could use deduplication tools. You could use something like Borg or Restic. Those are great, but they require you to learn their format, their commands, their restore process. Sometimes you just want plain files you can browse with ls.

What --link-dest Does

--link-dest tells rsync: "Compare the files you are about to copy against this other directory. If a file is identical to one in that directory, hard-link it instead of copying it."

rsync -a --link-dest=/backups/previous /source/ /backups/current/
Enter fullscreen mode Exit fullscreen mode

That is it. If a file has not changed, it becomes a hard link to the previous version. If it has changed, you get a fresh copy. The result is a full, browsable backup directory that looks complete, but only uses disk for the files that actually changed.

Why This Matters

Hard links are cheap. They are just multiple directory entries pointing to the same inode. Deleting one does not affect the others. So each backup looks and behaves like a full copy, but you only pay for the delta.

You can browse any backup with normal tools. You can restore by just copying files back. You do not need special software to read the archive. It is just files on disk.

A Practical Setup

Here is a minimal daily backup script:

#!/bin/bash
SOURCE="/data"
BACKUP_ROOT="/backups"
TODAY=$(date +%Y-%m-%d)
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)

mkdir -p "$BACKUP_ROOT/$TODAY"

if [ -d "$BACKUP_ROOT/$YESTERDAY" ]; then
  rsync -a --link-dest="$BACKUP_ROOT/$YESTERDAY" "$SOURCE/" "$BACKUP_ROOT/$TODAY/"
else
  rsync -a "$SOURCE/" "$BACKUP_ROOT/$TODAY/"
fi
Enter fullscreen mode Exit fullscreen mode

First run does a full copy. Every run after that hard-links unchanged files and copies only what is new. You get daily snapshots, each one looks complete, and disk usage stays low.

Things to Watch

  • --link-dest requires the reference directory to exist on the same filesystem. Hard links cannot cross mount points.
  • If you delete the "previous" backup, you do not lose data, because the hard links in other backups still point to the inodes. But you do lose the ability to compare against it for future runs.
  • This works best for files that do not change often. If everything changes every day, you are back to full copies.

When to Use This

This is good for:

  • Daily server backups where you want plain-file snapshots
  • Configuration directories that rarely change
  • Any situation where you want deduplication without a special backup format

This is not ideal for:

  • Offsite backups (hard links do not help over the network)
  • Very large, frequently changing datasets (databases, logs)

Summary

rsync --link-dest gives you space-efficient, full-directory backups using nothing but hard links and a standard tool that is already installed everywhere. It is not fancy, but it works, and you do not need to learn a new system to use it.

Top comments (0)