Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Having your way with rsync

Sandra Henry-Stocker | March 9, 2016
Maybe you want the directories that you synchronize to be exact copies of each other or maybe you just don't. Let's dig a little more deeply into the rsync command and see if we can't find just the right mix of options for what you want to do.

When there's nothing to be done

The next time we run the rsync command, there is nothing to copy. Nothing has changed in the archive directory so nothing is changed on the original. Still we see some bytes going back and forth because the rsync processes still need to compare notes and determine if any files or file content needs to move.

orig$ rsync -av archive remhost:~unixdweeb
building file list ... done

sent 132 bytes  received 26 bytes  63.20 bytes/sec
total size is 1170  speedup is 7.41

Dealing with new content

When a new file shows up on the remote archive, we have to adjust our rsync command if we want our two archive directories to remain exactly the same. Our next rsync operation shows that's just what happens. By adding the --delete option, we tell rsync to delete any files on the destination system that don't exist on the source system.

orig$ ls archive
newfile  try1  tryme  tryme1  tryme2
orig$ rsync -av --delete archive remhost:~unixdweeb
building file list ... done
deleting archive/newfile

Notice the "deleting archive/newfile" message that appears in our verbose output. If you want your rsync operation to use the local system as the content authority and make sure that the remote copy looks exactly the same as the original one, --delete is the option to use.

Next, we see the bytes transferred statistic and a very modest speedup.

sent 136 bytes  received 26 bytes  64.80 bytes/sec
total size is 1170  speedup is 7.22

And, as you'd expect, the new file that somehow snuck into the remote system's archive directory is now gone.

dest$ ls archive
try1  tryme  tryme1  tryme2

In addition, were we to examine the files on both systems, the ownership and permissions would be the same because we are making use of the -a (archive) option that ensure this is the case.

To replicate or not

If, instead, we want to maintain whichever files in the two archive directories are the newest, there are options for that as well.

The --existing option tells rsync to only update files that already exist on the remote system and not to create new files. Notice in the example below that newfile2 is not replicated.

orig$ echo hello > archive/newfile2
orig$ rsync -av archive --existing sea-aveksa-1:~shs
building file list ... done

sent 163 bytes  received 26 bytes  126.00 bytes/sec
total size is 1176  speedup is 6.22

We might also want to tell rsync not to touch files that are newer on the destination side. To demonstrate how this works, we first create a new file called "newfile" on the destination server.

dest$ echo "don't touch that" > archive/newfile2


Previous Page  1  2  3  Next Page 

Sign up for CIO Asia eNewsletters.