How to Synchronize Systems With rsync

How to Synchronize Systems with rsync

CHUONG K. NGUYEN – BSc., MCSEx2, MCSAx2, MCP, MCTS, MCITP, CCNA

In this guide, I will demonstrate simple usages of rsync – a powerful tool to transfer and synchronize local and remote systems.

Basic rsync syntax

We are going to create two directory inside /tmp called source and destination respectively.

Now we will generate 30 files inside source in order to demonstrate our rsync usage. We will use the touch command in a for loop for this purpose.

Let’s see the files

Now let the action begin. We will use rsync to synchronize the source and the destination directories.

Let’s show what’s inside /tmp/destination.

Suppose that there are files as well as directories inside /tmp/source, rsync will not copy the directories; it just only copies the files. To demonstrate this, let’s clear contents inside /tmp/destination, then create a directory inside /tmp/source with a few files in it. Then we will demonstrate rsync.

After clearing out the destination, we create a child directory inside /tmp/source called childdir. Use a for loop to create 5 files inside childdir.

Now let’s sync between /tmp/source and /tmp/destination using the rsync command above. As you can see from the output, the directory was skipped.

Recursive Synching

In order to sync all contents (including files and child directories), we need to use the –r option (for recursive).

As you can see, childdir has been synched.

And the contents of /tmp/destination/childdir is exactly as the source.

The –r option is just one of the many options available to rsync. Later, we will show other flags the preserve permissions, ownerships, group membership, symlinks, etc… All of these flags can be incorporated and interchanged with the –a option (for archive).

Symlink Synch

Symlink are not synched as the following example illustrates. First, clear the contents of /tmp/destination.

Create a symlink that points to /tmp/source/myfile30 from /tmp/source/myfile31

Now list to see what’s under /tmp/source

Now let’s attempt to sync. We can see that the symlink is skipped.

In order to sync the symlink, we need to use the –a flag (archive). First, clear what is inside /tmp/destination.

As you can see this time the symlink has been archived as well. Let’s see what’s under /tmp/destination.

Synching file ownership information

Let’s see the ownership of /tmp/source. As you can see from the below, the owner is root.

Now let’s change that owner to frank.

Now, let’s clear out /tmp/destination and resync.

Let’s see what’s under /tmp/destination. We can see that the ownership information will be copied over too.

Note that I used the slash (/) at the end of /tmp/source/ to synch the directory itself and all contents inside of it instead of just synching the contents alone.

The –a (archive flag) used above consists of some flags such as –rltpgoD.

-r: Recursive

-l: Symlinks

-t: Timestamps

-p: Permission

-g: Group

-o: Ownership

-D: block/character devices

 

Additionally, the following flags are often used:

-h is used for human readable format of file sizes and

-v is to display feedback to users of what is happening during the transfer.

-stats is to display detailed results

-q is to quiet the output (for scripting reasons).

 

The following example illustrates –av flags.

Some Advanced options

-exclude: exclude files from transferring

-exclude-from: exclude files from a line-delimited file

-update: update destination only if the source has been changed recently

-delete: delete files in destination if source no longer exists.

Remote Transfer

We can also transfer from a local server to a remote server using the following command syntax:

rsync [flags] [local-path] [user]@[remote-server]:[remote-path]

For example, in this scenario, I have a local host with IP address of 192.168.1.7 and the remote host with IP address of 192.168.1.8.

You will be asked to accept the digital key (for the first time only) and enter a password for the user chuong. When you have done all that, the transfer will begin.

Let’s check it out on the remote host.

If the remote host has a non-standard SSH port, for instance 10022, then we should use:

That’s it!