Rsyncmanager.rb

Author:	David Powers
Copyright: October, 2004
License: BSD


Overview:

Rsyncmanager is a daemon designed to simplify the task of coordinating 
multiple file transfers to or from a machine.  It was originally written
to serve as the managing hub of a backup system that used rsync to feed 
in files from many other machines, so that all backups could be run from 
a single place.  That said, it can be used to coordinate any kind of regular
file transfer, from a simple hourly copy of an email store from a primary
to a backup server, to multiple concurrent rsyncs to multiple machines on
varying schedules.


Features:

* Coordinate multiple file transfers using rsync, scp or a combination 
  of the two
* Limit the number of concurrently running file transfers to prevent
  resource starvation
* Prevent file transfers from starting based on arbitrary conditions using
  external scripts (time of day, machine load, memory usage, phase of the 
  moon...)
* Run commands before and after a file transfer to handle setup and cleanup
  conditions (e.g. dump a database to disk, transfer it to a backup, then
  delete the dump)
* Intelligently limit concurrent file transfers to or from the same machine
  to prevent resource limitations from slowing or starving a transfer
* Limit concurrent file transfers based on arbitrary grouping (e.g. only
  one transfer from internal to external servers over a slow line at any
  given point)
* Coordinate transfers between multiple machines (grouping over multiple
  machines, avoiding having two machines push data to a third at the same
  time, etc.)
* Provides a clear web-based administrative report of all currently
  managed transfers and their current status (running, behind schedule,
  etc)
* Intelligently schedule file transfers based on average runtimes so that
  transfers happen within a defined schedule but do not run constantly if
  not needed
  

Requirements:

ruby 1.8.x
rsync (if rsync is to be used for file transfers)
scp (if scp is to be used for file transfers)


Configuration:

Almost all configuration of rsyncmanager is handled through its XML 
configuration file.  It can be placed anywhere on the system and is
referenced with the -c commandline parameter.  A very complete example
follows at the end of this README - although many sections can be left 
out.  A much simpler example looks something like this:

<configuration>
        <general>
                <maxtransfers>3</maxtransfers>
                <transferdefaults>
                        <type>rsync</type>
                        <options>-aSrq --delete</options>
                </transferdefaults>
        </general>
        <transfers>
                <transfer>
                        <description>disk2 groups</description>
                        <frequency>10m</frequency>
                        <from>/mnt/filestore/groups/</from>
                        <to>172.16.1.2:/mnt/filestore/groups/</to>
                </transfer>
                <transfer>
                        <description>disk2 personal</description>
                        <frequency>10m</frequency>
                        <from>/mnt/filestore/personal/</from>
                        <to>172.16.1.2:/mnt/filestore/personal/</to>
                </transfer>
	</transfers>
</configuration>

This file ensures (to the best of its ability given the available resources)
that the /mnt/filestore/groups and /mnt/filestore/personal shares are transfered
to another disk server every 10 minutes.  Even this simple example, which does
not make use of all of the features available for controling transfers, has
several advantages over simple cron job scheduling.

1.  The transfers will never run concurrently to the same server, which makes much 
more efficient use of resources and actually causes the transfers as a whole to
finish more quickly.

2.  If other transfers are added to other servers in the future the total number 
of running transfers will still be limited to 3, which is about all this particular
machine can handle efficiently with the speed of its disks and the load of its other
tasks.

3.  The transfers will run no more or less than is needed.  The manager tracks
average runtimes of the transfers and ensures that they run as close to schedule
as possible.

4.  The transfers will never run multiple times - if a long transfer for groups runs
for 30 minutes then it will run for 30 minutes, run the personal, and then start
groups again.  With a simple cron job you would end up with multiple rsync attempts
on groups as it passed the 10 minute mark, with several transfers for personal attempting
to run at the same time.

5.  The configuration is easily extended to add reporting, checks on current system
state (to avoid running transfers during heavy load periods), pre and post transfer
setup/cleanup programs and more.


A complete listing of every conf file options follows:

