Back to Blog

How to Use rsync: Copy Files and Back Up a Server

Learn the rsync command: copy files between servers over SSH, the trailing slash rule, --dry-run and --delete, excludes, cron backups and common errors.

Nodesty Team8 min readServers
The word rsync with -avzP, --delete and --dry-run on a dark Nodesty background

The rsync command is the standard tool for copying files between Linux servers and taking backups. Unlike a plain copy, it only sends what has changed, so the second run of a 50 GB transfer can finish in seconds. It works over SSH, needs no extra service and is installed on most servers already.

This guide covers the basic syntax and the trailing slash rule, the options you'll use most, copying between servers over SSH, safe use of --delete, migrating a site or game server folder, snapshot backups, automating with cron and the most common errors.

Why rsync instead of scp?

scp copies every file in full each time. rsync compares the source and the destination by file size and modification time, skips files that haven't changed and, for large files that changed, sends only the changed blocks. It can also resume an interrupted transfer and mirror deletions. For a one-off copy of a single file, either is fine; for large folders or anything you copy repeatedly, rsync is the better choice. For the protocol side, see our FTP vs SFTP guide.

Installing rsync

rsync has to be installed on both machines when you copy over SSH:

sudo apt install rsync     # Ubuntu / Debian
sudo dnf install rsync     # AlmaLinux / Rocky Linux
rsync --version

Basic syntax and the trailing slash rule

rsync [options] SOURCE DESTINATION

The most important rule in rsync is the trailing slash on the source:

rsync -a /var/www/ /backup/www/   # copies the contents of www into /backup/www
rsync -a /var/www /backup/        # creates /backup/www and copies into it

With a slash, rsync copies the folder's contents; without it, it copies the folder itself. A slash on the destination makes no difference. Getting this wrong usually ends with a nested www/www folder, or worse, with --delete removing the wrong files.

The options you'll use most

OptionWhat it does
-aArchive mode: recursive, keeps permissions, times, symlinks, owners
-vLists the files being transferred
-zCompresses data in transit, useful on slow links
-hHuman-readable sizes
-PSame as --partial --progress: shows progress, keeps partial files
-n, --dry-runShows what would happen without changing anything
--deleteDeletes destination files that no longer exist in the source
--exclude, --exclude-fromSkips matching files or folders
--bwlimitLimits bandwidth

A good everyday combination is -avhP. Add -z across the internet; skip it on a fast local network or for already-compressed files such as .zip, .jpg or .mp4, where it only costs CPU. Note that -a doesn't include hard links, ACLs or extended attributes; add -H, -A and -X if you need them.

Copying between servers over SSH

rsync uses SSH automatically when one side has the user@host: form. If you're new to SSH, start with our What is SSH? guide.

# Push: from this server to a remote one
rsync -avhP /var/www/ root@203.0.113.10:/var/www/

# Pull: from a remote server to this one
rsync -avhP root@203.0.113.10:/var/www/ /var/www/

If SSH listens on a different port, pass it with -e:

rsync -avhP -e "ssh -p 2222" /var/www/ root@203.0.113.10:/var/www/

To avoid typing a password every time (and to run rsync from cron later), set up an SSH key:

ssh-keygen -t ed25519
ssh-copy-id -p 2222 root@203.0.113.10

Always do a dry run first

Before any command that copies into an important folder, and especially before --delete, add -n:

rsync -avhn --delete /var/www/ root@203.0.113.10:/var/www/

The output lists the files that would be copied and, as deleting ... lines, the files that would be removed. Only when the list looks right, run the same command without -n.

--delete: an exact mirror, and its danger

By default rsync never deletes anything on the destination. With --delete, files that no longer exist in the source are removed, so the destination becomes an exact mirror. That's what you want for keeping a copy in sync, but a wrong path or a missing slash can empty a folder in seconds, and a file deleted by mistake in the source disappears from the mirror on the next run. A mirror alone is not a backup; keep older copies too, as described in the snapshot section below.

Excluding files and limiting bandwidth

rsync -avhP --exclude 'cache/' --exclude '*.log' /var/www/ root@203.0.113.10:/var/www/

Patterns are matched relative to the source folder. For a longer list, put one pattern per line in a file and use --exclude-from=/root/rsync-exclude.txt.

To avoid saturating the network of a live server, limit the speed. The value is in KiB per second, so this is roughly 10 MB/s:

rsync -avhP --bwlimit=10000 /var/www/ root@203.0.113.10:/var/www/

Migrating a website or game server folder

