rsync is a popular command-line utility for efficiently copying and synchronizing files and directories between two locations, typically over a network. One of its key features is its ability to perform incremental transfers, which means it only copies the parts of files that have changed, reducing the amount of data transferred and making synchronization faster.
Here’s how rsync achieves incremental transfers:
- Checksum Comparison: rsync compares the checksums (hashes) of the files in the source and destination directories. This allows it to identify which files have changed quickly.
- Partial File Transfer: Instead of copying an entire file, rsync copies only the parts of a file that have changed. It does this by dividing files into small blocks and comparing these blocks between the source and destination. It then transfers only the differing blocks.
- File Deletion Handling: rsync can also detect when files have been deleted in the source directory. By default, it won’t delete files in the destination directory to mirror the source, but you can use the –delete option to enable this behaviour.
Here’s a basic usage example of rsync for incremental transfers:
rsync -avz --delete source_directory/ destination_directory/
- -a (or –archive): This option enables archive mode, which preserves permissions, ownership, timestamps, and other file attributes during synchronization.
- -v (or –verbose): This option enables verbose mode, showing detailed information about the synchronization process.
- -z (or –compress): This option compresses data during transfer, which can be helpful when syncing over a network to reduce bandwidth usage.
- –delete: This option will delete files in the destination directory that no longer exist in the source directory, ensuring that the two directories stay in sync.
- source_directory/ and destination_directory/ are the source and destination directories you want to synchronize. The trailing slashes are essential to ensure that rsync syncs the contents of the directories, not the directories themselves.
Local and Remote examples using rsync for backup
Here are two examples of using rsync for incremental transfers in different scenarios:
Related: How to use dry run with rsync?
Example 1: Local Incremental Backup
Suppose you want to create an incremental backup of your local “my_documents” directory to an external hard drive mounted at “/mnt/backup_drive.”
rsync -avz --delete ~/my_documents/ /mnt/backup_drive/my_documents/
In this example:
-avz # enables archive mode, verbose output, and compression. --delete # ensures that files deleted in the source are also deleted in the backup. ~/my_documents/ # is the source directory. /mnt/backup_drive/my_documents/ # is the destination directory on the external drive.
This command will efficiently synchronize the “my_documents” directory to the backup location, copying only the changed parts of files and deleting files that no longer exist in the source.
Example 2: Remote Server Synchronization
Suppose you want to synchronize files from a remote server to your local machine over SSH. You have SSH access to the remote server and want to sync the “remote_documents” directory to your local “my_documents” directory.
sync -avz --delete user@remote_server:/path/to/remote_documents/ ~/my_documents/
-avz # enables archive mode, verbose output, and compression. --delete # ensures that files deleted on the remote server are also deleted locally. user@remote_server:/path/to/remote_documents/ # specifies the remote source directory. ~/my_documents/ # is the local destination directory.
This command will synchronize the “remote_documents” directory from the remote server to your local machine over SSH, performing incremental transfers to minimize data transfer and ensure both locations stay in sync.
These examples demonstrate how rsync can be used for local and remote synchronization with incremental transfers to update the destination based on changes in the source efficiently.