Introduction
Rsync is one of the most powerful and versatile tools any Linux system administrator has at their disposal. Its name comes from “remote sync” and, although it was originally designed to synchronize files between machines over the network, its use has expanded to local copy, backup, and mirroring tasks. Thanks to its differential transfer algorithm, Rsync only sends the data blocks that have changed, which significantly reduces the consumed bandwidth and speeds up operations.
What is Rsync?
Rsync works as both client and server, although in most cases only the client mode is used, which communicates with an rsync daemon on the remote machine or, more commonly, through an SSH tunnel. It does not require special privileges to run, as it operates with the permissions of the user who invokes it. Its main advantage lies in the ability to detect differences at the block level, meaning that after the initial full synchronization, subsequent runs only transfer the modified chunks. This makes it the ideal choice for maintaining website replicas, synchronizing development directories, and creating backups that update without recopying the entire dataset each time.
Basic Syntax
The simplest syntax of Rsync is: rsync [options] source destination. The source and destination can be local paths (for example, /home/user/documents) or remote (user@server:/path/to/directory). If the trailing slash is omitted at the end of a source path, Rsync will copy the directory itself inside the destination; adding it copies only its contents. The most used options are -a (archive, preserves permissions and timestamps), -v (verbose), -z (compression), and -h (human-readable output). Combining -avz is a common practice to obtain a faithful and efficient copy.
Most Useful Options
- -n or –dry-run: simulates execution without making changes, useful for testing commands before applying them.
- –delete: deletes in the destination files that no longer exist in the source, keeping an exact mirror.
- –exclude=PATTERN: allows excluding files or directories that match a pattern, avoiding transfer of unnecessary data.
- –backup and –backup-dir=DIR: creates backups of files that are overwritten or deleted before synchronization.
- –partial: keeps partially transferred files in case of interruption, allowing the task to be resumed later.
- –log-file=FILE: logs all operations to a log file for auditing and troubleshooting.
Local Usage Examples
To copy a local directory to another location within the same system, it is enough to specify absolute or relative paths. For example, rsync -av /home/user/projects/ /mnt/backup/projects/ synchronizes the contents of projects while preserving permissions and showing progress. If you want to create a daily backup without overwriting existing files, you can use rsync -av –backup –backup-dir=/mnt/backup/projects_$(date +%F) /home/user/projects/ /mnt/backup/projects/. Another common utility is synchronizing two external hard drives: rsync -avh –progress /media/user/disk1/ /media/user/disk2/ transfers only the changes and shows a real-time progress bar.
Remote Usage Examples (SSH)
When working with remote machines, Rsync uses SSH to encrypt the transfer and authenticate the user. To upload a local website to a server: rsync -avz -e ssh /var/www/html/ user@example.com:/var/www/html/. Here -e ssh indicates SSH as the remote shell and -z enables compression. To download a backup to the local machine, invert source and destination: rsync -avz -e ssh user@example.com:/var/www/backup/ /home/user/backup/. If you need to run without human intervention, for example in a cron job, use SSH keys without a passphrase or ssh-agent for secure authentication. Additionally, –bwlimit=1000 limits the transfer to ~1 MB/s, avoiding saturating the network during peak hours.
Incremental Backups with –link-dest
An advanced technique to save space is to use incremental backups with hard links via –link-dest. A full copy (base) is kept, and for each new backup, Rsync hard-links