Pages

Unixbhaskar's Blog

Sunday, October 31, 2010

Rsync : A handy tool to sync with

In this article I am going to show you some implication of rsync tool.It is a tool often used by the web masters and system administrator to sync with various number of host across the network.Now what is the utility of having rsync under your armory? The advantage is quite useful.Here are few:

** It can sync filesystem across the network

** It can only sync the updated version the filesystem then the whole tree.

** It is very useful to copy the entire web site ported to new host or hosts.

** Rsync can do a various job related to backup procedure.

support for copying links, devices, owners, groups, and permissions

** exclude and exclude-from options similar to GNU tar

** a CVS exclude mode for ignoring the same files that CVS would ignore

** can use any transparent remote shell, including ssh or rsh

** does not require super-user privileges

** pipelining of file transfers to minimize latency costs

** support for anonymous or authenticated rsync daemons (ideal for mirroring)

Above stated advantages are name a few.Kindly look into the manual page of it.So,now I have four different partition and and I want to sync a particualr dir with all partiotn with a specific application running on all the OSes.

The idea is to get it sync; because I want the information to be indentical across all the partitions(same can be applied to different hosts too over the network!!).

How does it work?

You must set up one machine or another of a pair to be an "rsync server" by running rsync in a daemon mode ("rsync --daemon" at the commandline) and setting up a short, easy configuration file (/etc/rsyncd.conf). Below I'll detail a sample configuration file. The options are readily understood, few in number -- yet quite powerful.

Any number of machines with rsync installed may then synchronize to and/or from the machine running the rsync daemon. You can use this to make backups, mirror filesystems, distribute files or any number of similar operations. Through the use of the "rsync algorithm" which transfers only the diffs between files (similar to a patch file) and then compressing them -- you are left with a very efficient system.

For those of you new to secure shell ("ssh" for short), you should be using it! There's a very useful and quite thourough Getting Started with SSH document available. You may also want to visit the Secure Shell Web Site. Or, just hit the Master FTP Site in Finland and snag it for yourself. It provides a secure, encrypted "pipe" for your network traffic. You should be using it instead of telnet, rsh or rlogin and use the replacement "scp" command instead of "rcp."

Setting up a Server

You must set up a configuration file on the machine meant to be a server and run the rsync binary in daemon mode. Even your rsync client machines can run rsync in daemon mode for two-way transfers. You can do this automatically for each connection via the inet daemon or at the commandline in standalone mode to leave it running in the background for often repeated rsyncs. .Plus there is a CGI script that folks fire off frequently during the day for immediate updating of content. This is a lot of rsync calls! If you start off the rsync daemon through your inet daemon, then you incur much more overhead with each rsync call. You basically restart the rsync daemon for every connection your server machine gets! It's the same reasoning as starting Apache in standalone mode rather than through the inet daemon. It's quicker and more efficient to start rsync in standalone mode if you anticipate a lot of rsync traffic. Otherwise, for the occasional transfer follow the procedure to fire off rsync via the inet daemon. This way the rsync daemon, as small as it is, doesn't sit in memory if you only use it once a day or whatever. Your call.

Below is a sample rsync configuration file. It is placed in your /etc directory as rsyncd.conf.


motd file = /etc/rsyncd.motd
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock

[simple_path_name]
path = /rsync_files_here
comment = My Very Own Rsync Server
uid = nobody
gid = nobody
read only = no
list = yes
auth users = username
secrets file = /etc/rsyncd.scrt


Various options that you would modify right from the start are the areas in italics in the sample above. I'll start at the top, line by line, and go through what you should pay attention to. What the sample above does is setup a single "path" for rsync transfers to that machine.

Starting at the top are four lines specifying files and their paths for rsync running in daemon mode. The first is a "message of the day" (motd) file like you would use for an FTP server. This file's contents get displayed when clients connect to this machine. Use it as a welcome, warning or simply identification. The next line specifies a log file to send diagnostic and norml run-time messages to. The PID file contains the "process ID" (PID) number of the running rsync daemon. A lock file is used to ensure that things run smoothly. These options are global to the rsync daemon.

The next block of lines is specific to a "path" that rsync uses. The options contained therein have effect only within the block (they're local, not global options). Start with the "path" name. It's somewhat confusing that rsync uses the term "path" -- as it's not necessarily a full pathname. It serves as an "rsync area nickname" of sorts. It's a short, easy to remember (and type!) name that you assign to a try filesystem path with all the options you specify. Here are the things you need to set up first and foremost:

* path - this is the actual filesystem path to where the files are rsync'ed from and/or to.

* comment - a short, descriptive explanation of what and where the path points to for listings.

* auth users - you really should put this in to restrict access to only a pre-defined user that you specify in the following secrets file - does not have to be a valid system user.

* secrets file - the file containing plaintext key/value pairs of usernames and passwords.

One thing you should seriously consider is the "hosts allow" and "hosts deny" options for your path. Enter the IPs or hostnames that you wish to specifically allow or deny! If you don't do this, or at least use the "auth users" option, then basically that area of your filesystem is wide open to the world by anyone using rsync! Something I seriously think you should avoid...

Check the rsyncd.conf man page with "man rsyncd.conf" and read it very carefully where security options are concerned. You don't want just anyone to come in and rsync up an empty directory with the "--delete" option, now do you?

The other options are all explained in the man page for rsyncd.conf. Basically, the above options specify that the files are chmod'ed to uid/gid, the filesystem path is read/write and that the rsync path shows up in rsync listings. The rsync secrets file I keep in /etc/ along with the configuration and motd files, and I prefix them with "rsyncd." to keep them together.


For that reason I have mounted a partition which holds the updated version of the file and sync with others from that partition.

Say my Gentoo partition is updated and I want to update Arch,Fedora and Debian with that...so I have mouted all of them in different designated dir under the / .

Here is how I can sync that perticular file :


bhaskar@bhaskar-laptop_06:55:11_Sun Oct 31:/srv/http/dokuwiki> sudo /usr/bin/rsync -avrP data/ /Fedora/var/www/html/dokuwiki/


I will update the output of it once it finished.With rsync the trailing slash (/) has a big role to play.It signifies that you want to copy all the contents of that dir instead of the dir.Without the trailing slash rsync will copy the entire directory.Now some explanation about the flags I have passed with the rsync. Here is the meaning of those;


a ------> It says that in the format of archive

v -------> verbose mode

r --------> recursive mode

P ---------> progress mode

It has many other useful options with deal with.Kindly look into the man pages for more details.But when you sync over the network you must use it over the ssh so the tunnel get secured during transfer.It has "-e" option which will allow you to pass an ssh option with it.

Resources:
1) http://troy.jdmz.net/rsync/index.html

2) http://www.netbits.us/docs/stunnel_rsync.html

3) http://www.linux-mag.com/id/7888/1/
Hope this will help.

Cheers!
Bhaskar

No comments:

Post a Comment