NewCode CLOUD — 20% off your first month on all cloud VPSClaim

rsync Guide: Copy and Sync Files Between Servers Fast and Safely

rsync copies files locally or between servers, and only transfers what changed. That makes it the standard tool for migrating websites, keeping a standby server in sync, pushing deployments and copying backups off a server. It resumes interrupted transfers, preserves permissions and timestamps, and works over SSH with no extra software on the other end.

Install

rsync must be installed on both machines:

sudo apt install rsync        # Ubuntu / Debian
sudo dnf install rsync        # AlmaLinux / Rocky

The command you will use 90% of the time

rsync -avzP /var/www/example.com/ deploy@203.0.113.20:/var/www/example.com/
Option Meaning
-a Archive: recursive, keep permissions, times, symlinks, owner and group (owner/group need root on the target)
-v Verbose: list files
-z Compress during transfer (helps on slow links; skip on fast LANs or for already-compressed files)
-P --partial --progress: show progress and keep partially transferred files so you can resume

The trailing slash rule

This is the most common rsync mistake:

rsync -a /var/www/site  server:/backup/    # creates /backup/site/...
rsync -a /var/www/site/ server:/backup/    # copies the CONTENTS into /backup/...

A trailing slash on the source means “the contents of this directory”. Without it, the directory itself is copied. The trailing slash on the destination does not matter.

Always do a dry run first

rsync -avn --delete /var/www/site/ server:/var/www/site/

-n (--dry-run) lists what would be transferred or deleted without changing anything. Make this a habit, especially with --delete.

Mirroring with –delete

By default rsync never deletes files on the destination. To make the destination an exact mirror, add --delete:

rsync -avP --delete /var/www/site/ server:/var/www/site/

Danger: if you get the paths wrong — wrong trailing slash, wrong directory, or source accidentally empty — --delete removes everything on the destination that is not in the source. Dry-run first, double-check both paths, and never point --delete at a directory that contains other data you need (for example, a web root that also holds a separately managed /blog/ directory). For backups, prefer a tool with snapshots such as restic, so a mistake does not destroy the only copy.

Excluding files

rsync -avP \
  --exclude 'cache/' \
  --exclude '*.log' \
  --exclude 'node_modules/' \
  --exclude '.git/' \
  /var/www/site/ server:/var/www/site/

Or keep them in a file:

rsync -avP --exclude-from=/root/rsync-excludes.txt /var/www/site/ server:/var/www/site/

Patterns are relative to the source directory.

SSH options: keys, ports, users

rsync uses SSH automatically for user@host:path. Pass SSH options with -e:

rsync -avP -e "ssh -p 2222 -i ~/.ssh/backup_ed25519" /data/ backup@203.0.113.20:/backups/web1/

For scheduled jobs, use a dedicated SSH key without a passphrase, restricted on the target to the rsync command if possible — see SSH keys.

Preserving everything for migrations

When moving a site to a new server as root, keep ownership, ACLs, extended attributes and hard links:

sudo rsync -aHAXP --numeric-ids /var/www/ root@NEW_SERVER:/var/www/
Option Keeps
-H Hard links
-A ACLs
-X Extended attributes (SELinux contexts)
--numeric-ids User/group IDs as numbers (useful when names differ)

Run it once to copy the bulk while the old site is live, then again right before the switch — the second run only transfers changes and takes seconds. The full migration process is in migrating a website to a new server.

Large transfers across regions

Copying hundreds of gigabytes between, say, Hong Kong and Los Angeles:

  • Run inside tmux so a dropped SSH session does not kill the transfer: tmux new -s sync.
  • Resume after an interruption by running the same command again; with -P, partial files continue.
  • Skip compression (-z) for already-compressed data (videos, images, archives) — it only costs CPU.
  • Faster ciphers: -e "ssh -c aes128-gcm@openssh.com" on CPUs with AES acceleration.
  • Enable BBR on the sending server for better throughput on long, lossy paths — see TCP BBR.
  • Many small files transfer slowly because of per-file overhead; archive them first (tar) or run several rsync processes on different subdirectories in parallel.

Limit bandwidth

So a big copy does not saturate your server’s port during business hours:

rsync -avP --bwlimit=20M /data/ server:/data/      # about 20 MB/s

Checking for differences

Compare two directories without copying:

rsync -avnc /var/www/site/ server:/var/www/site/

-c compares checksums instead of size and time — slower, but catches files that changed without a timestamp change.

Scheduled pull backups

A simple, robust pattern: the backup server pulls from production, so production holds no credentials for the backup storage:

#!/usr/bin/env bash
# on the backup server
set -euo pipefail
DEST=/srv/backups/web1/$(date +%F)
LATEST=/srv/backups/web1/latest
rsync -aH --delete --link-dest="$LATEST" \
  -e "ssh -i /root/.ssh/pull_web1" \
  root@203.0.113.10:/var/www/ "$DEST/"
ln -sfn "$DEST" "$LATEST"
find /srv/backups/web1 -maxdepth 1 -type d -name '20*' -mtime +30 -exec rm -rf {} +

--link-dest hard-links unchanged files to the previous day’s copy, so each daily folder looks complete but only changed files use new space. Schedule it with cron. Back up databases with a dump first — MySQL backups — rather than copying live database files.

rsync on Windows

Windows users can run rsync inside WSL (Windows Subsystem for Linux), or use tools such as WinSCP for interactive copies and Robocopy for local Windows-to-Windows sync.

Common errors

Error Fix
rsync: command not found (remote) Install rsync on the other server
Permission denied (publickey) SSH key not authorised; test with ssh first
Files copied into a nested folder Missing trailing slash on the source
failed to set times / chown errors Destination filesystem or non-root user cannot set them; add --no-o --no-g or run as root
Transfer very slow with many small files Archive first or parallelise