WORLDBOOK

rsync | Worldbooks | WebMCP | Search | Submit

rsync

Category: Unknown Author: Unknown Version: 1.0.0 Updated: Unknown
0

rsync

Website: https://rsync.samba.org CLI Tool: rsync Authentication: SSH keys, passwords

Description

rsync is a fast and versatile file copying tool for local and remote file synchronization. It uses an efficient delta-transfer algorithm that only sends differences between files. Essential for backups, deployments, and file synchronization across systems.

Commands

Basic Usage

Local Copy

rsync source destination
rsync file.txt /backup/
rsync -a /source/ /destination/

Copy files locally (similar to cp).

Remote Copy

rsync source user@host:destination
rsync user@host:source destination
rsync file.txt user@example.com:/backup/

Copy files to/from remote host.

Sync Directories

rsync -av source/ destination/
rsync -av /data/ user@host:/backup/data/

Synchronize directories with archive mode.

Common Options

Archive Mode

rsync -a source/ destination/
rsync --archive source/ destination/

Archive mode: preserves permissions, times, symlinks (-rlptgoD).

Verbose Output

rsync -v source destination
rsync -av source/ destination/
rsync -avv source/ destination/

Verbose output (use -vv for more details).

Progress Display

rsync -av --progress source/ destination/
rsync -avP source/ destination/

Show progress during transfer.

Dry Run

rsync -avn source/ destination/
rsync -av --dry-run source/ destination/

Show what would be transferred without doing it.

Compression

rsync -avz source/ user@host:destination/
rsync -av --compress source/ destination/

Compress data during transfer.

Delete

rsync -av --delete source/ destination/
rsync -av --delete-after source/ destination/

Delete files in destination not in source.

File Selection

Include Patterns

rsync -av --include="*.txt" --exclude="*" source/ destination/
rsync -av --include="*.jpg" --include="*.png" --exclude="*" source/ destination/

Include only matching files.

Exclude Patterns

rsync -av --exclude="*.log" source/ destination/
rsync -av --exclude="node_modules/" source/ destination/
rsync -av --exclude={.git,dist,*.tmp} source/ destination/

Exclude matching files or directories.

Exclude File

rsync -av --exclude-from=exclude.txt source/ destination/
cat exclude.txt:
  *.log
  .git/
  node_modules/

Exclude patterns from file.

Include File

rsync -av --include-from=include.txt --exclude="*" source/ destination/

Include patterns from file.

Filter Rules

rsync -av --filter="- *.log" --filter="- .git/" source/ destination/
rsync -av -F source/ destination/

Use filter rules (advanced).

Permissions and Ownership

Preserve Permissions

rsync -p source/ destination/
rsync --perms source/ destination/

Preserve file permissions (included in -a).

Preserve Owner/Group

rsync -og source/ destination/
rsync --owner --group source/ destination/

Preserve ownership (included in -a).

Update Permissions Only

rsync -av --chmod=D755,F644 source/ destination/

Set permissions during sync.

No Permissions

rsync -av --no-perms --no-owner --no-group source/ destination/

Don't preserve permissions/ownership.

Transfer Options

Bandwidth Limit

rsync -av --bwlimit=1000 source/ destination/

Limit bandwidth to KB/s.

Partial Transfer

rsync -av --partial source/ destination/
rsync -avP source/ destination/

Keep partially transferred files (-P = --partial --progress).

Timeout

rsync -av --timeout=300 source/ user@host:destination/

Set I/O timeout in seconds.

Block Size

rsync -av --block-size=8192 source/ destination/

Set block size for delta algorithm.

Backup Options

Backup Files

rsync -av --backup source/ destination/
rsync -av --backup --suffix=.bak source/ destination/

Backup files before overwriting.

Backup Directory

rsync -av --backup --backup-dir=/backup/old source/ destination/

Store backups in separate directory.

Incremental Backup

rsync -av --link-dest=/backup/previous /source/ /backup/current/

Hardlink unchanged files to previous backup.

Update and Comparison

Update Only

rsync -avu source/ destination/
rsync -av --update source/ destination/

Skip files newer in destination.

Checksum Comparison

rsync -avc source/ destination/
rsync -av --checksum source/ destination/

Compare by checksum instead of time/size.

Size Only

rsync -av --size-only source/ destination/

Compare by size only.

Ignore Times

rsync -av --ignore-times source/ destination/

