How-to

Rsync over SSH, Done Right

Rsync sends only what changed, and SSH carries it safely. Together they are the workhorse for migrations, deploys and backups between machines.

Why use rsync instead of scp?

Because rsync compares source and target and transfers only the differences. The first run copies everything; later runs send just new and changed files, and for large changed files only the changed blocks. It also preserves permissions, ownership, timestamps and links when asked, can delete files that vanished from the source, and resumes cleanly after an interruption. Since version 3, rsync uses SSH as its transport by default whenever the target looks like user@host:path, so no daemon or extra port is needed.

What does a basic rsync over SSH look like?

rsync -aHz --info=progress2 /srv/www/ deploy@203.0.113.40:/srv/www/
  • -a archive mode: recursive, and keeps permissions, times, symlinks, owner and group.
  • -H keeps hard links, which matters for some backup trees.
  • -z compresses in transit; skip it for already-compressed media or on fast links.
  • --info=progress2 shows one overall progress line instead of a line per file.

Install rsync on both machines first: apt install rsync, dnf install rsync or pkg install rsync on FreeBSD.

Why does the trailing slash matter?

A trailing slash on the source means "the contents of this folder"; no slash means "this folder itself". The difference decides where files land:

Command sourceResult on target /backup/
/srv/www//backup/index.html, /backup/img/ ...
/srv/www/backup/www/index.html, /backup/www/img/ ...

The slash on the target makes no difference. When in doubt, preview with a dry run.

How do I preview before changing anything?

rsync -aHn --delete --itemize-changes /srv/www/ deploy@203.0.113.40:/srv/www/ | head -50

-n is a dry run; nothing is written. --itemize-changes prints a code per file: >f+++++++++ for a new file, >f.st...... for size and time changes, *deleting for removals. Always dry-run any command that includes --delete, since a wrong source path can empty the target.

How do I use a custom port or a dedicated key?

Pass SSH options with -e, or put them in ~/.ssh/config and use the alias:

rsync -a -e 'ssh -p 2207 -i /root/.ssh/mirror_ed25519 -o IdentitiesOnly=yes' \
  /var/lib/media/ mirror@198.51.100.12:/tank/media/

For scheduled jobs, give the key no passphrase but restrict it on the target. In the target user's authorized_keys, prefix the key with restrict,from="203.0.113.40" so it only works from the source machine. The rrsync helper script, packaged with rsync, can lock the key to one folder as well.

How do I copy files owned by root on the far side?

Log in as a normal user and run the remote rsync under sudo:

rsync -a --rsync-path='sudo rsync' deploy@203.0.113.40:/etc/letsencrypt/ ./le-backup/

The remote user needs a sudoers entry that allows /usr/bin/rsync without a password, for example deploy ALL=(root) NOPASSWD: /usr/bin/rsync in a file under /etc/sudoers.d/, checked with visudo -cf.

How do I mirror a whole server during a migration?

Run rsync twice: once while the old machine is live, then again after stopping services, so the second pass only copies the last changes. Exclude virtual and machine-specific paths:

rsync -aHAX --numeric-ids --delete \
  --exclude={/dev/*,/proc/*,/sys/*,/run/*,/tmp/*,/mnt/*,/boot/*,/etc/fstab,/etc/netplan/*,/etc/ssh/ssh_host_*} \
  root@old.example.net:/ /mnt/newroot/

-A and -X keep ACLs and extended attributes, including SELinux labels. Moving application data this way is common; moving whole operating systems is riskier, and installing a fresh image on the new box and copying only data folders is usually cleaner.

How do I avoid flooding the link?

--bwlimit=40M caps the transfer at about 40 MB per second, leaving room for production traffic. Every machine with a 10 or 25 Gbps port can move data far faster than that, but a backup job should not starve live users. Remember that data leaving a machine counts toward its 15 TB monthly allowance (5 TB in Singapore), while data arriving is free, so pulling backups onto a storage box costs that box nothing.

Machines Often Used as Rsync Targets

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
S3Intel Xeon E-2356G6 / 1264 GB DDR42x 1 TB NVMe2x 10 Gbps$80.38$80.38 (1 month)
D202x Intel Xeon Gold 6336Y48 / 96256 GB DDR44x 4 TB NVMe2x 25 Gbps$990.36$990.36 (1 month)
A2Ampere Altra Q80-3080 / 80256 GB DDR42x 4 TB NVMe2x 25 Gbps$277.57$277.57 (1 month)

Keep Reading

The SSH config file

Turn long ssh command lines into short aliases with ~/.ssh/config: hosts, ports, keys, jump hosts, keepalives and connection sharing.

Open

SFTP vs SSH

SFTP is a file transfer subsystem that rides inside SSH; learn when to hand someone SFTP only and how to jail them.

Open

Storage servers

From 8 TB mirrors to a six-drive NVMe array of more than 90 TB raw, with ZFS or mdadm laid out by you.

Open

Outbound traffic allowance

How the 15 TB monthly egress allowance works, what it means per day and per second, and why inbound data costs nothing.

Open

FAQ

Quick Answers

Yes. When the source or target is written as user@host:path, rsync connects through SSH.

A slash on the source copies the folder's contents; without it the folder itself is created inside the target.

Use -e 'ssh -p 2207' or define the port in a Host block of ~/.ssh/config and use that alias.

Yes, after a dry run with -n and --itemize-changes shows exactly what would be removed.

No. Pulling data in is free in every city; the meter only counts bytes your machine sends out.