Skip to content

Linux Foundations / Follow traffic and find the fault ยท Step 36 of 46

Lesson: rsyncSwitch
โ† Linux Foundations

Network operations

rsync

Copy and synchronise files efficiently, locally or over SSH, with rsync.

rsync copies files and directories, and it is smart about repeat runs. It skips files that already match and can send only the changed parts of files over a network. That makes it a favourite for backups.

Basic use

$ rsync -av project/ backup/
sending incremental file list
./
notes.txt

sent 183 bytes  received 38 bytes  442.00 bytes/sec
total size is 24  speedup is 0.11

-v is verbose. -a is archive mode: it copies recursively and preserves symlinks, permissions, times, group, owner and device files. Keeping owner generally needs root. It does not include hard links, ACLs or extended attributes.

By default rsync decides a file is unchanged when its size and modification time match.

The trailing slash

This detail matters:

  • rsync -a project/ backup/ copies the contents of project into backup.
  • rsync -a project backup/ creates backup/project.

The slash is significant on the source.

Preview and delete

$ rsync -avn --delete project/ backup/

-n (--dry-run) shows what would happen without changing anything. --delete removes files in the destination that no longer exist in the source. A wrong path with --delete can destroy data, so always dry run first.

Over the network

$ rsync -avz project/ [email protected]:backup/
$ rsync -avz [email protected]:backup/ restored/

The first pushes, the second pulls. With a single colon, rsync normally connects through SSH, and rsync must be installed on both machines. -z compresses data in transit, which helps on slow links.

For a different SSH port:

$ rsync -av -e "ssh -p 2222" project/ [email protected]:backup/

Excluding files

$ rsync -av --exclude '*.log' project/ backup/

Quote the pattern so your shell does not expand it first.

A synchronised copy is not a full backup history: a mistake in the source is copied too.

Try it

  1. Create a directory project with two files.
  2. Run rsync -av project/ backup/ twice and compare the output.
  3. Delete a source file, then run with -avn --delete and read what would be removed.

Check your understanding

rsync

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.