On this pageMirror or versioned?
A plain rsync mirror has a flaw: it copies mistakes as faithfully as it copies files. Delete a directory by accident, or let ransomware encrypt it, and the next run makes the backup match. Versioned backups fix that. Each run produces a dated directory that looks like a complete copy, while files that did not change are stored only once, through hard links. This guide builds that with rsync over SSH, pulled by a separate backup server through a key that can only read.
Mirror or versioned?
Mirror (--delete into one folder) |
Versioned (--link-dest snapshots) |
|
|---|---|---|
| Restore yesterday's file after today's run | No | Yes |
Survives an accidental rm -rf |
Only until the next run | Yes |
| Disk use | One copy | One copy plus each day's changes |
| Browsing | One tree | One full tree per day |
Pull from a backup server
The usual setup pushes from the server to the backup storage, which means the production server holds credentials that can reach, and delete, its own backups. Reverse it. A separate backup server logs in to production and pulls the files. If production is compromised, nothing on it can touch the snapshots.
A storage-oriented plan in another region works well as that backup server: CloudSat's storage VPS plans start at $2.78/mo, and a different region keeps the copy away from the original.
Give the backup server a read-only key
On the backup server, create a key dedicated to this job. It has no passphrase because it runs unattended, which is why the next step limits what it can do:
sudo ssh-keygen -t ed25519 -N "" -f /root/.ssh/backup_web01 -C "pull backup web-01"
On the production server, add its public key to /root/.ssh/authorized_keys with restrictions in front of it, on a single line:
from="198.51.100.20",command="/usr/bin/rrsync -ro /",restrict ssh-ed25519 AAAAC3Nza... pull backup web-01
from=accepts the key only from the backup server's address.command="/usr/bin/rrsync -ro /"forces every login with this key through rrsync, a helper shipped with rsync that allows rsync transfers and nothing else, and-romakes them read-only: no writes, no deletes.restrictturns off port, agent and X11 forwarding and terminal allocation for this key.
Ubuntu 24.04 and recent Debian releases install rrsync as /usr/bin/rrsync; check with command -v rrsync elsewhere. Reading every file needs root, and if your sshd allows root logins only for keys like this one, set PermitRootLogin forced-commands-only on the production server.
The snapshot script
Save this on the backup server as /usr/local/sbin/backup-web01.sh and make it executable:
#!/bin/bash
set -euo pipefail
SRC="root@web-01.example.com"
DEST="/srv/backups/web-01"
TODAY="$(date +%F)"
KEEP=14
mkdir -p "$DEST"
[ -e "$DEST/$TODAY" ] && { echo "snapshot $TODAY already exists"; exit 0; }
LAST="$(ls -1d "$DEST"/20??-??-?? 2>/dev/null | tail -n 1 || true)"
rsync -aHAX --numeric-ids --delete --partial \
-e "ssh -i /root/.ssh/backup_web01 -o BatchMode=yes" \
--exclude-from=/etc/backup/web-01.exclude \
${LAST:+--link-dest="$LAST"} \
"$SRC:/" "$DEST/$TODAY.partial/"
mv "$DEST/$TODAY.partial" "$DEST/$TODAY"
ls -1d "$DEST"/20??-??-?? | head -n -"$KEEP" | xargs -r rm -rf
What the important options do:
-aHAXkeeps permissions, owners, timestamps and symlinks (-a), plus hard links, ACLs and extended attributes, which a system backup needs.--numeric-idsstores user and group numbers as they are, so files keep the right owners even though the backup server has different accounts.--link-destcompares each file with the newest finished snapshot. Unchanged files become hard links to it; only new and changed files are copied.- The
.partialdirectory is renamed only when rsync succeeds, so an interrupted run never looks like a finished snapshot, and the next run picks up where it stopped. - The last line keeps the newest 14 snapshots and deletes older ones. Run it with
echoin place ofxargs -r rm -rfthe first time. - If the nightly run competes with production traffic, add
--bwlimit=20Mto cap the transfer rate at about 20 MB per second.
Before the first real run, rehearse it. The same rsync line with -n and --itemize-changes lists what would be copied without writing anything, which is the fastest way to catch a wrong path or an exclude that does not match:
sudo rsync -aHAXn --numeric-ids --itemize-changes \
-e "ssh -i /root/.ssh/backup_web01 -o BatchMode=yes" \
--exclude-from=/etc/backup/web-01.exclude \
root@web-01.example.com:/ /srv/backups/web-01/dry-run/ | head -50
If this fails with a permissions error, check the from= address and the rrsync path in authorized_keys first.
The exclude file lists what has no place in a backup:
/proc/
/sys/
/dev/
/run/
/tmp/
/var/tmp/
/var/cache/
/mnt/
/media/
/lost+found/
/swapfile
Databases need one more step. Copying live database files can produce a copy that will not start, so dump them on the production server first, for example with pg_dump or mysqldump from a timer that runs before the backup, into a directory the backup includes.
How much space it really uses
du counts a hard-linked file once, so it shows what each snapshot added:
du -sh /srv/backups/web-01/*
The first snapshot is the full size; the following ones are the size of each day's changes. Deleting an old snapshot frees only the data that no other snapshot still links to. Two cases defeat the savings: a renamed or moved file is stored again, and a large file that changes a little every day, such as a virtual disk or a database file, is stored in full each time. For those, a deduplicating tool such as restic or Borg is the better fit.
Run it every night with a systemd timer
Create /etc/systemd/system/backup-web01.service:
[Unit]
Description=Pull backup of web-01
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/backup-web01.sh
Nice=10
IOSchedulingClass=idle
And /etc/systemd/system/backup-web01.timer:
[Unit]
Description=Nightly backup of web-01
[Timer]
OnCalendar=*-*-* 02:30
RandomizedDelaySec=20m
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now backup-web01.timer
systemctl list-timers backup-web01.timer
journalctl -u backup-web01.service -n 50
Persistent=true runs a missed backup as soon as the backup server is up again. Hook an alert to failures, or check the journal daily: a backup job that fails quietly for a month is a common way to discover there is no backup.
Restore, then test the restore
Every snapshot is an ordinary directory tree, so restoring is copying. The pull key is read-only by design, so restores use your normal admin access, staged in a temporary folder first:
sudo rsync -aHAX --numeric-ids /srv/backups/web-01/2026-11-17/etc/nginx/ admin@web-01.example.com:/tmp/nginx-restore/
Then move the files into place on the server and check them. Once a month, restore a few random files and a database dump to a scratch location, compare checksums with sha256sum, and start the database from the dump. A backup is only proven by a restore.
Keep rsync itself current on both machines: the 3.4.3 and 3.5.0 releases in 2026 were both security releases. Running it over SSH, as here, also avoids exposing an rsync daemon to the network.
FAQ
Are the backups encrypted?
In transit, yes: SSH encrypts the transfer. At rest, no. Put the snapshot directory on an encrypted disk on the backup server, or use restic if the storage is not under your control.
What if a run is interrupted?
The half-finished .partial directory stays, and the next run continues it. The previous snapshot is untouched, and the new one only gets its dated name after a complete run.
Can rsync write to an object storage bucket?
No. rsync needs an rsync process on both ends and a normal file system to create hard links in. For object storage, use a tool built for it, such as restic or rclone.
Can --delete damage older snapshots?
No. It only removes files inside the new snapshot directory that no longer exist on the source. Older snapshots are separate directories, and hard links mean deleting one name never removes the data another snapshot still uses.
Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.




