How to use rsync to copy and sync files between servers

Copy and sync folders locally or between servers with rsync: the options you need, the trailing slash rule, dry runs, deletes, backups and migrations.

3–4 minutes
Pile of hard drive circuit boards

rsync copies files and folders, locally or between computers over SSH. Its strength is that it only sends what has changed, so the second run of a big copy takes seconds. It is the standard tool for server backups and for moving sites to a new host.

The command you will use most is rsync -avz --progress source/ user@server:/destination/. -a keeps permissions and dates, -v lists files, -z compresses in transit. Always test with –dry-run first, and remember the trailing slash: source/ copies the folder’s contents, source copies the folder itself.

Install it

rsync is installed on most Linux servers and on macOS. If not:

sudo apt install rsync
sudo dnf install rsync

The first line is for Ubuntu and Debian, the second for AlmaLinux and Rocky. It must be installed on both ends of a remote copy.

The basic command

rsync -avz --progress /var/www/example.com/ exampleco@203.0.113.10:/var/www/example.com/
OptionMeaning
-aArchive: copy folders recursively and keep permissions, owners, dates and links
-vVerbose: list each file
-zCompress data on the way, useful over the internet
--progressShow progress for each file
-n or --dry-runShow what would happen without changing anything
--deleteRemove files at the destination that no longer exist at the source
--excludeSkip matching files or folders
-e "ssh -p 2222"Use SSH on a non-standard port

The trailing slash rule

This is the mistake everyone makes once:

CommandResult
rsync -a site/ backup/Contents of site go into backup
rsync -a site backup/Creates backup/site/

The destination’s slash does not matter; the source’s does. When in doubt, run with --dry-run and read the list.

Pull from a server, or push to one

Push from the machine you are on:

rsync -avz ~/site/ exampleco@203.0.113.10:/var/www/example.com/

Pull from a server to the machine you are on:

rsync -avz exampleco@203.0.113.10:/var/www/example.com/ ~/site-copy/

Both use your normal SSHSSH A secure way to type commands on a server from your own computer, as if you were sitting at it. More about SSH → login, so SSH keys make it run without a password prompt.

Mirror exactly with –delete

--delete makes the destination an exact mirror, removing files that were deleted at the source. It is powerful and dangerous: with the paths the wrong way round, it deletes your real files. Always dry-run it first:

rsync -avz --delete --dry-run ~/site/ exampleco@203.0.113.10:/var/www/example.com/

Lines starting deleting show what would go. Run again without --dry-run only when the list is what you expect.

Exclude what you do not need

rsync -avz --exclude 'wp-content/cache/' --exclude '*.log' --exclude 'node_modules/' ~/site/ backup:/srv/site/

Exclude paths are relative to the source folder. For many rules, list them in a file and use --exclude-from=excludes.txt.

Use it for backups

A nightly copy of your sites to a second server, run by a cron job:

0 3 * * * rsync -az --delete /var/www/ backup@198.51.100.20:/backups/web/ >> /var/log/rsync-backup.log 2>&1

A mirror on its own is not a full backup: if a file is deleted or encrypted by ransomware, the next run copies the damage. Keep dated versions too, with --link-dest or a tool built for versioned backups. See how to back up a Linux server.

Use it for migrations

rsync is the best way to move large sites. Copy once in advance, then again just before the switch to pick up changes; the second run is fast. The full process is in how to move a WordPress site to a new host.

Common problems

MessageCauseFix
rsync: command not found on the remote sidersync missing on the serverInstall it there
Permission deniedWrong user, or no write access to the destinationCheck the folder owner, or use the right user
connection refusedSSH on another port or blockedAdd -e "ssh -p PORT" and check the firewall
Files owned by the wrong user afterwardsCopied as a different userRun chown on the destination, or copy as the site’s user
Every file copied again each runDates not preservedMake sure -a is used

Official documentation

Get the next guide

One email when there is something worth reading: new guides, errors going around, and hosting changes that affect your site.

No tracking pixels, and one click to leave.

Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.