· Tutorial ·

Synchronizing local and remote directories with rsync

In this guide we will explain how to use rsync to improve productivity on Linux based systems.

What is rsync?

Rsync is a utility that allows you to efficiently transfer and synchronize files or directories between a local machine or remote server.

Basic syntax

Basic syntax in local environment

The basic syntax for rsync in local environments is as follows:

rsync [optional modifiers] [source] [destination]

Basic syntax on remote server

The basic syntax for rsync between a local environment to a remote environment is as follows:

rsync [optional modifiers] [source] [user]@[host]:[destination]

And vice versa:

rsync [optional modifiers] [user]@[host]:[source] [destination] ````.

Install rsync

Many Linux distributions include the rsync package preinstalled. In case it is not preinstalled you can do it manually with the following command:

  • Debian-based distributions (Ubuntu, etc):
apt-get install rsync
  • RPM based distributions (Fedora, CentOS, etc):
yum install rsync
  • MacOS:
bew install rsync

It's that easy! To verify that it has been installed you can get the version with the following command:

rsync -version

Examples of use

In this first example we create the source and destination directories. Next, we will create 5 files in the source directory and we will see the magic of rsync.

  1. First we create the two directories:
cd ~
mkdir source
mkdir destination
  1. We create 5 example files in the source directory:
touch original/file{1..5}
  1. Next we will synchronize the contents of the source directory with destination by using rsync:
rsync source/*destination

The * indicates to synchronize the entire contents of the directory

If files are added or modified in the source directory and the rsync command is run again, only the new and modified files will be synchronized, thus avoiding copying unnecessary files.

Most commonly used optional modifiers

The following are some of the optional modifiers that can be added to the use of rsync:

  • -a or --archive this option enables us: -rlptgoD , which means:
    • -r recursive
    • -l copy symbolic links as symbolic links.
    • -p preserve permissions.
    • -t preserve the modification/creation date of the file.
    • -g preserve the file group.
    • -o preserve the owner of the files.
    • -D preserve device files and preserve special files.
  • -v or --verbose to display process information.
  • -e or --rsh is used to specify a remote shell, for example, if you want to rsync between servers.
  • --delete deletes files in the destination directory if they do not exist in the source directory.
  • -z or --compress compresses the data during the transfer.

Examples of use with modifiers

In this first example we will synchronize the contents of the /var/log folder on the 10.0.0.11 server to the /tmp folder on our server using the SSH shell.

rsync -avz -e 'ssh' [email protected]:/var/log /var/log /tmp/log

Next we will carry out the same operation, but in this case with the --delete tag, so that the files in destiny that do not exist in origin are erased:

rsync -avz --delete -e 'ssh' [email protected]:/var/log /var/log /tmp/log

Conclusions

In conclusion with rsync you can transmit file transfers over network connections or local directories. Its flexibility makes it a very good choice for many file-level operations.

A good command of rsync allows you to design complex backup operations and gain control over how and what is transferred.

i