Rsync (remote sync) is a tool for copying and synchronizing files between folders, disks, or computers. Its greatest advantage is intelligent copying: it transfers only the parts of files that have changed, making it much faster than regular copying for large amounts of data. That is precisely why it is popular for backups.
rsync -avh source_folder/ destination_folder/
How the Command Is Built
The basic form is simple: source, destination, and flags. The most common combination is -avh:
- a preserves all attributes (permissions, timestamps, symbolic links),
- v shows the progress of execution,
- h displays sizes in a human-readable format (KB, MB).
The Important Trailing Slash
The most common pitfall with rsync is the slash at the end of the source path. With it, the contents of the folder are copied; without it, the folder itself is copied. The difference is significant, so it is advisable to perform a test with the -n flag (dry run), which shows what would be transferred without actually executing anything.
rsync -avh documents/ backup/ # copies the contents of the documents folder rsync -avh documents backup/ # copies the documents folder as a whole rsync -avhn documents/ backup/ # dry run: only shows, without copying
Deleting Obsolete Files
By default, rsync does not delete files at the destination location that no longer exist at the source location. Using the --delete flag makes both folders completely identical — what disappears from the source also disappears from the destination. For backups, this flag is dangerous; for mirror copies, it is quite handy.
rsync -avh --delete documents/ backup/ # destination becomes a mirror of the source
Copying Over the Network
Rsync can also transfer over SSH to another computer or server. Only the changed part of each file is transferred. Precisely for this reason, extensive backups over the network are considerably faster than with regular copying.
rsync -avh documents/ borut@192.168.1.50:/backup/ rsync -avh borut@192.168.1.50:/backup/documents/ ./documents/
Excluding Files
With the --exclude flag, we skip what we do not want to copy. We can specify multiple patterns in a row; for a longer list, we use a file with the --exclude-from option.
rsync -avh --exclude='*.tmp' --exclude='cache/' documents/ backup/
Why rsync for Backups
Regular copying always rewrites everything from scratch. Rsync remembers what it has already transferred and, on the next run, transfers only the changes. For daily copies of the same data, this means minutes instead of hours — precisely why rsync is the basis of many scripts for automatic backups.
Due to its convenient syntax, rsync is a tool worth knowing by heart: one switch, one slash, and one path to a reliable copy.