To move files to a new server with minimal downtime, sync in two passes. The first pass copies everything while the old server is still running:

rsync -avhP /var/www/ root@NEW_IP:/var/www/

Then stop the service (web server, game server) on the old machine and run the same command once more with --delete. This time only the files that changed in the meantime are sent, so the downtime is short. For a game server, for example a Minecraft folder under /opt/minecraft/, always run the final pass with the server stopped so world files aren't copied half-written. If you migrate as root and want to keep owners exactly, add --numeric-ids. The DNS and database steps of a move are in our How to migrate a website guide.

Never rsync a running database's data directory

Don't copy /var/lib/mysql or /var/lib/postgresql while the database is running. The files change during the copy and the result may not start at all. Create a dump first and copy the dump:

mysqldump --single-transaction --all-databases > /root/db.sql
sudo -u postgres pg_dumpall > /root/pg.sql

--link-dest lets you keep a full dated copy for each day while unchanged files take no extra space: they're stored as hard links to the previous snapshot. Run this script on the backup server, pulling from the web server:

#!/bin/bash
set -e
DEST=/backups/web
TODAY=$(date +%F)
rsync -a --delete --link-dest="$DEST/latest" root@203.0.113.10:/var/www/ "$DEST/$TODAY/"
ln -sfn "$DEST/$TODAY" "$DEST/latest"

On the first run, latest doesn't exist yet; rsync prints a warning and makes a full copy. Each later run creates a new dated folder that looks complete but only uses space for changed files. Delete old dated folders to control how many days you keep. More strategies are in our Server backups guide.

Automating with cron

Save the script, for example as /root/rsync-backup.sh, make it executable with chmod +x and add it to root's crontab with crontab -e:

0 3 * * * /root/rsync-backup.sh >> /var/log/rsync-backup.log 2>&1

This runs every night at 03:00 and writes the output to a log. The job uses the SSH key you set up, so no password is needed; a separate key used only for backups is a good idea. Putting the commands in a script also avoids the cron rule that % must be escaped. The schedule format is explained in our What is cron? guide.

Restoring files

Restoring is the same command in the other direction. To restore a whole site from a snapshot on the backup server:

rsync -avhn /backups/web/2026-11-09/ root@203.0.113.10:/var/www/

Check the dry run, then remove -n. To restore a single file, give its full path as the source.

Common errors

  • Permission denied (publickey): The SSH login failed. Test with plain ssh first and check the user, port and key.
  • Permission denied (13): The user on the destination can't write to that folder, or can't read the file on the source. Check ownership, or run as a user with the right access; see our Linux file permissions guide.
  • rsync: command not found and connection unexpectedly closed: rsync isn't installed on the remote server. Install it there, or point to it with --rsync-path=/path/to/rsync.
  • protocol version mismatch -- is your shell clean?: A startup file such as .bashrc on the remote server prints text on login, which breaks the rsync protocol. Remove that output for non-interactive shells.
  • connection unexpectedly closed in the middle of a transfer: The network dropped, the remote disk filled up or the remote process was killed. Check df -h on the destination and rerun with -P to resume.

For other everyday commands, see our Linux commands guide.

rsync on Nodesty servers

Nodesty AMD Ryzen 9 9950X VPS and AMD EPYC 7763 VPS plans come with NVMe disks and full root access, so you can use rsync to migrate to your new server and to send backups to a second location. Daily backups are free on VPS plans and are kept for 7 days; an rsync copy on another server adds an independent layer on top of them.

Frequently Asked Questions

What is the difference between rsync and scp?

scp copies every file in full each time you run it. rsync compares the source and the destination and only sends files that changed, and for large files only the changed parts. It can also resume interrupted transfers and delete files that no longer exist in the source, which makes it much better for large or repeated transfers.

Why does a trailing slash on the source matter?

With a trailing slash, as in /var/www/, rsync copies the contents of the folder into the destination. Without it, as in /var/www, rsync creates a folder named www inside the destination and copies into it. The slash on the destination makes no difference.

Is rsync --delete dangerous?

It can be. --delete removes files on the destination that don't exist in the source, so a wrong path or a missing trailing slash can wipe out a whole folder. Always run the same command with --dry-run first and read the list of files it would delete.

Do I need rsync installed on both servers?

Yes. When you copy over SSH, rsync starts a second copy of itself on the remote server. If it isn't installed there, you get an error such as rsync command not found and connection unexpectedly closed. Install the rsync package on both sides.

Can I use rsync to back up a MySQL or PostgreSQL database?

