Log inSign up

Copy, sync and mirror folders with rsync

Install rsync on both ends, then run rsync -avP ./site/ deploy@198.51.100.24:/srv/site/ to copy a folder over SSH. Later runs send only the changed parts of files, and adding --delete (after a --dry-run) turns the copy into an exact mirror.

Topic: File transfer and archives · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Why pick rsync instead of a plain copy?

rsync compares source and destination and transfers only the differences, which makes the second and every later run dramatically shorter than the first. It checks file size and modification time by default, splits changed files into blocks and sends only the blocks that differ. The transport is plain SSH, so there is nothing extra to open on the firewall and the data is encrypted on the way.

That makes it the everyday tool for deploying a website build, pulling logs to a laptop, moving an application to a new server or keeping a second copy of a data folder. For one quick file, scp is shorter to type. For anything you will copy more than once, rsync wins.

How do I install rsync on each distribution?

Both machines need the rsync binary, because rsync starts a copy of itself on the far side through SSH. Ubuntu images usually ship it; minimal Debian, Rocky and AlmaLinux installs may not.

# Ubuntu 24.04 / 22.04, Debian 12 / 13
sudo apt update
sudo apt install rsync

# Rocky Linux 9, AlmaLinux 9
sudo dnf install rsync

rsync --version | head -n 1

On macOS an older rsync or the openrsync replacement is preinstalled; the options used in this guide work with both, though --info=progress2 needs rsync 3.1 or newer. On Windows, run rsync inside WSL. Make sure key based SSH login already works (see SSH keys and hardening), because rsync uses exactly the same login.

What does the trailing slash on the source change?

A slash at the end of the source means "the contents of this folder"; without it, rsync copies the folder itself. This single character causes most rsync surprises, so it is worth seeing side by side.

# copies the files inside site/ into /srv/site/
rsync -av ./site/ deploy@198.51.100.24:/srv/site/

# creates /srv/site/site/ on the server
rsync -av ./site deploy@198.51.100.24:/srv/site/

The slash on the destination does not matter. A good habit: always put the slash on both sides when you mean "make these two folders match". The -a (archive) flag keeps permissions, timestamps, symlinks and, when run as root, ownership. -v lists the files, -P shows progress and keeps partial files so an interrupted transfer resumes instead of starting over, and -z compresses data in transit, which helps with text over slow links but only wastes CPU on already compressed media.

How do I mirror a folder safely with --delete?

Run the command with --dry-run first, read the list, then run it again without the dry run. --delete removes files from the destination that no longer exist in the source, which is exactly what a mirror needs and exactly what wipes data when the paths are wrong.

rsync -av --delete --dry-run ./public/ deploy@198.51.100.24:/var/www/example.net/
rsync -av --delete ./public/ deploy@198.51.100.24:/var/www/example.net/

Lines beginning with deleting show what would disappear. If you want a safety net, add --backup --backup-dir=/var/backups/site-$(date +%F) so replaced and deleted files are moved aside instead of lost. Excludes keep build caches and secrets out of the copy:

rsync -av --delete \
  --exclude=.git/ --exclude=node_modules/ --exclude=.env \
  ./app/ deploy@198.51.100.24:/srv/app/

For longer lists, put one pattern per line in a file and pass --exclude-from=rsync-excludes.txt. Excluded files on the destination are protected from --delete unless you also pass --delete-excluded.

How do I use a custom SSH port, key or bandwidth cap?

Pass SSH options through -e. If you moved SSH to port 2222 and use a dedicated key, the command looks like this:

rsync -avP -e "ssh -p 2222 -i ~/.ssh/deploy_ed25519" \
  ./backup.tar.zst deploy@198.51.100.24:/home/deploy/

A cleaner option is a host block in ~/.ssh/config on your machine, after which plain rsync -avP file vps:/path/ works and every other SSH tool picks up the same settings:

Host vps
    HostName 198.51.100.24
    User deploy
    Port 2222
    IdentityFile ~/.ssh/deploy_ed25519

