banner mountains 2400x800

rsync from the ground up: local copies, then remote machines

rsync

Why rsync?

cp copies everything, every time. rsync looks at what is already at the other end and sends only the difference. That one idea makes it the tool I reach for whenever I copy a folder more than once: theme files to a web server, photos to a second disk, home folder backups to the NAS.

This article starts on a single machine, because everything rsync does locally it also does over the network. Once the local part makes sense, the remote part is just a different way of writing the destination.

The shape of every rsync command

rsync OPTIONS SOURCE DESTINATION

Source first, destination second, the same as cp. Most of the time you will use the same handful of options, and the rest are there for special cases.

Check it’s installed (on Fedora it usually is):

rsync --version

If not: sudo dnf install rsync on Fedora, sudo apt install rsync on Debian or Ubuntu. For remote copies, rsync needs to be installed on both machines.

Part 1: on one machine

Your first copy

rsync -av ~/Pictures/ /mnt/backup/Pictures/
  • -a (archive) copies folders recursively and keeps permissions, timestamps, symbolic links, owners and groups. It is shorthand for -rlptgoD, and it is almost always what you want.
  • -v (verbose) lists each file as it goes.

Run the same command again and it finishes almost straight away, because nothing has changed. That is rsync’s whole point.

The trailing slash: the one thing to remember

A slash at the end of the source changes what gets copied:

rsync -av Pictures/  /mnt/backup/Pictures/   # copies what's INSIDE Pictures
rsync -av Pictures   /mnt/backup/            # copies the folder itself -> /mnt/backup/Pictures/

Without the slash you get the folder; with it you get the contents. On the destination the slash makes no difference.

My rule of thumb: when syncing one folder to another, put a / on both ends and name both folders in full. It reads clearly and does what it says.

Look before you leap: dry runs

rsync -avn ~/Pictures/ /mnt/backup/Pictures/

-n (or --dry-run) shows exactly what rsync would do and changes nothing. Make it a habit for any command you haven’t run before, and treat it as compulsory once --delete is involved (more on that below).

For a more detailed report, add -i (--itemize-changes). Each line gets a short code: >f+++++++++ is a new file being sent, >f.st...... is a file whose size and time changed, cd+++++++++ is a new directory.

rsync -avni ~/Pictures/ /mnt/backup/Pictures/

Options for everyday use

OptionWhat it does
-hHuman-readable sizes (K, M, G)
-PShows progress and keeps half-sent files, so a big copy can resume. Same as --partial --progress
--info=progress2One overall progress line for the whole transfer, instead of one per file
-cCompare files by checksum, not by size and time. Slower but certain
-uSkip files that are newer at the destination
--ignore-existingNever touch a file that already exists at the destination
-H, -A, -XAlso keep hard links, ACLs and extended attributes. -a leaves these out

A big local copy between two disks, with readable progress:

rsync -ahP /mnt/disk1/videos/ /mnt/disk2/videos/

If it gets interrupted, run exactly the same command again and it carries on where it stopped.

Leaving things out

rsync -av --exclude '.git' --exclude '*.tmp' ~/Projects/mysite/ /mnt/backup/mysite/

--exclude takes a pattern. A few things worth knowing:

  • A pattern with no slash, like *.tmp, matches at any depth.
  • A pattern starting with / is anchored to the top of the source: --exclude '/cache' only leaves out cache at the top level.
  • A pattern ending in / only matches folders: --exclude 'node_modules/'.
  • For a long list, put one pattern per line in a file and use --exclude-from=excludes.txt.

You can also turn it around and copy only certain files. Rules are read in order and the first match wins, so the includes go first and a catch-all exclude goes last:

rsync -av --include '*/' --include '*.jpg' --exclude '*' ~/Pictures/ /mnt/backup/jpgs/

--include '*/' lets rsync walk into every folder, --include '*.jpg' keeps the photos and --exclude '*' drops everything else. Add -m (--prune-empty-dirs) to skip the folders that end up empty.

Making an exact mirror with –delete

By default rsync only adds and updates. If you delete a file from the source, the old copy stays at the destination. --delete changes that: it removes anything at the destination that isn’t in the source, so the two match exactly.

That is also how people lose files. Pointed at the wrong folder, or with a trailing slash missing, --delete will happily wipe things. So:

  1. Always dry-run first and read the deleting ... lines:
rsync -avn --delete ~/Documents/ /mnt/backup/Documents/
  1. Never point a --delete run at a folder that holds anything else. Give each mirror its own folder.
  2. Check the trailing slashes twice.
  3. Check the destination disk is actually mounted. If a backup disk isn’t mounted, /mnt/backup is just an empty folder on your main disk, and a later mirror from it would delete everything. A simple guard in a script:
