Skip to main content

What is Rsync?

Quick Summary

Rsync (remote sync) is a powerful command-line utility for efficiently copying and synchronizing files between local and remote Linux systems. It transfers only the changed portions of files (delta transfer), making it dramatically faster than full-copy methods like cp or scp for repeated operations.

Why Every Server Administrator Needs Rsync

Whether you manage a single VPS or dozens of production servers, rsync solves critical day-to-day challenges:

  • Application deployment — Push code changes from development to staging or production without transferring the entire project every time.
  • Incremental backups — Back up only what changed since the last run, saving time, bandwidth, and storage.
  • Server migration — Move everything from one VPS to another, preserving permissions, ownership, and timestamps.
  • Disaster recovery — Maintain synchronized replicas so you can restore service quickly after hardware failure or accidental deletion.
  • Configuration management — Keep /etc/ configs, .env files, and Docker Compose files consistent across multiple servers.
  • Automated workflows — Combine with cron jobs or systemd timers for hands-free, scheduled operations.

How Delta Transfer Works

Traditional copy tools transfer every file in full, even if nothing changed. Rsync uses a rolling checksum algorithm to detect which blocks inside a file have been modified, then transfers only those blocks.

┌──────────────────────────────────────────────────────────────────┐
│ Traditional Copy (cp, scp) │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ 100 MB file │ ──────→ │ 100 MB sent │ Every single time │
│ └──────────────┘ └──────────────┘ │
│ │
│ Rsync (delta transfer) │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ 100 MB file │ ──────→ │ 2 MB sent │ Only the changes │
│ │ (2 MB changed)│ └──────────────┘ │
│ └──────────────┘ │
└──────────────────────────────────────────────────────────────────┘

This makes rsync ideal for repetitive tasks like nightly backups or continuous deployment, where most files stay the same between runs.

Core Command Structure

Every rsync command follows this pattern:

rsync [OPTIONS] SOURCE DESTINATION
PartMeaning
SOURCEFile or directory to copy from (local path or user@host:/path)
DESTINATIONTarget location to copy to (local path or user@host:/path)
-aArchive mode — preserves permissions, ownership, timestamps, symlinks, and recurses into directories
-vVerbose — shows each file being transferred
-zCompress data during network transfer
-PShows progress bar and allows resuming partial transfers
--deleteRemoves files from destination that no longer exist in source
-n / --dry-runSimulates the transfer without making changes

Practical Examples for Server Management

Backup a Web Application

# Back up a web application (e.g., Node.js, Laravel, WordPress)
rsync -avP /var/www/html/ /backup/www/

# Back up a Python project
rsync -avP /var/www/django-app/ /backup/django-app/

Sync Files Between Two Servers Over SSH

# Push local project to a remote production server
rsync -avzP /var/www/html/ user@production-server:/var/www/html/

# Pull files from remote server to local machine
rsync -avzP user@remote-server:/var/www/html/ ./local-backup/

Back Up MySQL Database Dumps

# Export database, then rsync the dump to a backup server
mysqldump -u root -p my_database > /var/backups/db/my_database.sql
rsync -avzP /var/backups/db/ user@backup-server:/backups/db/

Sync Configuration Files

# Back up critical server configs
rsync -av /etc/nginx/ /backup/config/nginx/
rsync -av /etc/ssh/sshd_config /backup/config/ssh/

Preview Before Executing (Dry Run)

# See exactly what would happen before committing
rsync -av --delete --dry-run /source/ /destination/
Always Dry-Run First

On production servers, always run with --dry-run (or -n) first. This shows you exactly which files will be created, modified, or deleted — without actually touching anything.

When to Use Rsync vs Other Tools

ScenarioBest ToolWhy
One-time file copy to remotescpSimpler syntax for single transfers
Repeated syncs / backupsrsyncDelta transfer saves bandwidth and time
Full disk cloningddBlock-level copy for exact disk images
Cloud storage backuprcloneDesigned for cloud providers (S3, GCS)
Version-controlled deploymentsgitTracks code history with branching
Large-scale orchestrationansible / puppetConfiguration management at scale
Web Application Sync

Rsync is the standard for deploying code changes, syncing user-generated media libraries, and creating incremental backups for any web stack (Node.js, Python, PHP, etc.). You'll find practical, real-world workflows in the Real-World Sync Patterns section.

Best Practices

  • Start with --dry-run — Preview every operation before applying it to production.
  • Use archive mode (-a) — Preserves all file metadata, which is critical for web servers and system files.
  • Exclude unnecessary files — Skip cache directories, log files, and temporary data to speed up transfers.
  • Separate your workflows — Keep application code, user uploads, database dumps, and config files in separate rsync operations for safer, more granular rollbacks.
  • Maintain offsite backups — Keep at least one copy on a different server or cloud storage.
  • Use SSH keys, not passwords — For automated and secure transfers.
warning

The --delete flag mirrors the source exactly to the destination. Any file on the destination that doesn't exist in the source will be permanently removed. Always have a separate backup before using this option.

Common Pitfalls

PitfallWhat HappensHow to Avoid
Forgetting the trailing /Copies the directory itself instead of its contents, creating a nested folderUnderstand the trailing slash rule
Skipping --dry-runUnexpected deletions or overwrites on productionAlways preview first
Wrong permissions after syncWeb server can't read files, app breaksUse -a to preserve permissions, or set --chmod
Syncing over slow connection without -zTransfer takes foreverEnable compression for network transfers
Running as wrong userPermission denied errors or wrong file ownershipUse sudo when needed, or --chown to fix ownership

What's Next

Now that you understand what rsync is and why it matters, continue with:

  1. Installation and Setup — Get rsync installed and configured on your servers
  2. Rsync vs SCP — Detailed comparison to help you choose the right tool
  3. Source and Destination — Master path handling and the trailing slash rule