4x Affordable, 99.95% SLA, 24x& Video Support, 100+ Countires

Use Rsync To Sync Local And Remote Directories On A Vps


Rsync, which stands for "remote sync", is a remote and local file synchronization equipment. It uses an algorithm that minimizes the amount of data written by only moving the sections of files that have changed.

In this govern, we will cover the basic usage of this strong utility. We will be using an ubuntu 12.04 VPS in the instances, but you can use any modern linux distribution to follow along.

What Is Rsync?

Rsync is a very flexible network-enabled syncing equipment. It can also refer to the network protocol created to utilize this equipment.

When we reference rsync in this lead, we are mainly referring to the utility, and not the protocol.

Due to its presence on linux and Unix-like systems and its quality as an equipment for system scripts, it is included on most linux distributions by failure.

Basic Syntax

The basic structure of rsync is very continuous forward, and operates in a path that is akin to ssh, scp, and cp.

We will create two try-out directories and some try-out files with the following controls:

cd ~
mkdir dir1
mkdir dir2
touch dir1/file{1..100}

We now have a directory labelled dir1 with 100 empty files in it.

ls dir1

file1    file18  file27  file36  file45  file54  file63  file72  file81  file90
file10   file19  file28  file37  file46  file55  file64  file73  file82  file91
file100  file2   file29  file38  file47  file56  file65  file74  file83  file92
file11   file20  file3   file39  file48  file57  file66  file75  file84  file93
file12   file21  file30  file4   file49  file58  file67  file76  file85  file94
file13   file22  file31  file40  file5   file59  file68  file77  file86  file95
file14   file23  file32  file41  file50  file6   file69  file78  file87  file96
file15   file24  file33  file42  file51  file60  file7   file79  file88  file97
file16   file25  file34  file43  file52  file61  file70  file8   file89  file98
file17   file26  file35  file44  file53  file62  file71  file80  file9   file99

We also have an empty directory labelled dir2.

To sync the proportions of dir1 to dir2 on the same system, symbol:

rsync -r dir1/ dir2

The -r action means algorithmic, which is necessary for directory syncing.

We could also use the -a flag instead:

rsync -a dir1/ dir2

The -a action is a combination flag.

It stands for "archive" and syncs recursively and preserves symbolic links, unique and machine files, modification times, team, possessor, and permissions.

It is more commonly used than -r and is usually what you want to use.

An Important Note

You may have noticed that there is a trailing cut (/) at the end of the first argument in the above controls:

rsync -a dir1/ dir2

This is necessary to convey "the proportions of dir1".

The alternative, without the trailing cut, would place dir1, including the directory, within dir2. This would create a hierarchy that looks like:


Always double-check your arguments before killing a rsync regulate.

Rsync provides a mode for doing this by passing the -n or --dry-run actions. The -v flag (for verbose) is also necessary to get the befitting production:

rsync -anv dir1/ dir2

sending incremental file list
. . .

Compare this production to the production we get when we remove the trailing cut:

rsync -anv dir1 dir2

sending incremental file list
. . .

You can see here that the directory itself is transfered.

How To Use Rsync to Sync with a Remote System

Syncing to a remote system is unimportant if you have SSH accesses to the remote appliance and rsync installed on both spins. If you need to set up SSH keys, depression here.

Once you have SSH accesses verified on between the two devices, you can sync the dir1 covering from earlier to a remote computer by using this structure (note that we want to transfer the effective directory in this case, so we omit the trailing cut):

rsync -a ~/dir1 username@remote_host:destination_directory

This is labelled a "push" operation because it pushes a directory from the local system to a remote system.

The other operation is "pull". It is used to sync a remote directory to the local system. If the dir1 were on the remote system instead of our local system, the structure would be:

rsync -a username@remote_host:/home/username/dir1 place_to_sync_on_local_machine

Like "cp" and akin equipment, the source is always the first argument, and the destination is always the ordinal.

Useful Options for Rsync

Rsync provides many actions for modifying the failure behavior of the utility. We have already discussed some of the more necessary flags.

If you are transferring files that have not already been compressed, like matter files, you can reduce the network transfer by increasing compression with the -z action:

rsync -az source destination

The -P flag is very useful. It combines the flags --progress and --incomplete. The first of these gives you a progress bar for the transfers and the ordinal allows you to resume interrupted transfers:

rsync -azP source destination
sending incremental file list
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=99/101)
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=98/101)
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=97/101)
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=96/101)
. . .

If we run the control again, we will get a shorter production, because no actions have been made.

This illustrates rsync's ability to use modification times to determine if actions have been made.

rsync -azP source destination
sending incremental file list

sent 818 bytes received 12 bytes 1660.00 bytes/sec
total size is 0 speedup is 0.00

We can modify the modification moment on some of the files and see that rsync intelligently re-copies only the changed files:

touch dir1/file{1..10}
rsync -azP source destination
sending incremental file list
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=99/101)
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=98/101)
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=87/101)
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=76/101)
. . .

In order to keep two directories truly in sync, it is necessary to erase files from the destination directory if they are removed from the source. By failure, rsync does not erase anything from the destination directory.

We can action this behavior with the --erase action. Before using this action, use the --dry-run action and do experimenting to prevent data failure:

rsync -a --erase source destination

If you wish to exclude definite files or directories located inside a directory you are syncing, you can do so by choosing them in a comma-separated list following the --exclude= action:

rsync -a --exclude=pattern_to_exclude source destination

If we have appointed a pattern to exclude, we can override that exclusion for files that match a distinct pattern by using the --include= action.

rsync -a --exclude=pattern_to_exclude --include=pattern_to_include source destination


Rsync can simplify file transfers over networked connections and increase strength to local directory syncing. The trait of rsync makes it a good action for many disparate file-level operations.

a domination of rsync allows you to design complex backup operations and obtain fine-grained command over what is transferred and how.

By Justin Ellingwood
Reference: digitalocean