Don't skip files with same time/size.

Deletion Options

Delete During Transfer

rsync -av --delete source/ destination/

Delete extraneous files during transfer.

Delete Before Transfer

rsync -av --delete-before source/ destination/

Delete files before transfer starts.

Delete After Transfer

rsync -av --delete-after source/ destination/

Delete files after transfer completes.

Delete Excluded

rsync -av --delete --delete-excluded --exclude="*.log" source/ destination/

Delete excluded files in destination.

Max Delete

rsync -av --delete --max-delete=100 source/ destination/

Limit number of deletions.

SSH Options

Custom SSH Port

rsync -av -e "ssh -p 2222" source/ user@host:destination/

Use custom SSH port.

SSH Key

rsync -av -e "ssh -i ~/.ssh/id_rsa" source/ user@host:destination/

Use specific SSH key.

SSH Options

rsync -av -e "ssh -o StrictHostKeyChecking=no" source/ user@host:destination/

Pass options to SSH.

Output Control

Itemize Changes

rsync -av --itemize-changes source/ destination/
rsync -avi source/ destination/

Show detailed changes for each file.

Stats

rsync -av --stats source/ destination/

Show file transfer statistics.

Human Readable

rsync -avh source/ destination/
rsync -av --human-readable source/ destination/

Show file sizes in human-readable format.

List Only

rsync -av --list-only user@host:directory/

List files without transferring.

Advanced Options

Whole File

rsync -avW source/ destination/
rsync -av --whole-file source/ destination/

Copy whole files (no delta algorithm).

Inplace Update

rsync -av --inplace source/ destination/