mountpoint -q /mnt/backup || { echo "backup disk not mounted"; exit 1; }

A safety net worth adding: --backup --backup-dir moves anything that would be deleted or overwritten into a separate folder instead of throwing it away.

rsync -av --delete --backup --backup-dir=/mnt/backup/old-$(date +%F) \
  ~/Documents/ /mnt/backup/Documents/

Related options: --delete-excluded also removes excluded files from the destination, and --max-delete=50 stops rsync if it would delete more than 50 files, which catches the “wrong folder” mistake.

Moving instead of copying

rsync -av --remove-source-files ~/Downloads/videos/ /mnt/disk2/videos/

This deletes each source file once it has arrived safely. It leaves the empty folders behind.

Checking two copies are the same

rsync -avnc ~/Documents/ /mnt/backup/Documents/

A dry run with checksums. If it lists no files, the two sides are identical.

Part 2: other machines, over SSH

Same command, remote destination

To copy to another machine, write the remote side as host:path:

rsync -av ~/Projects/mysite/ server1:~/mysite/

rsync starts itself on the other machine over SSH and the two copies of rsync work out the difference between them. Only the changed parts of changed files cross the network.

To pull from a server, put the remote side first:

rsync -av server1:~/reports/scan.txt ~/

You can’t copy between two remote machines in one command. Log in to one of them, or copy through your own machine.

Use the names from ~/.ssh/config

server1 above isn’t a real DNS name. It’s a Host entry in ~/.ssh/config, which holds the address, port and username:

Host server1
    HostName 203.0.113.10
    User alice
    Port 2222

rsync reads that file through SSH, so the command stays short and the address, user and port are written down once. I’ve written up how I keep one config file on every machine in One ~/.ssh/config for every machine in the house.

Without a config entry you’d write it all out:

rsync -av -e "ssh -p 2222" ~/Projects/mysite/ alice@203.0.113.10:~/mysite/

-e sets the remote shell. It’s how you pass a port, a specific key (-e "ssh -i ~/.ssh/deploy_key") or any other SSH option.

Options that matter more over a network

OptionWhy
-zCompress while sending. Helps on slow links with text files; pointless for photos, video and zip files, which are already compressed
-PResume big files after a dropped connection
--bwlimit=5MCap the speed so a big upload doesn’t swamp your connection
--mkpathCreate missing parent folders on the destination (rsync 3.2.3 and newer)
--rsync-path="sudo rsync"Run rsync as root on the far end. Needs passwordless sudo for rsync there, so think twice

A real example: deploying a WordPress theme

This is how I push my site’s theme from my desktop to the web server:

rsync -avn --exclude .git ~/Projects/mysite-theme/ webhost:~/public_html/wp-content/themes/mysite/
rsync -av  --exclude .git ~/Projects/mysite-theme/ webhost:~/public_html/wp-content/themes/mysite/

The first line is the dry run, the second does it. --exclude .git keeps the git history off the web server, where it has no business being.

On a server where the web files belong to another user, rsync to your home folder first and copy into place with sudo cp, rather than giving rsync root on the far end.

rsync daemons: the double colon

Some machines, many NAS boxes among them, run rsync as a service of its own instead of going through SSH. You’ll recognise it by a double colon or an rsync:// address:

rsync -av ~/Documents/ nas::backups/Documents/
rsync -av --port=8730 ~/Documents/ rsync://nas/backups/Documents/

backups here is a “module” (a named share set up on the NAS), not a folder path. The daemon listens on port 873 unless it’s been moved. It doesn’t encrypt anything, so keep it to your home network and use SSH for anything that crosses the internet.

rsync or scp?

  • scp: a quick one-off copy of a file or two (scp notes.txt server1:~/).
  • rsync: folders, repeat copies, resuming, excludes, mirrors and dry runs.

Quick reference

rsync -av   SRC/ DEST/          # copy, keep everything, list files
rsync -avn  SRC/ DEST/          # dry run
rsync -ahP  SRC/ DEST/          # big copy, progress, resumable
rsync -avn --delete SRC/ DEST/  # preview a mirror (read the "deleting" lines)
rsync -avnc SRC/ DEST/          # are they identical?
rsync -av SRC/ host:path/       # push to a machine in ~/.ssh/config
rsync -av host:path/ DEST/      # pull from it

The full manual is in man rsync. It’s long, but the “USAGE” and “FILTER RULES” sections are worth a read once you’re comfortable with the basics.

Next in this series: snapshot-style backups with --link-dest, where every backup looks like a full copy but unchanged files take up no extra space.