Not by copying the data directory while the database is running, because the copy may be inconsistent and fail to start. Create a dump with mysqldump or pg_dump first, then copy the dump file with rsync.

Is rsync to another folder on the same server a backup?

Only partly. It protects you from accidental deletion, but if the server's disk fails or the server is compromised, the copy is lost too. Keep at least one copy on a different server or storage service.

Nodesty

About the author

Nodesty Team · Infrastructure & Support

The Nodesty team runs our servers, network and DDoS protection day to day and answers customer support requests around the clock. These guides come from that work.

Related articles

Launch your server now.

Build, scale, and conquer without limits on Nodesty's premium infrastructure.

                                                                                                                                                                                                                            
                                                                                                                                                                                                                            
                                                                                                                                                                                                                            
                                                                                                                             ············································                                                   
                                                ··································································································..........···························                                     
                                       ················..........................................................................................................................··············                             
                                  ·············.................::::::::::::::::::::::::::::::::::::::::::::::....................:::::::::::::::::::::::::::::::::::::::................···········                        
                             ············................:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::............·········                     
                         ············.................:::::::::::::::::::::::::::::::::::::::::::::::::::------------::::::::::::::::::::::::::::::----------------:::::::::::::::::::::..........········                  
                    ············......................::::::::::::::::::::::::::::::::::::------------------------------------------------------------------------------------:::::::::::::.........········                
                ··········.............................................::::::::::::::::::-------------==========================================-----------------------------------:::::::::::........·······               
             ········..................:.....................................::::::::::--------=======+++++++++++++++++++++++++++++++++++++=========================------------------::::::::::.......·······              
           ······.........::::::::::::::::::::................................:::::::-----=====++++xxxxxx**************************xxxxxxxxx++++++++++++++===================------------::::::::.......·······             
          ·····.......:::::::::::::::::::::::::::.............................:::::----===+++xxx****%%%%&&&&&&#############&&&&&&&%%%%%*******xxxxxxxx++++++++++++++==============----------:::::::......······             
         ····......::::::::-------------:::::::::::..........................::::---===+++xx***%%%&&&####@@@@@@@@@@@@@@@@@@@@@@@@####&&&&&%%%%*******xxxxxxxxx+++++++++++++===========--------::::::......·····             
        ····.....:::::::--------------------:::::::::.......................::::---===++xxx**%%&&&###@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###&&&&%%%%%********xxxxxxxxxxx++++++++++++========------::::::.....······            
        ····.....:::::--------------------------::::::::....................::::---===++xx***%%&&&####@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###&&&&&%%%%%**********xxxxxxxxxxxxx+++++++++=======------:::::.....·····            
        ····....::::::-------=============---------:::::::::................:::::---===+++xx***%%%&&&#####@@@@@@@@@@@@@@@@@@@@@@@@@#####&&&&&%%%%%%*************xxxxxxxxxxxxxx+++++++++======-----:::::....·····            
         ····....:::::-------================---------:::::::::...............:::::----===++++xxx***%%%%&&&&&&#################&&&&&&%%%%%%%*******************xxxxxxxxxxxxxxxxx++++++++======----:::::....·····            
         ·····....:::::--------=================---------::::::::::..............:::::::-----====+++++xxxxxx**************************xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx++++++++=====-----::::....·····            
          ·····.....:::::--------=================----------:::::::::::.................:::::::::-------=============++++++++++++++++++++++++++++++++++++xxxxxxxxxxxxxxxxxxxxxxx++++++++======-----::::....·····            
            ·····.....:::::::----------===========-------------::::::::::::.........................::::::::::::::::::------------------=============+++++++++++++++++++++++++++++++++=======-----::::.....·····            
              ······......:::::::---------------------------------:::::::::::::........................................:::::::::::::::-----------============+++++++++++++++++++++========------:::::.....·····             
                 ······........:::::::::---------------------------:::::::::::::::::...........................................:::::::::::::------------=============================--------::::::.....······              
                    ·········.........:::::::::::::::-------------::::::::::::::::::::::::.......................................::::::::::::::::--------------------------------------::::::::......·······                
                          ··········............::::::::::::::::::::::::::::::::::::::::::::::::::................................::::::::::::::::::::::::::-------------:::::::::::::::.........········                   
                                  ············..................:::::::::::::::::::::::::::::::::::::::::::.........................:::::::::::::::::::::::::::::::::::::................···········                        
                                             ················.............................................................................................................·················                                 
                                                           ······················................................................................···························                                                
                                                                               ·········································································