Update files in place (don't create temp file).

Sparse Files

rsync -avS source/ destination/
rsync -av --sparse source/ destination/

Handle sparse files efficiently.

rsync -avH source/ destination/
rsync -av --hard-links source/ destination/

Preserve hard links.

Extended Attributes

rsync -avX source/ destination/
rsync -av --xattrs source/ destination/

Preserve extended attributes.

ACLs

rsync -avA source/ destination/
rsync -av --acls source/ destination/

Preserve ACLs.

CVS Exclude

rsync -avC source/ destination/
rsync -av --cvs-exclude source/ destination/

Auto-exclude files like .git, .svn, etc.

Examples

Basic Synchronization

# Sync local directories
rsync -av /source/ /destination/

# Sync to remote server
rsync -avz /local/data/ user@server:/remote/data/

# Sync from remote server
rsync -avz user@server:/remote/data/ /local/data/

# Dry run first
rsync -avn /source/ /destination/

Backup Scripts

# Simple backup
rsync -av --delete /data/ /backup/data/

# Backup with progress
rsync -avP --delete /data/ /backup/data/

# Backup excluding files
rsync -av --delete \
  --exclude=".git" \
  --exclude="node_modules" \
  --exclude="*.log" \
  /source/ /backup/

# Incremental backup
DATE=$(date +%Y-%m-%d)
rsync -av --link-dest=/backup/latest \
  /source/ /backup/$DATE/
ln -nsf $DATE /backup/latest

Remote Synchronization

# Sync to remote with SSH key
rsync -avz -e "ssh -i ~/.ssh/backup_key" \
  /local/data/ backup@server:/backup/

# Custom SSH port
rsync -avz -e "ssh -p 2222" \
  /data/ user@host:/backup/

# Bandwidth limit for remote sync
rsync -avz --bwlimit=5000 \
  /data/ user@host:/backup/

# Resume interrupted transfer
rsync -avzP /large/files/ user@host:/backup/

Website Deployment

# Deploy to web server
rsync -avz --delete \
  --exclude=".git" \
  --exclude="node_modules" \
  --exclude=".env" \
  ./dist/ user@webserver:/var/www/html/

# Deploy with dry run first
rsync -avzn --delete ./dist/ user@webserver:/var/www/html/

# Deploy with backup
rsync -avz --delete \
  --backup --backup-dir=/backup/$(date +%Y%m%d) \
  ./dist/ user@webserver:/var/www/html/

Exclude Patterns

# Exclude multiple patterns
rsync -av \
  --exclude="*.log" \
  --exclude="*.tmp" \
  --exclude=".git" \
  --exclude="node_modules" \
  /source/ /destination/

# Exclude from file
cat exclude.txt:
  *.log
  *.tmp
  .git/
  node_modules/
  __pycache__/

rsync -av --exclude-from=exclude.txt /source/ /destination/

# CVS-style exclude
rsync -avC /source/ /destination/

Selective Sync

# Sync only specific file types
rsync -av --include="*.jpg" --include="*.png" --exclude="*" \
  /photos/ /backup/photos/

# Sync directory structure only
rsync -av -f"+ */" -f"- *" /source/ /destination/

# Include/exclude combination
rsync -av \
  --include="*.conf" \
  --include="*.ini" \
  --exclude="*" \
  /config/ /backup/config/

Verification and Comparison

# Checksum verification
rsync -avc /source/ /destination/

# Dry run to see differences
rsync -avnc /source/ /destination/

# Itemized changes
rsync -avi /source/ /destination/

# Show only what changed
rsync -avnc --itemize-changes /source/ /destination/ | grep '^>'

Mirror Directory

# Perfect mirror (delete extra files)
rsync -av --delete /source/ /destination/

# Mirror with progress
rsync -avP --delete /source/ /destination/

# Mirror with checksums
rsync -avc --delete /source/ /destination/

# Mirror excluding patterns
rsync -av --delete \
  --exclude="cache/" \
  --exclude="*.tmp" \
  /source/ /destination/

Snapshot Backups

# Daily snapshot backup
#!/bin/bash
DATE=$(date +%Y-%m-%d)
LATEST=/backup/latest
NEW=/backup/$DATE

rsync -av --delete \
  --link-dest=$LATEST \
  /source/ $NEW/

rm -f $LATEST
ln -s $DATE $LATEST

Database Backup

# Backup with compression
rsync -avz --delete /var/lib/mysql/ /backup/mysql/

# Backup with bandwidth limit
rsync -avz --bwlimit=10000 /var/lib/postgresql/ user@backup:/db/

# Stop service, backup, start service
systemctl stop mysql
rsync -av /var/lib/mysql/ /backup/mysql/
systemctl start mysql

Performance Tuning

# Whole file transfer (faster on local)
rsync -avW /source/ /destination/

# Larger block size
rsync -av --block-size=131072 /source/ /destination/

# Parallel rsync with GNU parallel
find /source -maxdepth 1 -type d | \
  parallel rsync -av {} /destination/

# In-place updates (faster, less safe)
rsync -av --inplace /source/ /destination/

Notes

  • Trailing Slash: /source/ copies contents, /source copies directory itself
  • Archive Mode: -a equals -rlptgoD (recursive, links, perms, times, group, owner, devices)
  • Delta Algorithm: Only transfers differences between files
  • Network Efficiency: Minimal data transfer over network
  • Bandwidth: Use --bwlimit to limit transfer rate
  • Resume: Use -P or --partial to resume interrupted transfers
  • Delete: --delete makes destination identical to source
  • SSH: Default transport for remote transfers
  • Compression: -z compresses data during transfer
  • Checksum: -c verifies by content, not time/size
  • Dry Run: -n shows what would happen without doing it
  • Progress: --progress shows transfer progress per file
  • Stats: --stats shows summary statistics
  • Itemize: -i shows detailed changes for each file
  • Exclude: Patterns match both directories and files
  • Include: Must come before exclude to work
  • Filters: Advanced pattern matching with merge/exclude/include
  • Exit Codes: 0=success, non-zero=error
  • Performance: Faster than scp for large file sets
  • Safety: Always test with -n (dry-run) first
  • Ownership: Requires root to preserve ownership on destination
  • Symlinks: -a preserves symlinks, -L follows them
  • Hard Links: -H preserves hard links
  • ACLs: -A for ACLs, -X for xattrs
  • Sparse: -S handles sparse files efficiently
  • Inplace: --inplace updates files without temp copy
  • Backup: --backup saves replaced files
  • Link-dest: Creates incremental backups with hardlinks
  • Best Practices:
  • Always test with --dry-run first
  • Use -a for full synchronization
  • Use -z for remote transfers
  • Use --delete carefully (test first!)
  • Use --exclude for large directories
  • Use --bwlimit on production systems
  • Use -P for resumable large transfers
  • Mind the trailing slash!
  • Keep backups before mirroring
  • Use --checksum for verification

Get this worldbook via CLI

worldbook get rsync

Comments (0)

Add a Comment

No comments yet. Be the first to comment!