Rsync Command in Linux with Examples
Rsync is a command-line application that allows you to synchronize files and directories between two sites using a remote shell or a remote Rsync daemon. It allows for quick incremental file transfer by just sending the differences between the source and destination files.
Rsync may be used to mirror data, create incremental backups, copy files across computers, and replace the commands SCP, sftp, and cp.
This article uses practical examples and extensive descriptions of the most popular sync settings to demonstrate using rsync.
Getting Rsync to Work
Most Linux distributions and macOS come with the rsync software pre-installed. If your system doesn't already have rsync installed, you may do so using your distribution's package manager.
Install Rsync on Ubuntu and Debian
sudo apt install rsync
Install Rsync on CentOS and Fedora
sudo yum install rsync
Syntax of the Rsync Command
Let's go through the basics of the rsync command before we can utilize it.
The utility expressions for rsync are as follows:
rsync (local to local) DEST... [OPTION]... [SRC]...
rsync from local to remote [USER@]... [OPTION]... [SRC]... HOST: DEST
rsync (remote to local) [USER@]... [OPTION]... [DEST] HOST:SRC...
SRC stands for the source directory.
DEST is the abbreviation for "destination directory."
USER - Username for remote access.
HOST - Hostname or IP address of a remote computer.
The command rsync has a variety of arguments that govern how it works. The most often used options are: -a, -b, and -c. —the archive is the archive mode, which is the same as -rlptgoD. This option instructs rsync to sync directories recursively, transfer special and block devices, preserve symbolic links, modification times, groups, ownership, and permissions, and sync directories.
—compress, -z This option instructs rsync to compress data before sending it to the destination system. Only use this option if the distant machine's connection is sluggish.
- -P, which is the same as —partial. —progress. rsync displays a progress indicator throughout the transfer and maintains the partly transferred files when this option is selected. It comes in very handy when transmitting huge files over sluggish or unpredictable network connections.
- —delete. Rsync removes any unnecessary files from the destination directory when this option is selected. It may be used for mirroring.
- -q stands for —quiet. If you wish to hide non-error warnings, choose this option.
- -e. You may choose a different remote shell using this option. Rsync is set up to utilize ssh by default.
Use of Rsync at a Basic Level
Rsync's most simple use case is to transfer a single file from one local place to another. Here's an illustration:
/tmp/rsync -a /opt/filename.zip
A user must execute the command with read access on the source and write permissions on the destination.
The file is copied using the current name when the filename is not specified in the destination location. If you wish to save the file under a different name, change the destination portion to include the new name:
/opt/filename.zip /tmp/newfilename.zip rsync -a
When it comes to syncing folders, sync shines. The following is an example of how to make a local backup of website files:
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 noticing 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 includes a trailing slash. rsync duplicates the source directory within the destination directory when the terminating slash is omitted.
Syncing Data from/to a Remote Machine using rsync
When transferring data over the internet, rsync must be installed on both the source and destination machines. The latest sync versions utilize SSH as the default remote shell.
We'll move a directory from a local system to a distant machine in the following example:
remote user@remote host or ip:/opt/media/ rsync -a /opt/media/
If you haven't set up a passwordless SSH login to the remote system, you'll be prompted for the user password.
Use the distant location as a source to transmit data from a remote to a local machine:
remote user@remote host or ip:/opt/media/ rsync
-a remote user@remote host or ip:/opt/media/
If SSH on the remote host is listening on a port other than 22 by default, use the -e option to specify the port:
remote user@remote host or ip:/opt/media/ rsync -a -e "ssh -p 2322" /opt/media/
It's best to execute the rsync command within a screen session or use the -P option when transferring large quantities of data:
remote user@remote host or ip:/opt/media/ rsync -a -P /opt/media/
Files and directories should be excluded.
To exclude files and folders, you have two alternatives. The first approach uses the —exclude parameter on the command line and specifies the files and folders you wish to exclude.
When excluding files or folders, you must use relative paths to the source location.
The node modules and tmp folders are shown to be excluded in the following example:
rsync -a /src directory/ /DST directory/ —exclude=node modules —exclude=tmp
The second method is to specify the files and directories you wish to exclude in a file using the —exclude-from option.
exclude-file.txt node modules tmp rsync -a —exclude-from='/exclude-file.txt' /src directory/ /dst directory/
Conclusion
We've taught you to copy and synchronize files and directories using Rsync. The Rsync User's Manual page has a lot more information about Rsync.