Help Docs Server Administration Linux Server Administration File and Directory Management in Linux Using rsync to Sync Local and Remote Systems

Using rsync to Sync Local and Remote Systems

Rsync is a utility commonly found on Linux operating systems and is a remote and local file synchronization tool. rsync stands for remote sync. The rsync algorithm is used to minimize network usage by only moving portions of files that have changed. This tutorial will take you through the following: basic rsync usage, remote transfers, alternate SSH ports.

rsync is a utility commonly found on Linux operating systems and is a remote and local file synchronization tool. rsync stands for remote sync. The rsync algorithm is used to minimize network usage by only moving portions of files that have changed.

This tutorial will take you through the following:

Basic rsync Usage

To begin, we will create two directories and some test files with the following commands:

cd ~
mkdir liquid
mkdir web
touch liquid/file{1..100}

Now we have a directory called liquid with 100 empty files in it.

You can see it with this command:

ls liquid
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

There is also still an empty directory called web.

You can use rsync to copy all the files from /liquid over to /web:

rsync -r liquid/ web

The -r command means recursive, which is needed for directory syncing.

You could also use the -a flag instead of the -r flag:

rsync -a liquid/ web

-a stands for archive, it syncs recursively and preservers symbolic links, special and device files, modification times, group, owner and permissions. It is more commonly used than -r and is usually what you should use.

Note:

You may have noticed the trailing slash (/) at the end of the first argument in the above example. This is necessary to mean “the contents of liquid”. Without using the trailing slash, the directory liquid would be placed within the directory web.

Without the trailing slash, the hierarchy will look like:

-/web/liquid/[files]

Make sure you double-check your arguments before executing your rsync command.

You can use the -n or –dry-run command to double-check your output, to get the appropriate output, use the -v (-verbose) flag together with -a and -n. It will look like this:

rsync -anv liquid/ web
sending incremental file list
./
file1
file10
file100
file11
file12
file13
file14
file15
file16
file17
file18
. . .

In comparison, removing the trailing slash will result in this output:

rsync -anv liquid web
sending incremental file list
liquid/
liquid/file1
liquid/file10
liquid/file100
liquid/file11
liquid/file12
liquid/file13
liquid/file14
liquid/file15
liquid/file16
liquid/file17
liquid/file18
. . .

You can see here how the directory itself was transferred.

Remote Transfers Made Simple

The true power of rsync is in its ability to perform remote transfers. Remote-to-local or local-to-remote file transfers can be made easier with the use of SSH keys. With SSH keys set up on both remote and local servers, synchronization can be scripted effortlessly and without having to enter a password every time. Find out how to set-up an SSH key in our article Setting Up and Using SSH Keys.

If you’ve used scp before, the syntax for remote transfers is similar:

rsync [flags] [local path] [user] @ [remote server] : {remote path}

As an example, an rsync using this syntax would look like the following:

rsync -a ~/liquid root@remote_host:/web

This is called a “push” operation because it pushes the directory from the local system to the remote system.

The opposite operation is “pull”. It is used to sync the remote directory to a local system. If the directory liquid were on a remote system, the syntax would be:

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

The source is always the first argument, the destination is always the second.

Note:

The colon (:) between the remote server and the remote path is required for both push and pull operations.

Alternate SSH Ports

If you have changed the SSH port on your server, you will need to tell rsync to use the new port number. If you want to learn how to change the SSH port, see our article Changing Your SSH Port with the Command Line.

Example with a normal SSH port:

rsync -azh /local/path/file user@host.com:/remote/path/file

Example with alternate SSH port:

rsync -azh /local/path/file -e 'ssh -p 22334' user@host.com:/remote/path/file

More about rsync

Mastering rsync allows you to design complex backup operations and have fine-tuned control over what is transferred and how. The flexibility of rsync makes a good option for many different file-level operations. For more information about flags and rsync commands, see our article, Exploring the rsync Utility.

Interested in hosting with Liquid Web? Explore our range of VPS hosting, dedicated servers, and cloud hosting solutions now.

Was this article helpful?