Back up files to another server with rsync
Updated 27 Nov 2026 backup rsync
Make dated copies on a separate machine, using a restricted SSH key.
A backup on the same server is lost with the server. Send copies elsewhere.
Let the backup machine pull
Running the job on the backup machine means a compromised server cannot delete old copies.
#!/usr/bin/env bash
set -euo pipefail
base=/backups/web01
today=$base/$(date +%F)
mkdir -p "$base"
rsync -az --delete \
--link-dest="$base/latest" \
-e "ssh -i /root/.ssh/backup_key" \
backup@203.0.113.10:/var/www/ "$today/"
ln -sfn "$today" "$base/latest"
find "$base" -maxdepth 1 -type d -name '20*' -mtime +30 -exec rm -rf {} +
--link-dest stores unchanged files once, so daily copies stay small.
Give least access
The backup user needs read access only. In its authorized_keys, prefix the key with restrict and allow only what is needed.
Databases
Copying live database files is unreliable. Dump them first.