What is rsync? Copy and sync files safely
Author: Domeny.ForSale editorial team Published
In brief: rsync is a command-line tool for copying and synchronizing files between directories or computers. On subsequent runs, it skips files it considers unchanged, and during network transfers it can send only the changed portions of data. It is useful for updating copies of data and transferring files to a server, but ordinary synchronization is no substitute for a backup with version history.
How does rsync work, and how does it differ from ordinary copying?
Rsync compares the specified source with the destination and determines which files need to be transferred. By default, it identifies changes primarily by file size and modification time. This means it does not compare the entire contents of every file on each run.
For network transfers, rsync can use a delta-transfer algorithm, sending missing or changed portions instead of the entire existing file. However, the first copy to an empty directory requires all the selected data to be transferred. When copying locally, rsync transfers entire files that need updating by default, because analyzing individual portions does not always offer an advantage.
For example, suppose you have a directory of product photos on your computer and a copy on a server. After you add five photos, the next rsync run will transfer the new files rather than copy the whole collection again. Deleting a photo from the source will not automatically delete it from the destination, however — that requires a separate option.
Where can you use rsync?
Rsync is widely used on Linux and is also available on macOS. On Windows, you can use it through WSL, for example: an environment that lets you run Linux tools. To check whether the program is available and which version is installed, run:
rsync --version
If the program is missing, install it using the appropriate package manager for your system. Check the documentation for your distribution or the specific program version for installation details and available options.
For typical transfers over SSH, rsync must be installed on both computers. SSH provides an encrypted connection to a remote computer. You also need an account with access to the server, permission to read the source, and permission to write to the destination; SFTP access alone is not always enough.
Your first synchronization, step by step
The basic syntax is rsync [options] source destination. Suppose you want to copy the contents of the projects directory into the existing /mnt/backup/projects/ directory. First, do a trial run without saving any changes:
rsync -av --dry-run --itemize-changes ~/projects/ /mnt/backup/projects/
Here is what the options mean:
-a— archive mode: copies subdirectories and preserves symbolic links and basic metadata, to the extent permitted by the system and your permissions.-v— displays more information about the operation’s progress.--dry-run— simulates the operation without copying or deleting files.--itemize-changes— shows the types of changes planned.
Check that the paths and list of changes are correct. If you are using an external drive, also confirm that it is mounted at the correct location. Then run the actual copy:
rsync -av --itemize-changes ~/projects/ /mnt/backup/projects/
A trailing slash on the source changes the command’s meaning. The source ~/projects/ means copying the directory’s contents. The source ~/projects means copying the directory itself, including its name — so an existing destination directory may end up with an additional projects directory level.
-a mode does not automatically preserve every possible file property. For example, access control lists, extended attributes, and relationships between hard links require additional options and support on both sides.
How do you transfer files to a server over SSH?
To copy the contents of a local directory to a server, you can use the following template. Replace the account name, server address, and path with your own values:
rsync -av -e ssh --dry-run ./site/ user (at) serwer.example:/var/www/site/
The -e ssh option specifies how to connect. After checking the output, remove --dry-run to carry out the transfer. On your first connection, verify the server’s key fingerprint against information provided by the administrator rather than accepting its identity without checking.
To download files, reverse the order of the source and destination: the remote path should come first. Rsync works in one direction — it does not provide automatic two-way synchronization or resolve conflicts between two independently edited copies.
Deleting files and backup limitations
The --delete option deletes files from the synchronized destination directory if they are absent from the source. This is useful for creating a mirror, but an incorrect path or an empty source can cause you to lose data you need. Before using it, always do a trial run with --dry-run --itemize-changes and check the planned deletions.
Even without --delete, rsync can overwrite an earlier version of a file. Nor can it, on its own, provide a consistent copy of a running database: that usually requires a database export or the backup mechanism provided by the database engine.
If you intend to use the data for recovery after an error or failure, plan to retain earlier versions and a separate copy. Try rsync on a small test directory first, then check that you can restore a selected file from the copy.
See also: What is 2FA?
