Rsync command for data transfer and sync

Rsync (Remote Sync) is a fast and versatile command-line tool for synchronizing files and folders between two locations via a remote shell, or to a remote rsync daemon. Rsync allows incremental file transfers by only transferring the differences between source and destination locations.

Rsync can be used to mirror data, additional backups, and file copies between systems instead of commands scp, sftpAnd and cp For daily use.

In this tutorial, we’ll show you how to use rsync with practical examples and step-by-step explanations of the most popular rsync options.

Install Rsync

The rsync utility comes pre-installed on most Linux distributions and on Mac OSX. You can check if Rsync is installed on your system by typing:

rsync --version
rsync  version 3.1.2  protocol version 31

If you do not have rsync installed on your system, you can easily install it using your distribution package manager.

Install Rsync on Ubuntu and Debian

sudo apt install rsync

Install Rsync on CentOS and Fedora

sudo yum install rsync

Rsync command syntax

Before discussing how to use the rsync command, let’s start by reviewing the basic syntax.

The rsync utility expression takes the following form:

Lokal ke Lokal:  rsync [OPTION]... SRC [SRC]... DEST
Lokal ke Remote: rsync [OPTION]... SRC [SRC]... [[email protected]]HOST:DEST
Remote ke Lokal: rsync [OPTION]... [[email protected]]HOST:SRC [DEST]
  • OPTION – rsync options
  • SRC – Source directory
  • DEST – Destination directly
  • USER – Remote username
  • HOST – remote host

Rsync provides a number of options that control every aspect of its behavior. The most used options are:

  • -a, --archive, Equal -rlptgoD. The commonly used options will sync directories frequently, move specific devices, preserve symbolic links, edit times, groups, ownership, and permissions.
  • -z, --compress This option will force rsync to compress data as it is sent to the destination device. Use this option only if the connection to the remote device is slow.
  • -P, --partial --progress– This option will tell rsync to show the progress bar while transferring and save part of the transferred file. This command is useful when transferring large files over slow or unstable network connections.
  • --delete When using this option, rsync will delete foreign files from the destination site. This command is useful for reflection.
  • -q, --quiet Use this option if you only want to suppress the output message and an error message.
  • -e. This option allows you to select a different remote jacket. By default, Rsync is configured to use ssh.
See also  Git for Beginner

Primary use for Rsync

To copy a file from one local location to another, you must run the following command:

rsync -a /opt/ /tmp/

Removes the file name from the destination location, then the file name is the same as the name when moving. If you want to save the file with a different name, then you need to specify a new name:

rsync -a /opt/ /tmp/

In the example below we are making a local backup of website files:

rsync -a /var/www/ /var/www/

If the destination directory does not exist then rsync will create it.

It is worth noting that rsync provides different treatment of source evidence with a slash /. If you add a slash to the source directory, only the directory contents are copied to the destination directory.

When removing the slash, rsync will copy the source directory to the destination directory.

How to use Rsync for remote transfers

When rsync is used for remote transfers, it must also be installed on the source and destination devices. Newer versions of rsync are configured to use SSH as the default remote shell.

In the following example, we’re moving a directory from a local to a remote machine:

rsync -a /opt/media/ [email protected]_host_or_ip:/opt/media/

If you have not set an SSH login without password for the remote machine, you will be prompted for the password.

If you want to transfer data from a remote machine to a local machine, then you need to use the remote site as the source:

rsync -a [email protected]_host_atau_ip:/opt/media/ /opt/media/

If the SSH on the remote host is listening on a port other than the default 22, then you can specify the port using the argument -e:

rsync -a -e "ssh -p 2322" /opt/media/ [email protected]_host_or_ip:/opt/media/

When you are transferring large amounts of data, it is recommended that you run the rsync command in-screen or tmux session or use the options to make it easier -P.

Exclude files and directories

When excluding files or directories, you need to use their relative paths. There are two options to exclude files and directories when using rsync.

See also  A tutorial on using the timeout command in Linux

The first option is to use arguments --exclude And specify the files and directories you want to exclude in the command line.

In the next example we will exclude evidence node_modules And and tmp It is located inside the directory src

rsync -a --exclude=node_modules --exclude=tmp /src/ /dst_directory/

The second option is to use arguments --exclude-from Select the files and directories you want to exclude in a file.

rsync -a --exclude-from='/daftar-exclude.txt' /src/ /dst_directory/

Let’s look at the list -lusione.txt file with the cat command:

cat exclude-file.txt


In this tutorial, you learned how to use Rsync to copy and synchronize files and directories. There is a lot to learn about Rsync on the Rsync User’s Guide page.

Source link

Originally posted 2020-11-19 15:33:28.

About wahyuway

Check Also

How To Install PHP 8.0 on Amazon Linux 2

PHP 8.0 is a major release of the PHP programming Language mostly used in the Development …

Leave a Reply