Catalog / Rsync Cheatsheet

Rsync Cheatsheet

A comprehensive cheat sheet for using Rsync, covering essential options, examples, and use cases for efficient file synchronization and backup.

Rsync Basics

Basic Syntax

rsync [options] source destination

Example:
rsync -avz /path/to/source/ user@host:/path/to/destination/

Essential Options

a

Archive mode; preserves permissions, ownership, timestamps, etc.

v

Verbose mode; increases the amount of information displayed during the transfer.

z

Compress file data during the transfer.

r

Recursively copy directories and files.

t

Preserve modification times.

o

Preserve owner.

g

Preserve group.

p

Preserve permissions.

Basic Examples

Copy a file to a remote server:
rsync myfile.txt user@host:/path/to/destination/

Copy a directory recursively to a remote server:
rsync -r /local/directory/ user@host:/remote/directory/

Synchronize two directories:
rsync -avz /source/directory/ /destination/directory/

Advanced Options

Deletion Options

--delete

Delete extraneous files from the destination directory.

--delete-before

Deletion happens before transfer.

--delete-after

Deletion happens after transfer.

--delete-during

Deletion happens during transfer.

--delete-excluded

Also delete excluded files from destination.

Transfer Options

--progress

Show progress during transfer.

--partial

Keep partially transferred files if the transfer is interrupted.

--checksum

Skip files based on checksum, not modification time and size.

--ignore-existing

Skip updating files that exist on destination.

--remove-source-files

Remove source files after successful transfer.

--max-size=SIZE

Don’t transfer any file larger than SIZE.

Filtering Options

--exclude='pattern'

Exclude files matching pattern.

--include='pattern'

Include files matching pattern.

--exclude-from=FILE

Read exclude patterns from FILE.

--include-from=FILE

Read include patterns from FILE.

Security and Remote Transfers

SSH Options

-e 'ssh -p port'

Specify a different SSH port.

--rsh='ssh -l user'

Specify a remote shell program.

--rsync-path=PATH

Specify where rsync is installed on the remote machine.

Using SSH Keys

Ensure SSH keys are set up for passwordless authentication to avoid interactive prompts.

Use ssh-keygen to generate keys and ssh-copy-id to copy them to the remote server.

Security Considerations

Always use secure protocols like SSH for remote transfers.
Avoid using rsync over unencrypted connections, especially for sensitive data.

Real-world examples

Backup

Incremental backup of a directory to an external drive:
rsync -av --delete /home/user/documents/ /mnt/backup/documents/

Daily incremental backup:
rsync -av --link-dest=/mnt/backup/yesterday /home/user/ /mnt/backup/today

Synchronization

Sync a website to web server:
rsync -az -e "ssh -i /path/to/key" /local/website user@host:/var/www/website

Mirroring

Create a mirror of a website:
rsync -avz --delete /path/to/source/ user@host:/path/to/destination/