Skip to content
VDS · Backups and disk

Backups with rsync

How to set up file copying to another server over SSH, make a version history using hard links, and avoid deleting too much with the dangerous --delete option.

Updated August 23, 2026

rsync is the simplest working way to make copies: it's available everywhere, transfers only the changed parts of files, and works over SSH. For small datasets, that's enough.

Basic command

rsync -avz --delete \
  -e "ssh -i /root/.ssh/backup_key -p 22" \
  /var/www/ backup@backup.example.com:/srv/backup/vds/www/

Flag breakdown:

  • -a — preserve permissions, owners, times, and links;
  • -v — show what's happening;
  • -z — compress during transfer (useful on bandwidth up to 500 Mbps if there's a lot of data);
  • --delete — delete on the receiver what no longer exists on the source.

A trailing slash on the source path means “the contents of the directory”; without it, the directory itself is copied. /var/www/ and /var/www produce different results, and paired with --delete, that difference wipes the wrong thing. Always run with --dry-run first.

rsync -avzn --delete /var/www/ backup@backup.example.com:/srv/backup/vds/www/

The -n flag is the dry run.

Passwordless access key

Scheduled copying requires logging in without entering a password:

ssh-keygen -t ed25519 -f /root/.ssh/backup_key -N ""
ssh-copy-id -i /root/.ssh/backup_key.pub backup@backup.example.com

On the receiver, restrict this key to a single command — then a stolen key won't give a shell. In the receiver's ~/.ssh/authorized_keys:

command="rrsync -wo /srv/backup",restrict ssh-ed25519 AAAA... backup

Plain rsync keeps only the current state: if you deleted a file yesterday and noticed today, there's nowhere to restore it from. --link-dest gives you history: unchanged files aren't copied again but linked, so thirty copies take up little more than one.

#!/bin/bash
set -euo pipefail
DEST=/srv/backup/vds
TODAY=$(date +%F)
rsync -a --delete \
  --link-dest="$DEST/latest" \
  /var/www/ "$DEST/$TODAY/"
ln -sfn "$DEST/$TODAY" "$DEST/latest"
find "$DEST" -maxdepth 1 -type d -mtime +30 -name "20*" -exec rm -rf {} +

Run the script on a schedule at night, and always with logging.

Exclusions

rsync -avz --exclude-from=/root/backup-exclude.txt / backup@host:/srv/backup/vds/root/
/proc/
/sys/
/dev/
/run/
/tmp/
/var/lib/docker/
/var/cache/
*.log

Copying the entire root is rarely necessary: it's more useful to build an exact list from the article what exactly to back up.

When rsync isn't a good fit

  • If you need storage encryption and deduplication, use Restic.
  • If data changes constantly and integrity matters, dump the database first, then rsync the resulting file.
  • The receiver is someone else's storage you don't trust: rsync writes files as-is, with no client-side encryption.

Common mistakes

  • --delete with the wrong source path. An empty source directory wipes the receiver. A dry run is mandatory.
  • A copy on the same disk. That's not a backup, it's a second target for the same failure. See offsite copies.
  • Compression for already compressed data. -z only slows down archives and images.
  • The nightly run hits a disk bottleneck. The write throughput cap depends on the tier: see disk speed. --bwlimit=20000 and running during off-peak hours help.
  • Nobody checks the result. Log the return code and monitor it: rsync ... || echo "backup failed" | mail -s alert you@example.com.