Introduction
Rsync is one of the most powerful and versatile tools any Linux system manager has at his disposal. His name comes from »remote sync» and, although originally designed to synchronize files between machines across the network, its use has been extended to local copying, backup and mirroring tasks. Thanks to its differential transfer algorithm, Rsync only sends the data blocks that have changed, which significantly reduces the bandwidth consumed and accelerates operations.
What is Rsync?
Rsync works as a client and server, although in most cases only the client mode is used, which communicates with a daemon rsync on the remote computer or, more commonly, through an SSH tunnel. It does not require special privileges to run, as it operates with the permission of the user who invokes it. Its main advantage is the ability to detect block-level differences, which means that, after the first complete synchronization, subsequent executions only transfer the modified fragments. This makes it the ideal option to maintain replicas of websites, synchronize development directories and create backup that is updated without recopying the entire data set at a time.
Basic syntax
The simplest syntax of Rsync is: rsync [options] destination origin. Origin and destination can be local routes (e.g., / home / user / documents) or remote (user @ server: / route / to / directory). If the end bar is omitted at the end of a source route, Rsync will copy the directory itself within the destination; when adding it, it only copies its content. The most used options are -a (archive, preserve permissions and timstamps), -v (verbose), -z (compression) and -h (legible output). Combining -avz is a common practice to obtain a faithful and efficient copy.
Most useful options
- -n or -dry-run: simulates the execution without making changes, useful to test commands before applying them.
- -delete: it eliminates in the destination files that no longer exist in the origin, maintaining an exact mirror.
- -excludes = PATON: allows to exclude files or directories that match a pattern, avoiding transferring unnecessary data.
- -backup and -backup-dir = DIR: create backup of overwritten or deleted files before synchronization.
- -Partial: it keeps files partially transferred in case of interruption, allowing to resume the task later.
- -log-file = ARCHIVO: records all operations in a log file for audit and problem resolution.
Examples of local use
To copy a local directory to another location within the same system, simply indicate the absolute or relative routes. For example, rsync -av / home / user / projects / / mnt / backup / projects / synchronizes project content by maintaining 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 to synchronize two external hard drives: rsync -avh -progress / media / user / disco1 / / media / user / disco2 / transfer only the changes and show a real-time progress bar.
Examples of remote use (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 remote shell and -z activates compression. To download a backup to the local team, reverse source and destination: rsync -avz -e ssh user @ example.com: / var / www / back-up / / home / user / back-up /. If you need to run without human intervention, for example in a cron, use SSH keys without password phrase or ssh-agent for secure authentication. In addition, -bwlimit = 1000 limits the transfer to ~1 MB / s, avoiding saturation of the network in peak hours.
Incremental support with -link-des t
An advanced technique to save space is to use incremental backups with hard links by -link-dest. A full copy (base) is maintained and, on each new backup, Rsync links the files to the previous copy without change, copying only the modified ones. For example, if the full copy is in / mnt / backup / full, the incremental of the following day is created with: rsync -av -delete -link-st = / mnt / backup / full / home / user / / mnt / backup / incremental. Here -delete from the incremental remove files that no longer exist in the origin and -link-dest points to the base, so that identical files are linked instead of copied. Thus, the incremental directory only occupies the space of the real changes, but when it is listed it is seen as a complete copy. This strategy allows to maintain several restoration points using little additional space.
Tips and good practices
- Always try -n or -dry-run before running commands that delete or overwrite files.
- Use absolute routes to avoid ambiguities, especially in cron scripts.
- Keep the Rsync version up to date; recent versions include security and performance improvements.
- Combine Rsync with monitoring tools such as inotify to create real-time synchronization when needed.
- Document your backup scripts and review them regularly to ensure that they continue to comply with data retention policies.
Conclusion
Rsync has been consolidated as an indispensable tool in the Linux ecosystem thanks to its efficiency, flexibility and low resource consumption. Whether you need to maintain a mirror of a website, create incremental backup or synchronize large data volumes between servers, Rsync offers a reliable solution that fits almost any scenario. By mastering your most powerful options and following the best practices described in this article, you can reduce transfer times, save storage space and ensure the integrity of your data. We invite you to experiment with the examples provided and to adapt them to your specific needs; you will soon discover why Rsync is the preferred choice of administrators and developers worldwide.


