Understanding ‘rsync’ Command in Linux with Examples.

Understanding rsync Command in Linux with Examples

In this tutorial, we will understand the ‘rsync’ command in Linux, Along with that, we will understand its options.

Rsync is very fast for synchronizing files and directories between two remote servers through a secured remote shell. It provides fast incremental file transfer. It checks the destination server if the same files or directory exists, then it will only transfer the incremental data or the additional data.

rsync used for incremental backups, copying the files and directories between servers. It works as the replacement, or we can say successor of scpsftp
and cpcommand.

Installing Rsync

In most Linux distributions rsync command is already installed. But in case If your Linux system doesn’t have rsync command installed, use the following command to install
rsync as per your Linux Distribution.

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 review the syntax of rsync command.

The rsync utility expressions take the following form.

Local to Local:  rsync [OPTION]... [SRC]... DEST
Local to Remote: rsync [OPTION]... [SRC]... [[email protected]]HOST:DEST
Remote to Local: rsync [OPTION]... [[email protected]]HOST:SRC... [DEST]
  • OPTION — The rsync options or attributes
  • SRC — Source directory or file.
  • DEST — Destination directory.
  • USER — Put remote username.
  • HOST — Remote hostname or IP Address.

rsync provides a variety of options through them, we can control how command behaves. Most commonly used options :

  • -a (--archive, archive mode, same as -rlptgoD) — It syncs directories recursively. Also, it preserves symbolic links, modification times, groups, ownership, and permissions.
  • -P (--partial , --progress) —  Very useful option while transferring the large data over a slow or unstable connection. Also shows a progress bar during the data transfer and keeps the partially transferred files.
  • -z (--compress)- used to transfer the data in compressed form. This option is only used if the connection of the remote machine is slow.
  • --delete — Deletes extraneous files from the destination location. It is mostly useful in mirroring.
  • -q  (--quiet) — used to suppress non-error messages.
  • -e — can be used to choose different remote shell. The default shell used by the rsync is ssh.

Basic rsync Usage

  • The most basic use of rsync the command is to copy a single file from one local to another local. /Download/filename.zip Represent the source file, /home/ represent the destination.
rsync -a /Download/filename.zip /home/

If a non-root user running this command then that user must have read permission on the source file and write permission on the destination.

  • Now, If we want to copy the file or directory with a different name to the destination, then we have to mention that name with the command:
rsync -a /Download/filename.zip /home/newfilename.zip
  • For example, In the below command we will see how to create a local backup of a website files:
rsync -a /var/www/Linuxpanda/public_html/ /var/www/Linuxpanda/backup_public_html/

If the destination directory doesn’t exist, rsync will create it.

Now, If the source directory has / at the end, the command will copy only the directory contents to the destination directory. If the source directory doesn’t have / at the end, the command will copy the source directory inside the destination directory.

Using rsync to Sync Data from/to a remote Machine

While using the rsync command to transfer the data remotely, then rsync the command must be installed on both source and destination servers. By default, rsync command uses ssh as the remote shell.

  • In the below example, we will transfer a directory from a local machine to a remote server
rsync -a /opt/media/ [email protected]_host_or_ip_address:/opt/media/

In the above command, we have mentioned the source just after the -a the argument, It indicates that we are transferring a file.

  • While transferring the data from a remote server to a local system, use a remote server as a source:
rsync -a [email protected]_host_or_ip_address:/home/media/ /home/media/

In the above command we have mentioned the source just after : , it indicates that
we are requesting a file.

  • If the destination server’s port is not listening on 22 than we have to mention the listening port in the command with -e option:
rsync -a -e "ssh -p 2322" /home/Document/ [email protected]_host_or_ip-address:/home/Document/

Note:- Always run the rsync command in screen if you are transferring a large amount of data or use -p option:

rsync -a -P [email protected]_host_or_ip_address:/home/Document/ /home/Document/

Exclude Files and Directories

While transferring the data from one server to another server, sometimes we need to exclude some files or directories. We have the following method to exclude the file or directory:-

  • The option  --exclude argument is used with rsync command and mention the files and directories you want to exclude:

In the below example, we will exclude a file filename.txt and a directory tester:

rsync -a --exclude=tester --exclude=filename.txt /home/linuxpanda [email protected]:/home/

Conclusion

In this tutorial, we have learned how to use rsync commands to copy and synchronize the files and directories on one server to another server. For more detailed information about the rsync command, write man rsync in the terminal.

Read Also: Find and Replace in Vim Editor

If you guys have any queries related to this Understanding ‘rsync’ Command in the Linux tutorial, Let me know in the comments.