Backups with rsync
rsync copies files and directory trees, locally or over SSH, and only transfers what changed. That makes it the workhorse of simple server backups, migrations and deployments. ssh & scp used it once; here you learn the options that decide whether a backup is correct, and how to keep many daily snapshots for the price of one.
The trailing slash, and -a
mkdir -p site/css && echo "<h1>v1</h1>" > site/index.html && echo "body{}" > site/css/app.css
rsync -a site copy1/ # without slash: copies the directory itself
rsync -a site/ copy2/ # with slash: copies the directory's contents
find copy1 copy2 | sortcopy1
copy1/site
copy1/site/css
copy1/site/css/app.css
copy1/site/index.html
copy2
copy2/css
copy2/css/app.css
copy2/index.html
site means "this directory", site/ means "what is inside it". Most rsync accidents come from that one character, so always test with --dry-run (-n) first.
-a (archive) is -rlptgoD: recursive, keep symlinks, permissions, times, group, owner (when run as root) and device files. It does not include ACLs (-A), extended attributes (-X) or hard links (-H). For a server backup use at least -aAXH, plus --numeric-ids when the target machine has different user names, so file ownership is restored by UID, not by a name that may map to someone else.
Mirroring safely: --delete and --itemize-changes
A mirror must also remove files that were deleted at the source; --delete does that, which is exactly why it deserves a dry run. -i (--itemize-changes) prints one line per change, with a code saying what changed:
mkdir -p site/css && echo "<h1>v1</h1>" > site/index.html && echo "body{}" > site/css/app.css
echo "debug" > site/debug.log
touch -d "2026-01-15 10:00" site/index.html
rsync -a site/ backup/ # first full copy
echo "<h1>v2</h1>" > site/index.html # change one file (same size)...
touch -d "2026-01-15 10:00" site/index.html # ...and, as some tools do, keep its mtime
echo "news" > site/news.html # add a file
rm site/css/app.css # delete another
echo "--- dry run:"
rsync -a --delete --dry-run -i --exclude='*.log' site/ backup/
echo "--- for real:"
rsync -a --delete -i --exclude='*.log' site/ backup/
find backup -type f | sort
cat backup/index.html
echo "--- with --checksum:"
rsync -a -c --delete -i --exclude='*.log' site/ backup/
cat backup/index.html--- dry run:
>f+++++++++ news.html
*deleting css/app.css
--- for real:
>f+++++++++ news.html
*deleting css/app.css
backup/debug.log
backup/index.html
backup/news.html
<h1>v1</h1>
--- with --checksum:
>fc........ index.html
<h1>v2</h1>
Read the codes: >f+++++++++ a new file is received, *deleting a deletion, and in other lines the letters c/s/t say whether checksum, size or time differ. Three things to notice:
debug.logstayed in the backup: excluded files are also protected from deletion (add--delete-excludedto remove them too).index.htmlwas not updated by the normal run. By default rsync decides "unchanged" from size and modification time alone; the new version had the same size and someone (an editor,cp -p, an extraction tool) kept the old timestamp. That shortcut is fast and nearly always right.-c(--checksum) reads every file on both sides and compares contents, so it caught the change. It costs a full read of both trees: use it for verification runs, not every hour.
Many snapshots for the price of one: --link-dest
A single mirror protects you against a dead disk, but not against "someone deleted a file last Tuesday" or ransomware that encrypted everything before last night's sync. You want dated snapshots, and --link-dest makes them cheap: files unchanged since the previous snapshot become hard links to it (no new space), only changed files are stored again. Every snapshot still looks like a complete copy.
mkdir -p site && for f in a b c; do head -c 1M /dev/zero > site/$f.bin; done
mkdir -p snapshots
rsync -a site/ snapshots/2026-01-14/
echo "changed" >> site/b.bin # one file changes overnight
rsync -a --link-dest=../2026-01-14 site/ snapshots/2026-01-15/
echo "--- link counts (2 = shared between both snapshots):"
stat -c '%h %n' snapshots/*/*.bin
echo "--- space used:"
du -sh snapshots/2026-01-14 snapshots/2026-01-15--- link counts (2 = shared between both snapshots):
2 snapshots/2026-01-14/a.bin
1 snapshots/2026-01-14/b.bin
2 snapshots/2026-01-14/c.bin
2 snapshots/2026-01-15/a.bin
1 snapshots/2026-01-15/b.bin
2 snapshots/2026-01-15/c.bin
--- space used:
3.1M snapshots/2026-01-14
1.1M snapshots/2026-01-15
du counts a hard-linked file only once per run, so the second snapshot costs about one changed megabyte. Deleting an old snapshot (rm -rf snapshots/2026-01-14) frees only the files no other snapshot links to. The --link-dest path is relative to the destination directory, hence ../2026-01-14.
Over SSH, from a cron-free timer
On real servers rsync runs over SSH (sample session; needs two hosts):
$ rsync -aAXH --numeric-ids --delete -e "ssh -i /root/.ssh/backup_ed25519" \
--link-dest=../latest /srv/data/ backup@vault1:/backups/web1/2026-01-15/
$ ssh backup@vault1 'ln -sfn 2026-01-15 /backups/web1/latest'- Run it from a systemd timer (
Type=oneshot), so failures show up insystemctl --failedand the journal. - Give the backup key
restrictand afrom=on the receiving side, ideally a forcedcommand=(SSH Keys);rrsync(shipped with rsync) restricts a key to rsync into one directory. - Better still, pull from the backup server, so a compromised web server holds no credentials to the backups.
- Add
--bwlimit=20Mon busy links, and--partialfor large files over flaky links. - Exit codes matter:
0success,23/24partial transfer (some files failed or vanished during the run). Alert on anything but 0 and 24.
rsync copies files. It is not enough on its own for running databases (copy a database dump or a filesystem snapshot instead, see Growing Volumes & LVM Snapshots), and it doesn't encrypt or deduplicate on the target; tools such as restic and BorgBackup add encryption, deduplication and retention policies on top of the same idea. What every approach needs is in the next lesson: Backup Strategy & Restore Drills.
Test yourself
What is the difference between rsync -a src dst/ and rsync -a src/ dst/?
- None
- The first creates dst/src/…, the second copies the contents of src directly into dst/
- The second deletes extra files
- The first is recursive, the second isn't
How does --link-dest save space?
- It compresses files
- Files unchanged since the reference snapshot are hard-linked to it instead of copied
- It skips large files
- It deletes old snapshots
Which options does a server backup need beyond -a to keep ACLs, extended attributes and hard links?
- -z
- -AXH
- --delete
- -c