To avoid filling your monthly transfer allowance or saturating a home connection during the day, cap the rate with --bwlimit=20M (about 20 MB per second). Both copying to and from the server count against the plan, and you can watch the total with the usage monitoring tutorial.

How do I copy a whole application with owners and ACLs intact?

Run rsync as root on both sides and add -H -A -X --numeric-ids so hard links, ACLs, extended attributes and numeric owner IDs survive. That matters when you migrate to a new server, for example after deploying a bigger plan as described in move to a larger plan.

sudo rsync -aHAX --numeric-ids -P \
  -e "ssh -i /root/.ssh/migrate_ed25519" \
  /srv/ root@198.51.100.24:/srv/

Stop services that write to the folder (a database, for instance) before the final pass, or dump the database separately. A common pattern is one long copy while everything still runs, then a short second pass during a few minutes of downtime, which only moves the files changed in between.

How do I schedule a recurring sync with a systemd timer?

Wrap the command in a oneshot service and trigger it with a timer; systemd then logs every run and can catch up after a reboot. Create /etc/systemd/system/site-sync.service:

[Unit]
Description=Pull uploads from the web server

[Service]
Type=oneshot
User=deploy
ExecStart=/usr/bin/rsync -a --delete deploy@198.51.100.24:/var/www/uploads/ /srv/mirror/uploads/

And /etc/systemd/system/site-sync.timer:

[Unit]
Description=Run site-sync every hour

[Timer]
OnCalendar=hourly
Persistent=true

[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now site-sync.timer
systemctl list-timers site-sync.timer
journalctl -u site-sync.service -n 20

The deploy user needs a passphrase-free key for unattended runs; restrict that key on the far side as shown in the FAQ. More scheduling options are in cron jobs and systemd timers.

How can I tell that the copy is complete and correct?

Run the same command again with --dry-run --itemize-changes: if it lists nothing, source and destination match. For a stronger test, add --checksum, which reads every file on both sides and compares content rather than size and time. It is slow on large trees but definitive.

rsync -an --delete --itemize-changes ./site/ vps:/srv/site/
rsync -anc --itemize-changes ./site/ vps:/srv/site/

The exit code also tells the story: 0 means success, 23 means some files could not be transferred (usually permissions), and 24 means files vanished during the copy, which is normal for live log folders.

Which errors show up most often?

  • rsync: command not found on the remote side: install rsync on the server too.
  • Permission denied (13): the destination folder is owned by another user. Fix ownership with sudo chown -R deploy: /srv/site or run the remote end with --rsync-path="sudo rsync" after allowing that in sudoers.
  • protocol version mismatch: something in the remote login prints text (an echo in .bashrc). Silence it for non-interactive shells.
  • Connection timed out: check that the firewall allows your SSH port with sudo ufw status or sudo firewall-cmd --list-all.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1rsync itself needs very little memory1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months
A2-4room for large trees and checksum passes2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months

Questions about Copy and mirror files with rsync

Do I need to open port 873 for rsync?

No. Port 873 is only for the standalone rsync daemon. Running rsync over SSH, as every example here does, needs only your SSH port, and that is the safer choice on a public server.

Is rsync a backup tool?

It makes a copy, not a history. If a file gets corrupted and the next sync runs, the copy is corrupted too. For versions over time, use BorgBackup or the daily backups setup, or keep dated folders with --link-dest.

How do I limit what an automated rsync key can do?

Install the rrsync helper (shipped with rsync on Debian and Ubuntu as /usr/bin/rrsync) and prefix the key in authorized_keys with command="rrsync -ro /srv/mirror",restrict. That key can then only read one folder.

Why does rsync copy every file again on the second run?

Usually the timestamps were not kept. Use -a (or at least -t), and on file systems with coarse time stamps add --modify-window=1.

Can rsync copy between two remote servers?

Not directly from a third machine. Log in to one of the two servers and run rsync there with the other as the remote side; that keeps the data off your own connection.