Skip to main content

Rsync Options and Flags

This is a comprehensive reference for rsync flags, organized by purpose. Each flag includes what it does, when to use it, and practical examples.

Quick Start

For most server tasks, start with -avzP and add flags as needed:

rsync -avzP /source/ /destination/

This gives you archive mode, verbose output, compression, and progress display.

Archive and Basic Flags​

These are the flags you'll use in virtually every rsync command.

FlagNameDescription
-aArchiveCombines -rlptgoD: recursive, symlinks, permissions, timestamps, group, owner, devices
-vVerboseShows each file being transferred
-rRecursiveRecurse into subdirectories (included in -a)
-nDry runPreview changes without transferring anything
-PProgress + partialShows transfer progress and keeps partial files for resume
-hHuman-readableShows file sizes in KB/MB/GB instead of bytes

What Archive Mode (-a) Actually Does​

The -a flag is a shortcut for seven individual flags:

ComponentFlagWhat It Preserves
Recursive-rDescends into directories
Symlinks-lCopies symlinks as symlinks
Permissions-pFile permissions (rwx bits)
Times-tModification timestamps
Group-gGroup ownership
Owner-oUser ownership (requires root)
Devices-DDevice and special files
info

Archive mode (-a) is the correct choice for almost all server sync operations. It preserves the metadata that web servers, application runtimes, and system services depend on.

Examples​

# Standard backup — archive + verbose
rsync -av /var/www/html/ /backup/www/

# Dry run — see what would happen without doing anything
rsync -av --dry-run /var/www/html/ /backup/www/

# Full combo — archive, verbose, compress, progress + resume
rsync -avzP /var/www/html/ user@backup-server:/backups/www/

Compression and Bandwidth​

Control how much data goes over the network and how fast.

FlagDescriptionWhen to Use
-zCompress data during transferRemote transfers over the internet
--compress-level=NSet compression level (1–9)Fine-tune CPU vs bandwidth trade-off
--bwlimit=KBPSLimit bandwidth in KB/sPrevent saturating shared connections
--whole-fileDisable delta transfer (copy full files)Sometimes faster on LAN

When to Use Compression​

ScenarioUse -z?Why
Remote transfer over internetYesReduces data sent over slow links
Transfer between LAN serversUsually notNetwork is fast; compression adds CPU overhead
Already-compressed files (.gz, .zip, .jpg)NoCan't compress further; wastes CPU
Text-heavy files (code, configs, logs)YesText compresses very well

Examples​

# Compress for remote transfer
rsync -avz /var/www/html/ user@remote:/var/www/html/

# Limit bandwidth to 500 KB/s to avoid saturating the connection
rsync -avz --bwlimit=500 /var/backups/ user@remote:/backups/

# High compression for slow connections
rsync -avz --compress-level=9 /var/www/ user@remote:/var/www/

# Disable delta transfer for LAN (sometimes faster)
rsync -av --whole-file /source/ /nas/backup/

SSH and Connection Options​

Rsync uses SSH by default for remote transfers. These flags control the connection.

FlagDescriptionExample
-e "ssh"Specify remote shell (default is SSH)Usually implicit
-e "ssh -p 2222"Use non-standard SSH portFor servers on custom ports
-e "ssh -i ~/.ssh/key"Use specific SSH keyFor dedicated backup keys
--rsync-path="sudo rsync"Run rsync as sudo on remoteWhen remote user lacks permissions

Examples​

# Non-standard SSH port
rsync -avz -e "ssh -p 2222" /var/www/ user@server:/var/www/

# Specific SSH key for backup automation
rsync -avz -e "ssh -i ~/.ssh/backup_key" /var/www/ backupuser@remote:/backups/

# Remote rsync with sudo (when remote user can't read files)
rsync -avz --rsync-path="sudo rsync" /var/www/ user@server:/var/www/
Security

Always use SSH key-based authentication for automated rsync operations. Never store passwords in scripts. For production automation, create a dedicated SSH key pair with limited permissions.

Filtering: Exclude and Include​

Control which files and directories are transferred.

FlagDescription
--exclude=PATTERNSkip files/directories matching the pattern
--include=PATTERNForce inclusion of matching files (overrides excludes)
--exclude-from=FILERead exclude patterns from a file
--include-from=FILERead include patterns from a file
--filter=RULEApply complex filter rules

Common Exclude Patterns​

# Skip cache directories and log files
rsync -av \
--exclude='cache/' \
--exclude='*.log' \
--exclude='*.tmp' \
/var/www/html/ /backup/www/

# Skip version control and build artifacts
rsync -av \
--exclude='.git/' \
--exclude='node_modules/' \
--exclude='vendor/' \
--exclude='__pycache__/' \
/var/www/app/ /backup/app/

# Skip environment files (contain secrets)
rsync -av \
--exclude='.env' \
--exclude='*.pem' \
--exclude='*.key' \
/var/www/app/ user@staging:/var/www/app/

Using an Exclude File​

Create a reusable exclude list:

/etc/rsync/excludes.txt
# Build artifacts
.git/
node_modules/
vendor/
__pycache__/

# Temporary files
*.log
*.tmp
*.swp
cache/

# Secrets
.env
*.pem
*.key
rsync -av --exclude-from=/etc/rsync/excludes.txt /var/www/ /backup/www/
tip

