Looking for a fast and efficient way to synchronize files and directories between two locations? Look no further than rsync. This versatile command-line utility can be used over a remote shell or with a remote Rsync daemon and provides lightning-fast incremental file transfer by only transferring the differences between the source and destination.
Whether you need to mirror data, create incremental backups, copy files between systems, or replace traditional commands like scp, sftp, and cp, rsync has got you covered.
In this article, we’ll explore practical examples and detailed explanations of the most common rsync options.
How to Install Rsync on Linux
The rsync tool comes pre-installed on the majority of Linux distributions and macOS. However, if you don’t have rsync on your system, you can easily install it through your distribution package manager.
How to Install Rsync on Ubuntu and Debian
To install rsync on Ubuntu and Debian, type the following command in the terminal:
sudo apt install rsync
How to Install Rsync on CentOS and Fedora
To install rsync on CentOS and Fedora, use the following command:
sudo yum install rsync
Introduction to Basic Rsync Command Syntax
Let’s begin by reviewing the basic syntax of the rsync command before diving into its usage.
The rsync utility expressions follow a specific format based on the transfer direction, as shown below:
Local to Local: rsync [OPTION]... [SRC]... DEST Local to Remote: rsync [OPTION]... [SRC]... [USER@]HOST:DEST Remote to Local: rsync [OPTION]... [USER@]HOST:SRC... [DEST]
The format has several syntaxes:
OPTION– The rsync options .
SRC– Source directory.
DEST– Destination directory.
USER– Remote username.
HOST– Remote hostname or IP Address.
There are several options available in rsync that can be used to control the behavior of the command. The most commonly used are:
--archiveoption in rsync stands for archive mode, which is essentially equivalent to the combination of
-rlptgoDoptions. By using this option, rsync will recursively synchronize directories, copy special and block devices, maintain symbolic links, and preserve modification times, groups, ownership, and permissions.
--compressforces rsync to compress the transmitted data as it is transferred to the destination machine. It’s advisable to enable this option solely if the network connection to the remote machine is slow.
-Pis the same as
--partial --progressin rsync. By using this option, a progress bar is displayed during file transfer, and partially transferred files are retained. This option is particularly helpful when transferring large files over an unreliable or slow network connection.
--deleteoption in rsync results in the removal of any additional files present in the destination location. This option is particularly handy for mirroring purposes.
--quietuse this option if you wish to silence non-error messages.
-e. You can use this option to choose a different remote shell. Rsync is configured to use ssh by default.
Basic usage of Rsync
The most basic way to use rsync is to copy a single file from one local location to another.
Simply run the command:
rsync -a /opt/filename.zip /tmp/
When running this command make sure you have read permissions on the source location and write permissions on the destination.
If you exclude the filename when specifying the destination location, the file will be duplicated with its existing name. But you can specify a new name for the file in the destination directory:
rsync -a /opt/filename.zip /tmp/newfilename.zip
One of the best parts of using rsync is you can synchronize directories, such as you can see below example on how to create a local backup of website files:
rsync -a /var/www/domain.com/public_html/ /var/www/domain.com/public_html_backup/
If the destination directory doesn’t exist, rsync will create it for you.
When using rsync, it’s important to note the significance of trailing slashes
(/) in source directories. If a source directory is specified with a trailing slash, rsync will only transfer the contents of the directory to the destination directory. However, if the trailing slash is omitted, rsync will copy the entire source directory into the destination directory. Understanding this distinction is crucial for effectively utilizing rsync in your file transfer operations.
How to Use
rsync to Sync Data from/to a remote Machine
For remote data transfer using rsync, installation of this tool is required on both the source and destination machines. With the latest rsync updates, SSH is set as the default remote shell, providing a secure and efficient transfer process.
To transfer a directory from a local to a remote machine, follow the command:
rsync -a /opt/media/ remote_user@remote_host_or_ip:/opt/media/
In case you haven’t established passwordless SSH access to the remote device, you need to provide the user password.
Use the remote location as a source when transferring data from a remote machine to a local one:
rsync -a remote_user@remote_host_or_ip:/opt/media/ /opt/media/
In case the SSH service on the remote host is not using the default port 22, utilize the
-e option to specify the port number it is listening on:
rsync -a -e "ssh -p 2322" /opt/media/ remote_user@remote_host_or_ip:/opt/media/
To ensure the smooth transfer of large data sets, it is advisable either to execute the rsync command within a screen session or utilize the
rsync -a -P remote_user@remote_host_or_ip:/opt/media/ /opt/media/
How to Exclude Files and Directories using Rsync
To exclude files and directories, you have two options. The first is to use the
--exclude argument on the command line and specify the relevant files and directories.
It is important to note that when excluding files or directories, you must use their relative paths to the source location.
For instance, the following example demonstrates how to exclude the
rsync -a --exclude=node_modules --exclude=tmp /src_directory/ /dst_directory/
The second option is to use the
--exclude-from feature and list the files and directories that should be excluded from a designated file:
rsync -a --exclude-from='/exclude-file.txt' /src_directory/ /dst_directory/
/exclude-file.txt node_modules tmp
We have demonstrated the use of Rsync for copying and syncing files and directories.
For further information about Rsync, visit the Rsync User’s Manual page.
Don’t hesitate to leave a comment if you have any inquiries.