rsync commands cheat sheet
Basic options
-
rsync -av /var/www/site/ /backup/site/Copies the contents of the directory in archive mode (-a) and lists each file as it's transferred (-v). On a repeat run, only files that changed are transferred, so the second copy is much faster.
-
rsync -aArchive mode, equivalent to -rlptgoD: copies recursively and preserves symbolic links, permissions, modification times, group, owner, and device files. Preserving the owner and device files requires root privileges on the receiving side. Archive mode doesn't include hard links (-H), ACLs (-A), or extended attributes (-X).
-
rsync -avh /var/www/site/ /backup/site/Shows sizes and numbers in a human-readable format, such as 1.2M instead of a long byte count.
-
rsync -avz /var/www/site/ user@203.0.113.10:/backup/site/Compresses the data during the transfer, so less data goes over a slow connection. For files that are already compressed, such as images, videos, and archives, compression saves almost nothing, and on a fast internal network it only loads the CPU, so skip it there.
-
rsync -avP backup.tar.gz user@203.0.113.10:/backup/The -P option is equivalent to --partial --progress: it shows the progress of each file and keeps a file whose transfer was interrupted, so a repeat run resumes from it instead of starting over. Especially useful for large files.
-
rsync -a --info=progress2 /var/www/ /backup/www/Shows a single progress line for the whole transfer instead of a line for each file. Handy when you copy a directory with thousands of small files.
-
rsync -avn /var/www/site/ /backup/site/A dry run (--dry-run): shows what would be transferred, without changing anything. Run it before every new command, and especially before a command with --delete.
-
rsync -ain /var/www/site/ /backup/site/The -i option shows a short code for each file that describes what will change: content, size, time, permissions, or owner. Together with -n, you get an exact list of the changes before the real run.
Example:
>f.st...... index.php -
rsync -a --stats /var/www/site/ /backup/site/Shows a summary of the transfer at the end: the number of files, how many of them were transferred, the amount of data sent, and the transfer speed.
-
rsync -a --mkpath /var/www/site/ /backup/2026/site/Creates the missing directories in the destination path before copying, instead of failing when the path doesn't exist yet.
The trailing slash on the source path
-
rsync -a /var/www/site /backup/Without a trailing slash on the source, the directory itself is copied, and the result is the /backup/site directory.
-
rsync -a /var/www/site/ /backup/site/With a trailing slash on the source, only the contents of the directory are copied, into /backup/site. You can read the slash as "copy what's inside the directory."
-
rsync -a /var/www/site/ /backup/Caution: with a slash on the source and a general destination, the site's files are spread directly into /backup, without a directory named site. Combined with --delete, a mistake like this may delete other files in the destination, so check with -n first.
Transfers over SSH
-
rsync -av /var/www/site/ user@203.0.113.10:/home/user/site/Push: transfers from the local computer to the remote server. A single colon after the server name means a connection over SSH, with the same user and the same password or key you use to log in to the server.
-
rsync -av user@203.0.113.10:/home/user/site/ /backup/site/Pull: transfers from the remote server to the local computer. This is how a backup server pulls files from other servers.
-
rsync -av -e "ssh -p 2222" /var/www/site/ user@203.0.113.10:/home/user/site/Connects to a server where the SSH service listens on a port other than 22.
-
rsync -av -e "ssh -p 2222 -i ~/.ssh/id_ed25519" /var/www/site/ user@203.0.113.10:/home/user/site/Connects with a specific SSH key and on a different port. Suitable for a backup script that runs without typing a password.
-
rsync -av /var/www/site/ myserver:/home/user/site/When a shortcut for the server is defined in the ~/.ssh/config file, with the address, user, port, and key, rsync uses it, and you don't need the -e option.
-
rsync -av --rsync-path="sudo rsync" user@203.0.113.10:/etc/nginx/ /backup/nginx/Caution: runs rsync on the remote server with sudo, to read or write root's files without logging in as root. The user needs permission to run sudo rsync without typing a password, and this is a broad permission that allows writing to any file on the server. Give it only to a dedicated user.
-
rsync -avP --bwlimit=5000 /var/www/site/ user@203.0.113.10:/backup/site/Limits the transfer rate to about 5,000 KiB per second. Without a suffix, the unit is KiB. Useful when you transfer from a live server, so the transfer doesn't slow down the site.
-
rsync -av --timeout=300 /var/www/site/ user@203.0.113.10:/backup/site/Stops the transfer when no data moves for 300 seconds, with exit code 30. This way, a scheduled script doesn't hang for hours.
Syncing with deletion (mirroring)
-
rsync -av --delete /var/www/site/ /backup/site/Caution: deletes from the destination any file that doesn't exist in the source, so the destination becomes an exact mirror of the source. A mistake in the path, especially in the trailing slash on the source, may delete files in the destination. Always run the same command with -n first.
-
rsync -avn --delete /var/www/site/ /backup/site/A dry run of a sync with deletion: the files that would be deleted are shown in lines that start with deleting, and nothing is actually deleted.
Example:
deleting old-page.html -
rsync -av --delete-after /var/www/site/ /backup/site/Deletes the extra files in the destination only after the transfer is finished. By default, --delete deletes during the transfer.
-
rsync -av --delete-delay /var/www/site/ /backup/site/Finds the files to delete during the transfer and deletes them at the end. More efficient than the --delete-after option.
-
rsync -av --delete --max-delete=50 /var/www/site/ /backup/site/A safety net: deletes at most 50 files and directories. When more deletions are needed, rsync skips the rest and exits with code 25, so a mistake in the path doesn't empty the destination.
-
rsync -av --delete --backup --backup-dir=/backup/changed/$(date +%F) /var/www/site/ /backup/site/Files that are deleted or replaced in the destination don't disappear; they're kept in a separate directory by date. This way, you can restore a file that was deleted from the source by mistake.
-
rsync -av --remove-source-files /var/backups/export/ user@203.0.113.10:/archive/Caution: deletes from the source every file that was transferred successfully, so the command moves files instead of copying them. The directories themselves stay in the source. Suitable for moving backup files that are fully written, not for a directory that files are written to during the transfer.
Excluding and filtering files
-
rsync -av --exclude='cache/' /var/www/site/ /backup/site/Excludes every directory named cache, at any level. A trailing slash in the pattern matches directories only, not files.
-
rsync -av --exclude='*.log' --exclude='node_modules/' --exclude='.git/' /var/www/app/ /backup/app/Excludes log files, node_modules directories, and .git directories. You can repeat --exclude as many times as you need.
-
rsync -av --exclude='/storage/logs/' /var/www/app/ /backup/app/A leading slash anchors the pattern to the root of the transfer: only the storage/logs directory at the root of the source is excluded, not other logs directories.
-
rsync -av --exclude-from=/root/rsync-exclude.txt /var/www/site/ /backup/site/Reads the excludes from a file, one pattern per line. Blank lines and comment lines that start with # or ; are ignored. Handy when the exclude list is long or shared by several scripts.
-
rsync -av --include='*/' --include='*.php' --exclude='*' /var/www/site/ /backup/php/Copies only PHP files. The order of the rules matters, because the first rule that matches a file is the one that applies to it. The '*/' rule includes all directories, and without it rsync wouldn't enter them because of '*'.
-
rsync -avm --include='*/' --include='*.php' --exclude='*' /var/www/site/ /backup/php/The -m option (--prune-empty-dirs) skips directories that are left empty after filtering, so only directories that contain files are created in the destination.
-
rsync -av --delete --delete-excluded --exclude='cache/' /var/www/site/ /backup/site/Caution: by default, excluded files are also protected from deletion in the destination. The --delete-excluded option deletes them from the destination too, for example cache directories that were copied earlier.
Comparing and skipping files
-
rsync -avc /var/www/site/ /backup/site/Compares files by a checksum of their content instead of by modification time and size. It reads the files in full on both sides, so it's slow; use it when modification times aren't reliable.
-
rsync -av --ignore-existing /var/www/uploads/ /backup/uploads/Skips every file that already exists in the destination and copies only new files. Suitable for an uploads directory where existing files don't change.
-
rsync -avu /var/www/site/ /backup/site/The -u option (--update) skips files whose modification time in the destination is newer, so a change made in the destination isn't overwritten.
-
rsync -av --size-only /mnt/old/ /mnt/new/Compares by size only and ignores the modification time. Useful when you switch to rsync from another copy tool that didn't preserve file times.
-
rsync -av --inplace /var/lib/images/disk.img /backup/images/Caution: writes the changes directly to the existing file in the destination instead of creating a temporary copy and replacing it. It saves space with large files, but during the transfer the file in the destination isn't valid, and if the transfer is interrupted, it stays that way. An executable that's in use won't be updated, or the program running from it may crash.
Permissions, ownership, and attributes
-
sudo rsync -aHAX /srv/data/ /mnt/newdisk/data/Adds to archive mode the preservation of hard links (-H), ACLs (-A), and extended attributes (-X). Suitable for a full copy of data to another disk or server.
-
sudo rsync -a --numeric-ids /srv/data/ root@203.0.113.20:/srv/data/Transfers user and group IDs (UID and GID) as they are, instead of matching them by name. Suitable for moving an entire server, when the IDs need to stay the same.
-
sudo rsync -a --chown=www-data:www-data /tmp/site/ /var/www/site/Sets the owner and group of all the files in the destination. Changing the owner requires root privileges on the receiving side.
-
rsync -a --chmod=D755,F644 /tmp/site/ /var/www/site/Sets permissions during the copy: D for directories and F for files. Here, 755 for directories and 644 for files.
Incremental backups and scheduling
-
rsync -a --delete --link-dest=/backup/latest /var/www/ /backup/$(date +%F)/An incremental backup: every day, a new directory is created that looks like a full backup, but files that haven't changed since the previous backup aren't copied again; they're created as hard links to the file in the previous backup. The space added each day is only that of the files that changed. A relative path in --link-dest is relative to the destination directory, so a full path is better.
-
ln -sfn /backup/$(date +%F) /backup/latestUpdates the latest link so it points to the backup that just finished, and the next backup uses it as its base. Run it only after rsync finishes successfully.
-
rsync -a --log-file=/var/log/rsync-backup.log /var/www/ /backup/www/Writes everything that was done to a log file, including a line for each file that changed. This way, you can check in the morning what was backed up overnight and whether there were errors.
-
echo $?Shows the exit code of the last command. 0 means success; 23 and 24 mean a partial transfer; 30 means a timeout. In a backup script, check the code before you continue.
Example:
0 -
30 2 * * * /usr/local/bin/rsync-backup.shA line in the crontab file that runs the backup script every night at 2:30 AM. Edit the file with crontab -e.
Moving a site to a new server
-
rsync -avzP /home/user/public_html/ user@203.0.113.20:/home/user/public_html/The first copy of the site files from the old server to the new one, while the site keeps running. This copy takes a long time, but it needs no downtime.
-
rsync -avzP --delete /home/user/public_html/ user@203.0.113.20:/home/user/public_html/The second copy, right before the switch: only the files that changed since the first copy are transferred, so it's short. --delete deletes files on the new server that were deleted from the old server in the meantime. Run it with -n first.
-
rsync -avzP --exclude='wp-content/cache/' /home/user/public_html/ user@203.0.113.20:/home/user/public_html/Skips cache directories, which are rebuilt on the new server. The transfer is shorter and has no unnecessary files.
-
mysqldump --single-transaction -u dbuser -p dbname > dbname.sqlMove the database separately, because copying the files of a running database doesn't give a consistent copy. This command creates a backup file of the database on the old server.
-
rsync -avP dbname.sql user@203.0.113.20:~/Transfers the database backup file to the user's home directory on the new server.
-
mysql -u dbuser -p dbname < dbname.sqlOn the new server: loads the backup file into an empty database created in advance.
rsync server (rsync daemon)
-
rsync rsync://backup.example.com/Lists the modules, that is, the shared directories, that an rsync server offers. The connection goes directly to the rsync server on port 873, not over SSH.
-
rsync -av /var/www/site/ backup@backup.example.com::site/Two colons after the server name mean a connection to an rsync server, to a module named site. Data on this kind of connection isn't encrypted.
-
rsync -av --password-file=/root/.rsync-pass /var/www/site/ backup@backup.example.com::site/Reads the module password from a file, for use in scripts. The file must be accessible only to its owner (chmod 600).
-
[site]In the /etc/rsyncd.conf file: the start of the definition of a module named site. The settings after it apply to the module until the next module.
-
path = /srv/backup/siteThe directory on the server that the module exposes. Required in every module.
-
read only = noAllows writing to the module. The default is read-only.
-
hosts allow = 10.0.0.0/24Allows connections only from the listed addresses. Without this setting, any address can connect.
-
auth users = backupRequires a user name and password. Without this setting, the module is open to access without authentication.
-
secrets file = /etc/rsyncd.secretsThe file of users and passwords, in lines of the form backup:password. The file must not be readable by other users on the server, so set its permissions to 600; otherwise, the server rejects the connection.
-
sudo rsync --daemonStarts the rsync server, which reads its settings from the /etc/rsyncd.conf file and listens on port 873.
Common errors
-
rsync: connection unexpectedly closedThe remote side closed the connection. Common causes: rsync isn't installed on the remote server, the SSH login failed, or there's no free space in the destination. First test a regular login with ssh user@host, and then run rsync with -vvv for details.
-
bash: rsync: command not foundrsync must be installed on both sides. Install it on the remote server: sudo apt install rsync on Ubuntu and Debian, or sudo dnf install rsync on AlmaLinux and Rocky Linux.
-
protocol version mismatch -- is your shell clean?A startup file of the user on the remote server, such as .bashrc, prints text at login, and the text disrupts rsync's communication. Test with ssh user@host /bin/true > out.dat: the out.dat file must be empty. Change the startup file so the output runs only in an interactive terminal.
-
Permission denied (publickey,password)This error comes from SSH, not from rsync: logging in to the server failed. Check the user name, the key chosen with -i, and the port.
-
failed: Permission denied (13)The user doesn't have permission to read a file in the source or to write to the destination. Check ownership and permissions with ls -l, run the command as the user that owns the files, or use sudo for system files.
-
rsync error: some files/attrs were not transferred (see previous errors) (code 23)A partial transfer because of an error: most of the files were transferred, but some weren't. The cause appears in the lines before the message, usually permissions, an invalid file name, or a full disk.
-
rsync warning: some files vanished before they could be transferred (code 24)Files were deleted in the source during the transfer, for example cache or temporary files. When you back up a live site, this is usually just a warning, and many backup scripts treat code 24 as success.
-
rsync error: timeout in data send/receive (code 30)No data moved for the time set with the --timeout option. Check the connection and the server load, and increase the value if there are large files that take a long time to compare.
-
No space left on device (28)The disk in the destination is full. Check the free space with df -h, and free up space or exclude unnecessary directories.
No matching commands and options.
Names and addresses in the examples
The examples on this page use the example.com domain and IP addresses reserved for documentation, such as 203.0.113.10, so replace them with the names and addresses of your own servers.
Before every sync with deletion
- Dry run first: run the command with -n and check the deleting lines. Only when the list is right, run it without -n.
- The trailing slash on the source:
site/copies the contents, andsitecopies the directory itself. Most rsync mistakes start here. - A safety net:
--max-deletelimits the number of deletions, and--backup-dirkeeps a copy of every deleted file.
A template for a daily backup script
- At the top of the script, set the source directory, the backups directory, and the date.
- Run rsync with
--link-destpointing to the previous backup and with--log-file, so each day is kept as a full backup that takes little space. - Check the exit code: only when it's 0, or 24 when files were deleted during the backup, update the latest link.
- Schedule the script with cron. You can build the line with the Cron expression generator.
- Keep the backup on another server or disk, not on the same disk as the data.
Moving a site: files and database
rsync moves the site files, and the database is moved separately with mysqldump and imported on the new server. You'll find the full backup and restore commands on the MySQL commands page. When logging in to the new server fails, the cause is usually covered on the Common SSH errors page.
rsync server or SSH
An rsync server authenticates users but doesn't encrypt the data. That's why most transfers go over SSH, which encrypts the whole connection and doesn't need an extra port. When you run an rsync server, open port 873 in the firewall only to the addresses that need access.
On a Linux VPS, you get full root access over SSH, so you can back up, sync, and move sites with rsync directly from the server. Alongside the backups of our cloud backup service, rsync lets you keep an extra copy of your own wherever you choose.
rsync commands FAQ
What's the difference between rsync and scp?
rsync compares the source with the destination and transfers only files that changed, and between servers it sends only the changed parts of each file. That's why a repeat copy of a large directory finishes quickly, an interrupted transfer resumes from where it stopped, and you can make the destination an exact mirror of the source. scp suits a quick copy of a single file, and rsync suits backups, syncing, and moving whole sites.
How do I move a site to a new server with rsync?
Copy the site files to the new server while the site keeps running. Right before the switch, run the same command again with --delete, and only the files that changed in the meantime are transferred, so the downtime is short. Export the database with mysqldump and import it on the new server, and finally update the DNS records.
How do I set up an automatic daily backup with rsync?
Write a short script that runs rsync with --link-dest pointing to the previous backup, so each day is kept in its own directory as a full backup, but only the files that changed take up new space. Schedule the script with cron, and check the log file and the exit code from time to time. This way, you can restore a file from any day that was kept.
Does rsync need to be installed on both servers?
Yes. In a transfer over SSH, rsync on the local computer starts a copy of rsync on the remote server, and the two copies work together. When it's missing on one side, you get the error rsync: command not found, and you install it with a single apt or dnf command.
What difference does the trailing slash on the source path make?
A trailing slash on the source, as in /var/www/site/, copies only the contents of the directory. Without the slash, /var/www/site copies the directory itself, and a directory named site is created in the destination. The difference matters most with --delete, so test every new command with -n first.