rsync (1/2)
rsync syncs files locally, over ssh or through a daemon and sends only what changed. Part 1 covers verbosity, the quick check, archive mode and the trailing-slash rule, backups, links, permissions, delta transfer, deletion, size limits and the remote shell.
146.1 What it does, where it runs
- Local, over ssh, or via an rsync daemon; only changed data travels.
- Linux: often preinstalled (apt/dnf/apk). macOS ships an old 2.6.9/openrsync: brew install rsync. Windows: WSL, Cygwin, cwRsync.
- Lab: client 192.168.166.10 -> server 192.168.166.20 (sshd :2222), rsync 3.4.3.
146.2 How it works over ssh
- Step 1
1.Local rsync starts ssh to the server (port 2222)
- Step 2
2.ssh starts the remote rsync in server mode (both sides need rsync)
- Step 3
3.Server sends its file list with size and mtime
- Step 4
4.Quick check: same size + mtime = skip
- Step 5
5.Only data of changed files (changed blocks) travels
146.3 Help, version and verbosity
- --version shows protocol 32 and capabilities; --help / -h print 185 lines.
- -a alone is silent; -v lists files and a summary; -vv explains reasons (is uptodate).
- --info=name1,stats1 gives the same output without -v; --info=progress2 shows total progress; --debug=FILTER shows matching rules.
- -q is silent on success but still prints errors; -n (dry run) changes nothing, not even creating the folder.
rsync --version | head -4rsync -a src/ d1/; echo rc=$?rsync -av src/ d2/rsync -avv src/ d2/rsync -a --info=name1,stats1 src/ d3/rsync -aq src/ d4/; echo rc=$?rsync -avn src/ d5/; ls d5$ rsync -avv src/ d2/sending incremental file listdelta-transmission disabled for local transfer or --whole-fileindex.html is uptodatecss/site.css is uptodatelogs/app.log is uptodatetotal: matches=0 hash_hits=0 false_alarms=0 data=0$ rsync -avn src/ d5/sending incremental file listcreated directory d5./index.htmlcss/site.csslogs/app.logtotal size is 26,925 speedup is 105.59 (DRY RUN)$ ls d5ls: d5: No such file or directory146.4 Quick check and comparison
- Quick check compares only size and modification time.
- Same size and mtime but different content: -a skips it, -c (checksum) fixes it.
- --size-only ignores time; -I ignores the quick check (copies all).
- -u skips files that are newer on the receiver; --existing never creates new files; --ignore-existing skips files that already exist.
- --modify-window=1 for FAT drives; --cc picks the checksum (xxh128, xxh3, md5...).
rsync -a src/ q/printf '<h1>HACK v1</h1>\n' > q/index.htmltouch -r src/index.html q/index.htmlrsync -av src/ q/; cat q/index.html # nothing transferredrsync -avc src/ q/; cat q/index.html # checksum finds it$ rsync -avc src/ q/; cat q/index.htmlsending incremental file listindex.html sent 284 bytes received 37 bytes 642.00 bytes/sec<h1>Home v1</h1>rsync -av --size-only src/ q/rsync -avI src/ q/rsync -avu src/ q/rsync -av --existing src/ q/rsync -av --ignore-existing src/ q/146.5 Archive mode and src/ vs src
- Step 1
1.rsync -a src dest/ (no slash)
- Step 2
2.The folder itself is copied: dest/src/...
- Step 3
3.rsync -a src/ dest/ (slash)
- Step 4
4.Only the content is copied: dest/...
- Step 5
5.Same rule on the remote side (host:/path/)
The most common rsync gotcha
- -a = -rlptgoD: recursive, links, perms, times, group, owner, devices.
- -r alone skips symlinks and times, so the next run copies everything again.
- -d copies only the top level; -R keeps the full path (use /./ to cut it); --mkpath creates missing destination folders.
- --no-OPTION turns off one part of -a; -x does not cross filesystems; -m prunes empty dirs.
rsync -a src s1/ # s1/src/...rsync -a src/ s2/ # s2/... (content only)E="ssh -p 2222 -i ~/.ssh/id_lab"B=root@192.168.166.20rsync -a -e "$E" src $B:/srv/noslash/rsync -a -e "$E" src/ $B:/srv/slash/[b]$ find /srv/noslash /srv/slash -maxdepth 2 | sort/srv/noslash/srv/noslash/src/srv/noslash/src/css/srv/noslash/src/index.html…/srv/slash/srv/slash/css/srv/slash/css/site.css/srv/slash/index.html…rsync -r src/ r1/ # no symlink, no timesrsync -av src/ r2/; rsync -av src/ r2/ # 2nd run copies nothingrsync -dv src/ dd/ # top level onlyrsync -avR src/./css/site.css rr2/rsync -a --mkpath src/css/ deep/new/path/146.6 Backups and update behaviour
- -b keeps the old file with a ~ suffix; --suffix=.bak changes it.
- --backup-dir puts replaced or deleted files into a (dated) folder.
- Default write: hidden temp file, then rename (inode changes). --inplace writes into the same file (inode stays): good for big VM images, but a failure leaves a half-written file.
- --append adds only new data at the end; --remove-source-files deletes the source after copy (a move).
rsync -avb src/ bk/ # index.html~rsync -avb --suffix=.bak src/ bk/rsync -av --delete --backup-dir=../old-2026-11-09 src/ bk/rsync -av -b --backup-dir=/srv/old -e "$E" src/ $B:/srv/site/$ rsync -a big.bin ip/ # default: temp file + rename$ stat -c 'inode %i' ip/big.bininode 170836$ rsync -a --inplace big.bin ip/$ stat -c 'inode %i' ip/big.bininode 170836[b] default: inode 170829 → 170839[b] --inplace: inode 170839 → 170839146.7 Symbolic and hard links
| Option | Effect |
|---|---|
| -r (no -l) | Symlinks skipped |
| -l | Symlinks copied as symlinks |
| -L | Real files copied instead of links |
| --copy-unsafe-links | Only links pointing outside become files |
| --safe-links | Links pointing outside are ignored |
| -H | Hard links preserved (-a alone splits them) |
| -k / -K | Dir symlink as real dir / keep receiver's dir links |
rsync -rv tree/ l0/ # links skippedrsync -rlv tree/ l1/ # links as linksrsync -rLv tree/ l2/ # real filesrsync -rlv --copy-unsafe-links tree/ l3/rsync -rlv --safe-links tree/ l4/rsync -aH --safe-links -e "$E" tree/ $B:/srv/links/$ rsync -rlv --safe-links tree/ l4/ignoring unsafe symlink "abs.lnk" -> "/etc/hostname"ignoring unsafe symlink "up.lnk" -> "../outside.txt"$ ls l4a.txt hard.txt in.lnk[b]$ ls -li /srv/links176199 -rw-r--r-- 2 root root 5 … a.txt176199 -rw-r--r-- 2 root root 5 … hard.txt176198 lrwxrwxrwx 1 root root 5 … in.lnk -> a.txt146.8 Permissions, ownership and attributes
- -r sets owner root and today's time; -p / -t keep mode / time; -a also keeps owner (when root).
- --chmod=D755,F644 (D folders, F files); --chown=user:group (root only); -E only the executable bit.
- -A copies ACLs and -X xattrs; -a alone drops both.
- --numeric-ids keeps uid/gid numbers (backup servers); --fake-super stores owner/perms in xattrs.
- -N (crtimes) is not supported in the lab build: real error below.
rsync -r perm/ p1/ # owner root, time todayrsync -rpt perm/ p2/ # mode + time keptrsync -a perm/ p3/ # + owner keptrsync -a --no-o --no-g perm/ p4/rsync -a --chmod=D755,F644 perm/ p5/rsync -a --chown=nobody:nobody perm/ p6/rsync -aAX perm/ p9/ # ACL + xattrsrsync -aN perm/ p10/$ rsync -aN perm/ p10/rsync: This rsync does not support --crtimes (-N)rsync error: syntax or usage error (code 1) at main.c(1797) [client=3.4.3]146.9 Transfer method and performance
- Step 1
1.Local copy: whole file by default (-W)
- Step 2
2.Network: server sends checksums of its old file's blocks
- Step 3
3.Client finds matching blocks
- Step 4
4.Client sends only changed blocks as literal data
- Step 5
5.Server rebuilds the file from old blocks + new data
- 8 bytes changed in a 2 MB file: local default sent 2 MB, --no-W ~1400 bytes; over ssh delta sent ~4 KB, -W the full file.
- --bwlimit=512 (KB/s) or 1m limits speed.
- --partial keeps an interrupted file; the next run reuses it as matched data.
- --link-dest hard-links unchanged files from the previous backup (cheap daily snapshots); --delay-updates swaps all files at the end; -S sparse.
# before each run: change 8 bytes of big.binrsync -a --stats big.bin w/ # local: whole file (2 MB literal)rsync -a --no-W --stats big.bin w/ # ~1400 bytesrsync -a --stats -e "$E" big.bin $B:/srv/ # delta: ~4 KBrsync -a -W --stats -e "$E" big.bin $B:/srv/$ rsync -a -W --stats -e "$E" big.bin $B:/srv/ | grep …Literal data: 2,097,152 bytesMatched data: 0 bytesTotal bytes sent: 2,097,760time rsync -a --bwlimit=512 big.bin bw2/time rsync -a --bwlimit=1m -e "$E" big.bin $B:/srv/bw/timeout -s INT 2 rsync -a --partial \ --bwlimit=256 -e "$E" big.bin $B:/srv/pp/rsync -a --partial --stats -e "$E" big.bin $B:/srv/pp/$ rsync -a --partial --stats -e "$E" big.bin $B:/srv/pp/Literal data: 1,901,152 bytesMatched data: 196,000 bytes146.10 Deletion and size limits
- --delete removes extra files on the destination; always dry-run first (-n).
- --max-delete=N stops after N deletions with exit code 25 (protects from a wrong big delete).
- Excluded files survive --delete unless you add --delete-excluded.
- Timing: --delete-before / -during (default) / -delay / -after.
- --max-size=1m / --min-size=10k skip files by size (k, m = 1024 units).
rsync -avn --delete src/ dst/rsync -av --delete --max-delete=2 src/ dst/echo rc=$?rsync -av --delete --delete-excluded --exclude='*.tmp' src/ dst/rsync -avn --delete -e "$E" src/ $B:/srv/site/$ rsync -av --delete --max-delete=2 src/ dst/deleting x5deleting x4Deletions stopped due to --max-delete limit (4 skipped)rsync error: the --max-delete limit stopped deletions (code 25)rc=25$ rsync -avn --delete -e "$E" src/ $B:/srv/site/deleting old.htmldeleting new.txtdeleting css/old.csstotal size is 26,937 speedup is 93.53 (DRY RUN)rsync -av --max-size=1m sz/ m1/rsync -av --min-size=10k sz/ m2/rsync -av --min-size=10k --max-size=1m -e "$E" sz/ $B:/srv/sz/146.11 Remote shell and connection
- Step 1
1.No -e: rsync connects to port 22, no sshd: Connection refused (rc 255)
- Step 2
2.-e 'ssh -p 2222 -i key': connection works
- Step 3
3.Wrong --rsync-path on the server: rc 12
- Step 4
4.sshd stopped on server: connection unexpectedly closed
- Step 5
5.--port / --contimeout: only for an rsync daemon
- -e sets the remote shell (port, key); --rsync-path tells where the remote rsync is.
- host:path pulls when it is the source.
- --timeout closes an idle connection; --stop-after / --stop-at halt the copy at a time limit; --iconv converts filename charset.
rsync -a src/ $B:/srv/x/ # port 22: Connection refused, rc=255rsync -a -e 'ssh -p 2222 -i ~/.ssh/id_lab' src/ $B:/srv/x/rsync -a -e "$E" --rsync-path=/opt/rsync/bin/rsync \ src/ $B:/srv/x/; echo rc=$? # wrong path: rc=12rsync -a -e "$E" $B:/srv/site/ pulled/ # pullrsync -a --timeout=30 --contimeout=5 -e "$E" src/ $B:/srv/x/$ rsync -a --timeout=30 --contimeout=5 -e "$E" src/ $B:/srv/x/The --contimeout option may only be used when connecting to an rsync daemon.rsync error: syntax or usage error (code 1) at main.c(1561)146.12 Comparison: rsync vs scp
| Point | rsync | scp |
|---|---|---|
| Unchanged files | Skipped (quick check) | Copied again |
| Changed files | Only changed blocks (delta) | Whole file |
| Resume | --partial | No |
| Mirror | --delete | No |
| Bandwidth | --bwlimit | -l |
146.13 Common mistakes
- Forgetting the trailing slash rule: src vs src/ gives a nested folder.
- Thinking -a copies everything: no hard links, ACLs or xattrs; use -aAXH --numeric-ids for full backups.
- Running --delete without -n and --max-delete first.
- Expecting -a to catch same-size, same-mtime changes: use -c.
- Forgetting -e 'ssh -p PORT' when sshd is not on 22.
146.14 Interview tips
- rsync beats scp: quick check skips unchanged files, delta sends only changed blocks, plus --partial, --delete, --bwlimit.
- Same size + mtime but changed content: rsync -a skips it; -c compares checksums.
- src/ copies content, src copies the folder itself.
- Full system backup: rsync -aAXH --numeric-ids, dry run first.