nixCraft Poll

Topics

How to use rsync for transferring files under Linux or UNIX

Posted by Vivek Gite [Last updated: August 2, 2007]

How do you install and use rsync to synchronize files and directories from one location (or one server) to another location? - A common question asked by new sys admin.

rsync is a free software computer program for Unix and Linux like systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. An important feature of rsync not found in most similar programs/protocols is that the mirroring takes place with only one transmission in each direction.

So what is unique about rsync?

It can perform differential uploads and downloads (synchronization) of files across the network, transferring only data that has changed. The rsync remote-update protocol allows rsync to transfer just the differences between two sets of files across the network connection.

How do I install rsync?

Use any one of the following commands to install rsync.

If you are using Debian or Ubuntu Linux, type the following command

# apt-get install rsync
OR
$ sudo apt-get install rsync

If you are using Red Hat Enterprise Linux (RHEL), type the following command

# up2date rsync

If you are using CentOS/Fedora Core Linux, type the following command

# yum install rsync

Always use rsync over ssh

Since rsync does not provide any security while transferring data it is recommended that you use rsync over ssh . This allows a secure remote connection. Now let us see some examples of rsync.

rsync command common options

Task : Copy file from a local computer to a remote server

Copy file from /www/backup.tar.gz to a remote server called openbsd.nixcraft.in
$ rsync -v -e ssh /www/backup.tar.gz jerry@openbsd.nixcraft.in:~Output:

Password:
sent 19099 bytes  received 36 bytes  1093.43 bytes/sec
total size is 19014  speedup is 0.99

Please note that symbol ~ indicate the users home directory (/home/jerry).

Task : Copy file from a remote server to a local computer

Copy file /home/jerry/webroot.txt from a remote server openbsd.nixcraft.in to a local computer /tmp directory:
$ rsync -v -e ssh jerry@openbsd.nixcraft.in:~/webroot.txt /tmp
Password

Task: Synchronize a local directory with a remote directory

$ rsync -r -a -v -e "ssh -l jerry" --delete openbsd.nixcraft.in:/webroot/ /local/webroot

Task: Synchronize a remote directory with a local directory

$ rsync -r -a -v -e "ssh -l jerry" --delete /local/webroot openbsd.nixcraft.in:/webroot

Task: Synchronize a local directory with a remote rsync server

$ rsync -r -a -v --delete rsync://rsync.nixcraft.in/cvs /home/cvs

Task: Mirror a directory between my "old" and "new" web server/ftp

You can mirror a directory between my "old" (my.old.server.com) and "new" web server with the command (assuming that ssh keys are set for password less authentication)
$ rsync -zavrR --delete --links --rsh="ssh -l vivek" my.old.server.com:/home/lighttpd /home/lighttpd

Read related previous articles

Other options - rdiff and rdiff-backup

There also exists a utility called rdiff, which uses the rsync algorithm to generate delta files Using rdiff. A utility called rdiff-backup has been created which is capable of maintaining a backup mirror of a file or directory over the network, on another server. rdiff-backup stores incremental rdiff deltas with the backup, with which it is possible to recreate any backup point. Next time I will write about these Utilities :)

rsync for Windows server/XP

Please note if you are using Windows, try any one of the program:

  1. DeltaCopy
  2. NasBackup

Further readings

=> Read rsync man page
=> Official rsync documentation

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. n00b Says:

    I have RSYNC running to copy files from a mapped linux server (through a windows PC running cwrsync) to a remote linux server. Everything works fine, but the permissions and ownership changes. I added this line:

    –chmod=u+rwx,g+rx,o+x

    and that seems to work fine in ensuring I have chmod 0755 set for my folders and files. However, it keeps reverting my users:groups from myuser:nobody to 400:401.

    What is the code that I add to rsync to have it:
    a) Retain the existing ownerships, or
    b) Force myuser:nobody for all files and folders?

  2. n00b Says:

    Oops. Second question. I’d like for all directories to retain their original ownership and permissions (rather than forcing the chmod). The reason is because I’d like for most directories to be 0755, but need 0777 on some specific ones for a templating system and tiny_mc plug-in to work. How can i retain the original ownership and permissions? rather than having to go through and modify them one-by-one after the Rsync, or giving a blanket 0777 for everything? Regardless, I don’t know how to specify folder-specific privileges in rsync and would prefer to just have them remain the same as before the rsync.

  3. vivek Says:

    <owever, it keeps reverting my users:groups from myuser:nobody to 400:401.

    You need to add myuser:nobody to your local server.

    <I don’t know how to specify folder-specific privileges in rsync and would prefer to just have them remain the same as before the rsync.

    Make sure files are transferred in “archive” mode, which ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer.
    rsync -avr src/path /dest/path2

  4. babu Says:

    HiFriends,

    Can you give a help about. I have some unix specific setup.

    We have the following directories on p1cpe, and p2cpe:
    /opt/Apps/StaticContent
    /opt/Apps/html_templates
    /opt/Apps/bea/user_projects/domains/NDC-Dev/lib
    /opt/Apps/webapps
    /opt/Apps/src

    I need them to be available, and in sync to both systems. The originals should all be one p1cpe, so if you have to copy or do anything, those should be considered the source.

    The idea is, if a developer makes changes to files on p1cpe:/opt/Apps/StaticContent, I want it also available on p2cpe, without them having to modify it twice.

    All the work should be done as the user “webdude”. You should have sudo rights to webdude on p1cpe and p2cpe.

    Couls you please help me on that.

    Thanks,
    Babu

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.