Introduction
rsync
is a command-line tool that uses a remote shell or a remote Rsync daemon to synchronize files and directories across two sites. By only delivering the differences between the source and destination files, it enables rapid incremental file transfer.
rsync
can be used to mirror data, create incremental backups, copy files between computers, and replace the commands scp
, sftp
, and cp
.
This tutorial uses practical examples and extensive descriptions of the most common rsync
settings to demonstrate how to use it. We will also address a few FAQs on rsync command in Linux.
Installing Rsync
The rsync
utility comes pre-installed on most Linux distributions and macOS. If rsync
isn't currently installed on your system, you can install it using your distribution's package manager.
Rsync
can be installed on Ubuntu and Debian systems.
Install Rsync on Ubuntu and Debian
sudo apt install rsync
Install Rsync on CentOS and Fedora
sudo yum install rsync
Rsync Command Syntax
Let's go through the basics of the rsync
command before we get into how to use it.
The utility expressions for rsync
are as follows:
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]
OPTION
- Thersync
options.SRC
- Source directory.DEST
- Destination directory.USER
- Remote Username.HOST
- The remote computer's hostname or IP address.
The command rsync
has a variety of arguments that govern how it works. The following are the most popular choices:
-a
,--archive
, archive mode, which is the same as-rlptgoD
. This option instructsrsync
to recursively sync directories, transfer special and block devices, preserve symbolic links, modification times, groups, ownership, and permissions, and sync directories recursively.--compress
,-z
this option instructsrsync
to compress data before sending it to the destination machine. Only use this option if the distant machine's connection is slow.-P
, which is the same as--partial --progress
.rsync
displays a progress indicator throughout the transfer and keeps the partially transferred files when this option is selected. When transmitting large files over slow or unpredictable network connections, it comes in very handy.--delete
, when this option is selected,rsync
removes any unnecessary files from the destination directory. It can be used for mirroring.-q
, stands for--quiet
. If you want to hide non-error warnings, select this option.-e
, you can select a different remote shell with this option.rsync
is configured to use ssh by default.
Basic Rsync Usage
Rsync's most basic use case is to copy a single file from one local place to another. Here's an illustration:
rsync -a /opt/filename.zip /tmp/
The command must be run by a user with read access on the source and write permissions on the destination.
When the filename is not specified in the destination location, the file is copied using the current name. If you want to save the file under a different name, change the destination portion to include the new name:
rsync -a /opt/filename.zip /tmp/newfilename.zip
When it comes to syncing directories, rsync
really shines.
rsync -a /var/www/domain.com/public_html/ /var/www/domain.com/public_html_backup/
rsync
will construct the destination directory if it does not already exist.
It's worth mentioning that rsync
treats source folders with a trailing slash (/
) differently. The command will only copy the directory contents to the target directory if the source directory has a trailing slash. rsync
duplicates the source directory inside the destination directory when the terminating slash is omitted.
Syncing Data from/to a Remote Machine using rsync
rsync
must be installed on both the source and destination machines for transferring data over the internet. The latest rsync
versions are set to use SSH as the default remote shell.
We'll move a directory from a local machine to a distant machine in the following example:
rsync -a /opt/media/ remote_user@remote_host_or_ip:/opt/media/
To send data from a remote machine to a local machine, use the distant location as a source:
rsync -a remote_user@remote_host_or_ip:/opt/media/ /opt/media/
To send data from a remote machine to a local machine, use a distant location as a source:
rsync -a -e "ssh -p 2322" /opt/media/ remote_user@remote_host_or_ip:/opt/media/
It's best to run the rsync
command inside a screen session or use the -P
option when transferring big amounts of data:
rsync -a -P remote_user@remote_host_or_ip:/opt/media/ /opt/media/
Exclude Files and Directories
You have two options for excluding files and folders. The first approach is to use the --exclude
parameter on the command line and specify the files and folders you want to exclude.
You must use relative paths to the source location when excluding files or directories.
The node_modules
and tmp
directories are shown to be excluded in the following example:
rsync -a --exclude=node_modules --exclude=tmp /src_directory/ /dst_directory/
The second method is to specify the files and directories you want to exclude in a file with the --exclude-from
option.
rsync -a --exclude-from='/exclude-file.txt' /src_directory/ /dst_directory/
node_modules
tmp
FAQs on rsync command in Linux
How does rsync differ from regular file copy commands?
Unlike regular file copy commands, rsync only transfers the differences between source and destination files, minimizing bandwidth usage and speeding up subsequent sync operations.
How do I use rsync to copy files and directories?
To use rsync, specify the source and destination paths along with optional arguments like -avz
for archive mode, verbosity, and compression. For example, rsync -avz source/ destination/
copies files from the source directory to the destination directory.
Can rsync be used for remote file synchronization?
Yes, rsync can synchronize files between local and remote systems using SSH. Simply prefix the remote path with user@host:
. For example, rsync -avz source/ user@remote:/destination/
copies files from local source to the remote system.
Does rsync support resuming interrupted transfers?
Yes, rsync supports resuming interrupted transfers. If a transfer is interrupted, you can rerun the rsync command with the same source and destination to resume where it left off.
Can I exclude specific files or directories during synchronization?
Yes, you can exclude files or directories using the --exclude
option followed by the pattern to be excluded. Multiple exclusions can be specified by repeating the --exclude
option.
Can rsync preserve file permissions and attributes?
Yes, rsync can preserve file permissions, ownership, timestamps, and other attributes using the -a
or --archive
option. It ensures that all relevant metadata is retained during the synchronization process.
Is it possible to schedule automatic rsync backups?
Yes, you can schedule automatic backups using tools like cron. Create a cron job with the rsync command and desired options, specifying the frequency at which the backup should be executed.
Conclusion
We've taught you how to copy and synchronize files and directories with Rsync. More information regarding Rsync can be found on the Rsync User's Manual page.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.