Using an exclude file keeps your rsync commands clean and makes it easy to maintain consistent patterns across multiple backup scripts.

Deletion and Synchronization​

These flags control how rsync handles files that exist at the destination but not at the source.

FlagDescriptionSafety Level
(no flag)Don't delete anything — only add/updateSafest
--deleteDelete destination files not in sourceDestructive
--delete-beforeDelete before transfer startsMirror accuracy
--delete-duringDelete while transferringFaster for large transfers
--delete-afterDelete after transfer completesSafer for partial syncs
--ignore-existingNever overwrite existing destination filesVery safe
--existingOnly update files that already exist at destinationSafe
--updateSkip files newer at destinationSafe
Critical: Understanding --delete

The --delete flag makes rsync a mirror tool — it makes the destination exactly match the source by removing files from the destination that don't exist in the source.

Always:

  1. Run with --dry-run first
  2. Verify the output carefully
  3. Have a separate backup before using --delete
# ALWAYS preview first
rsync -av --delete --dry-run /source/ /destination/

# Only execute after verifying the dry run output
rsync -av --delete /source/ /destination/

Examples​

# Mirror production to staging (exact copy)
rsync -avz --delete /var/www/html/ user@staging:/var/www/html/

# Add new files without touching existing ones
rsync -av --ignore-existing /source/ /destination/

# Only update files that already exist (no new files)
rsync -av --existing /source/ /destination/

# Skip files that are newer at the destination
rsync -av --update /source/ /destination/

Permissions and Ownership​

Control file metadata at the destination.

FlagDescriptionRoot Required?
-pPreserve permissionsNo
-oPreserve ownerYes
-gPreserve groupDepends
-tPreserve modification timesNo
--chown=USER:GROUPSet ownership on destinationYes
--chmod=MODESet permissions on destinationNo
-aAll of the above (archive mode)-o needs root

Examples​

# Sync and set ownership for web server
rsync -av --chown=www-data:www-data /deploy/app/ /var/www/html/

# Sync and set directory/file permissions
rsync -av --chmod=D755,F644 /deploy/app/ /var/www/html/

# Preserve everything (requires root)
sudo rsync -av /source/ /destination/
Web Server Permissions

After syncing application files, ensure the web server user (often www-data for Nginx/Apache) has read access. Use --chown during sync or fix permissions afterward:

rsync -av --chown=www-data:www-data --chmod=D755,F644 \
/deploy/app/ /var/www/html/

Progress, Logging, and Statistics​

Monitor transfers and keep records.

FlagDescription
--progressShow per-file transfer progress
--statsPrint a summary of transfer statistics at the end
--log-file=PATHWrite detailed log to a file
-v / -vv / -vvvIncreasing levels of verbosity
--itemize-changesShow exactly what changed for each file

Examples​

# Show detailed statistics after backup
rsync -avz --stats /var/www/ /backup/www/

# Log all activity to a file (great for cron jobs)
rsync -avz --log-file=/var/log/rsync-backup.log /var/www/ /backup/www/

# Show exactly what changed and why
rsync -av --itemize-changes /source/ /destination/

Advanced Flags​

FlagDescriptionUse Case
--link-dest=DIRHard-link unchanged files to reference directorySpace-efficient incremental backups
--backupMove overwritten files to backup locationVersioned backups
--backup-dir=DIRWhere to store backup copiesOrganize old versions
--remove-source-filesDelete source files after successful transferMove operations
--partial-dir=DIRStore partial transfers in separate directoryResume on flaky connections
--ignore-timesTransfer all files regardless of timestamp/sizeForce complete resync
--checksumCompare files by checksum instead of timestamp/sizeVerify integrity
--max-size=SIZESkip files larger than SIZEExclude large files
--min-size=SIZESkip files smaller than SIZESkip tiny files

This is one of rsync's most powerful features — create daily snapshots that share disk space:

# Yesterday's backup is the reference
rsync -av --link-dest=/backup/2024-01-14/ \
/var/www/html/ /backup/2024-01-15/

Unchanged files are hard-linked to the previous backup (using zero extra disk space). Only changed files consume new storage.

Common Flag Combinations​

TaskCommand
Standard backuprsync -avzP /source/ /destination/
Safe previewrsync -av --dry-run /source/ /destination/
Mirror (exact copy)rsync -avz --delete /source/ /destination/
Deploy without secretsrsync -avz --exclude='.env' /app/ user@prod:/app/
Bandwidth-limited remotersync -avz --bwlimit=500 /source/ user@remote:/dest/
Incremental snapshotrsync -av --link-dest=/backup/prev/ /source/ /backup/today/
Log everythingrsync -avz --log-file=/var/log/rsync.log /source/ /dest/
Web-safe permissionsrsync -av --chown=www-data:www-data --chmod=D755,F644 /app/ /var/www/

Common Pitfalls​

PitfallRiskPrevention
Using --delete without --dry-runAccidental permanent deletionAlways preview first
Forgetting -a (archive mode)Lost permissions, timestamps, ownershipUse -a for all server operations
Using -z on LAN transfersWasted CPU, slower transfersSkip compression on fast networks
--ignore-errors in productionSilently skips corrupt filesOnly use for non-critical data
--inplace on active filesCorrupts files being read by applicationsUse only for offline file updates
Missing --chown after deploymentWeb server can't read synced filesSet correct ownership during sync

What's Next​