How-to

Rsync Exclude Rules That Do What You Mean

Excludes keep caches, logs and junk out of copies and backups. They are simple once you know how rsync matches a pattern against a path.

What is the simplest way to exclude something?

Add one --exclude per pattern. The pattern is matched against each file and folder name as rsync walks the tree:

rsync -a --exclude='node_modules' --exclude='*.log' --exclude='.cache' /home/ana/projects/ /mnt/nvme1/projects/

Quote every pattern so your shell does not expand the wildcard before rsync sees it. Excluding a folder also skips everything below it, which is usually what you want for dependency folders and caches.

How does rsync match a pattern?

The rules are few but important:

PatternMatches
tmpAny file or folder named tmp at any depth
tmp/Only folders named tmp, at any depth
/tmptmp at the top of the transfer only
*.isoFiles ending in .iso anywhere
cache/*Contents of any cache folder, keeping the empty folder
logs/**/*.gzCompressed files at any depth below any logs folder

A leading slash anchors the pattern to the root of the transfer, not to the root of the file system. When you copy /srv/site/, the pattern /uploads means /srv/site/uploads. That point causes more confusion than anything else about excludes.

How do I keep a long list of excludes?

Put patterns in a file, one per line, and point rsync at it. Lines starting with # are comments:

# /etc/backup/web.exclude
/var/cache/
/var/tmp/
/var/lib/php/sessions/
*.swp
.git/objects/pack/tmp_*
/srv/site/storage/framework/cache/
rsync -a --exclude-from=/etc/backup/web.exclude / vault@198.51.100.12:/backup/web/

Keeping the list in a file makes it reviewable, versionable and shared between jobs.

How do include and exclude work together?

Rsync checks rules in the order given, and the first matching rule wins. To keep only some things, list the includes first and a broad exclude last. Folders need their own include, or rsync never descends into them:

rsync -a --include='*/' --include='*.conf' --include='*.yaml' --exclude='*' --prune-empty-dirs /etc/ /mnt/nvme1/etc-configs/

--prune-empty-dirs, short form -m, drops folders that end up empty, so the target holds only the matching files and the folders they live in.

What are filter rules?

The general form that --include and --exclude are shortcuts for. A --filter rule starts with a symbol: - excludes, + includes, and : reads per-folder rule files during the walk. Per-folder files let each project carry its own excludes, much like a .gitignore:

rsync -a --filter=':- .rsync-skip' /srv/projects/ /mnt/nvme1/projects/

Any folder that contains a .rsync-skip file adds its patterns for itself and everything below it. The -F shortcut does the same with files named .rsync-filter.

What happens to excluded files on the target with --delete?

They are left alone. Excluded paths are protected from deletion by default, so a mirror with --delete keeps old cache folders on the target if you exclude them now. To clear them as well, add --delete-excluded. Dry-run that combination first; it can remove more than you expect:

rsync -an --delete --delete-excluded -i --exclude-from=/etc/backup/web.exclude /srv/site/ /srv/site-mirror/ | grep '^\*deleting'

How do I check which rule caught a file?

Ask rsync to explain itself. --debug=FILTER prints a line for each file hidden or shown and the rule responsible:

rsync -an --debug=FILTER --exclude-from=/etc/backup/web.exclude /srv/site/ /tmp/probe/ 2>&1 | grep -m 20 hiding

What should a server backup usually exclude?

  • Virtual file systems: /proc, /sys, /dev, /run.
  • Temporary data: /tmp, /var/tmp, package caches under /var/cache/apt or /var/cache/dnf.
  • Rebuildable folders: node_modules, vendor if locked by a lock file, build output.
  • Raw database files, which should be dumped instead, as explained in Back up data with rsync.
  • Swap files and large VM images that a hypervisor backs up separately.

Smaller transfers mean shorter backup windows and less outgoing traffic from production, which counts toward its 15 TB monthly allowance.

Keep Reading

Rsync command examples

Twelve rsync patterns for daily admin work: local copies, checksums, partial resumes, size limits, backups of replaced files and safe deletes.

Open

Back up data with rsync

Build dated, space-saving snapshot backups with rsync and hard links, pull them from a second machine and test restores.

Open

Rsync over SSH

Copy and mirror folders between machines with rsync over SSH: trailing slashes, dry runs, custom ports, sudo on the far side and bandwidth caps.

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

Use --exclude='name/' to skip every folder with that name, or --exclude='/name' to skip it only at the top of the transfer.

Relative to the source folder of the transfer. A leading slash anchors to that folder, not to the file system root.

List them in a file and pass --exclude-from with its path.

The first matching rule wins. Put includes before the broad exclude, and include */ so rsync enters folders.

No. Excluded paths are protected unless you also pass --delete-excluded.