What is rsync and how to use it?
Data synchronisation is a crucial aspect of managing files and folders across different systems or locations. Among the myriad of tools available for this purpose, rsync stands out for its efficiency, flexibility, and robustness. In this tutorial, we’ll delve into the fundamentals of rsync and explore how to use it effectively for synchronizing data between local and remote systems.
What is rsync?
rsync is a powerful command-line utility for efficiently transferring and synchronizing files and directories between different systems. It’s designed to minimize data transfer by only copying the parts of files that have changed, which makes it ideal for syncing large volumes of data over networks.
Installation
- Linux/Unix:
rsyncis typically pre-installed on most Linux and Unix distributions. If not, you can install it using your distribution’s package manager (e.g.,aptfor Ubuntu,yumfor CentOS). - macOS:
rsyncis included with macOS by default. - Windows: For Windows users,
rsynccan be installed using third-party packages such as Cygwin, or by using the Windows Subsystem for Linux (WSL)
Basic Usage
The basic syntax of rsync is as follows:
rsync [options] source destination- source: Specifies the source directory or file you want to synchronize.
- destination: Specifies the target directory where you want to synchronize the data.
Example
rsync -avz /path/to/source/ user@remote_host:/path/to/destination/
Key Options
- -a, –archive: Archive mode, which preserves permissions, ownership, timestamps, and recursive copying.
- -v, –verbose: Verbose output, providing detailed information about the sync process.
- -z, –compress: Enables compression during data transfer, reducing bandwidth usage.
- -n, –dry-run: Performs a trial run without making any changes, useful for previewing sync operations.
- –delete: Deletes extraneous files from the destination directory. Use with caution.
Advanced Usage
- Remote Synchronization:
rsynccan synchronize files between local and remote systems using SSH:
rsync -avz -e ssh /path/to/source/ user@remote_host:/path/to/destination/
--exclude option:rsync -avz --exclude='*.log' /path/to/source/ user@remote_host:/path/to/destination/
rsync allows using include and exclude patterns for finer control over synchronization:rsync -avz --include='*.txt' --exclude='*' /path/to/source/ user@remote_host:/path/to/destination/