<configuration>
	<!--
		The general section contains overall settings as well
		as subsections that set defaults that can be overriden
		or used later as desired
	-->
	<general>
		<!--
			The maximum number of transfers that may be running
			at any point
		-->
		<maxtransfers>1</maxtransfers>
		
		<!--
			This program may be set to anything.  It is run
			before attempting to run *any* transfer.  If it
			exits with a non-zero exit code no transfers will
			be run
		-->
		<cutoffprogram>/usr/local/bin/loadcheck.rb</cutoffprogram>
		
		<!--
			These options are usually not necessary as rsyncmanager
			will search the filesystem on start to set these to a
			sane value.  However, if your installation of these
			programs is in an odd place it can be set here.  Otherwise
			simply remove these lines from your configuration.
		-->
		<rsyncpath>/path/to/rsync</rsyncpath>
		<scppath>/path/to/scp</scp>
		
		<!--
			transfer defaults set default values that are used
			for transfers unless sepcifically overridden.  See 
			the appropriate sections under transfer below for
			more information
		-->
		<transferdefaults>
			<type>rsync</type>
			<options>-aSr</options>
			<ignorepartialerrors>true</ignorepartialerrors>
		</transferdefaults>
		
		<!--
			If this section is defined a small informational
			HTTP server will be started on the listed address
			and port (the defaults are listed here).  Pointing
			a web browser at the /info page of that server will
			show a status page on all defined transfers.  Pointing
			a server at the /xml page will deliver an xml document
			suitable for use with the coordinate option (see below)
		-->
               <httpserver>
                        <port>7962</port>
                        <listen>0.0.0.0</listen>
                </httpserver>
		
		<!--
			the coordinate section, if defined, may hold one or more
			url elements.  Each url should point to an xml page,
			delivered via HTTP that describes the current state of
			another rsyncmanager running on a different machine.  This
			page could (int theory) be generated by anything, but in practice
			it is easiest to let the other rsyncmanager serve it itself via
			the httpserver option.  This information will be used 
			(if available) when deciding which transfers can be run.  Thus 
			several machines can coordinate to make the best use of resources 
			like slow lines, single collection/backup servers, etc.  NOTE: 
			Using this makes iteasier to miss potential starvation conditions
			that might lead to some transfers never being run.
		-->
		<coordinate>
			<url>url of xml page here</url>
		</coordinate>

	</general>
	<!-- 
		all the transfers rsyncmanager is managing.  Multiple transfer
		subsections may be defined.  Not all parts of a transfer need to 
		be defined.  If a part is missing it will be replaced by the
		appropriate section from transferdefaults or a reasonable default
	-->
	<transfers>
                <transfer>
                       <!-- plaintext description of the transfer, for web display purposes ..> 
			<description>A plain text name for the transfer</description>
			
			<!-- a transfer may be given an arbitrary group name.  Transfers with the
			same group name are never run at the same time (even across servers - see
			coordinate above).  This is very useful for marking transfers that may
			make use of slow links (from one local network across a DSL or T1 line to
			another network) so that the line is used only for one transfer at a time -->
			<group>group name</group>

			<!-- can also be scp -->
			<type>rsync</type>
			
			<!--
				This program may be set to anything.  It is run
				before attempting to run this transfer.  If it
				exits with a non-zero exit code this transfers will
				not be run.
			-->
			<cutoffprogram>/usr/local/bin/loadcheck.rb</cutoffprogram>
			
			<!-- these options are passed verbatim to the transfer program -->
			<options>-aq</options>
			
			<!-- 
				how often should the transfer run in the ideal case (this may
				of course change based on load, size of transfer, etc).  Can
				be expressed with m (minutes), h (hours), d (days), or w (weeks).
				Combinations (1d 6h) are not allowed.
			-->
                        <frequency>1d</frequency>
			
			<!--
				If this section is defined the command runs verbatim immediately 
				before the transfer is started.  If the command fails to run the
				transfer will be aborted.
			-->
                        <runbefore>/usr/bin/ssh www.wwebserver.com "su - pgsql -c /usr/local/bin/pg_dumpall &gt; /tmp/www.webserver.com.pgdump"</runbefore>
			
			<!-- 
				where to transfer the files from - uses the same form as the rsync 
				and scp commands
			-->
                        <from>www.webserver.com:/tmp/www.webserver.com.pgdump</from>
			
			<!-- where to transfer the files to -->
                        <to>/mnt/filestore/serverbackups/www.willcap.com/</to>
			
			<!-- similar to runbefore except that it is run after the transfer finishes -->
                        <runafter>/usr/bin/ssh www.willcap.com "rm /tmp/www.willcap.com.pgdump"</runafter>
			
			<!-- if set to true then partial transfer rsync errors will be ignored when 
			determining the success or failure of a transfer.  This is useful for regular
			transfers of live filesystems where certain files may be deleted or moved
			part way through the transfer -->
			<ignorepartialerrors>true/false</ignorepartialerrors>
                </transfer>
	</transfers>
</configuration>