Part III · Shell and commands
28.7 rsync
alsorsyncsync folderscopy foldermirrorbackup
TL;DR
rsynccopies files and only transfers what changed: run it again and it finishes fast.-akeeps rights, times and symbolic links (owners too, as root),-vlists files,-zcompresses over the network,-nshows what would happen without doing it.- A trailing slash on the source means "what's inside":
src/copies the content of src,srccopies the folder itself.
rsync -av --dry-run src/ backup/
rsync -av src/ backup/
rsync -av --delete src/ backup/ # an exact mirror: removes extra files
rsync -avz photos/ alice@nas:/data/photos/
Pitfalls
⚠ --deleteremoves from the destination what isn't in the source: with a wrong path, it empties a whole folder. Always run it with-nfirst.- The order is source then destination, as with
cp: swapping them copies the old version over the new one. - Over SSH, rsync must be installed on both machines.
-adoesn't keep hard links, ACLs or extended attributes: add-H,-Aand-Xwhen they matter.
Deep dive 2 questions
How does rsync know what changed?
By default it skips files whose size and modification time match on both
sides; -c compares checksums instead, which is slower. For a file that
did change, over the network it sends only the parts that differ, found by
comparing checksums of blocks. Between two local folders, it simply copies
the whole file.
rsync, cp or scp?
cp for a one-time local copy. scp for a one-time copy to another
machine (since OpenSSH 9.0 it uses the SFTP protocol). rsync for anything
you will repeat, such as backups, and for big transfers that may be
interrupted: --partial keeps what was already sent.