Use rsync to remotely mirror files

Oct 23, '02 09:34:24AM

Contributed by: Anonymous

Did you know rsync is available under Mac OS X 10.2? Cool, now you can remotely mirror directories rather than using rcp or scp. rsync can be tunneled over ssh, making it as secure as scp. I'll not go into that for this hint; you'll need to get the general idea first.

Scenario
As a Solaris admin, I have a hardware failover server, and I need to mirror the web_site directory from the primary to secondary server. I use rsync running as a daemon on the "server" to mirror (synchronize) the secondary client. I do this via a cron job at midnight, so I'm never more than 24 hours behind.

This will allow you to synchronize directories and mirror from another machine as a client. Better than remote copy for me. Here's how to do it.

  1. Create a file called rsync in "/etc/xinet.d/"

    Contents:
    # default: off
    # description: The rsync server is a good
    # addition to an ftp server, as it
    # allows crc check summing etc.
    service rsync
    {
    disable = no
    log_on_failure += USERID
    socket_type = stream
    user = root
    server = /usr/bin/rsync
    server_args = --daemon
    wait = no
    only_from = 10.0.0.0
    }
    # This "only_from = 10.0.0.0" Covers this network.
    # Enter the IP or range of which machine can access this sever.
  2. Add the port in "/etc/services":
    rsync    873/tcp    # rsync
    rsync 873/udp # rsync
    I would place it numerically in the 800 range. (duh) ;-)

  3. You'll need a rsyncd.conf file in /etc. This file with provide your server with mappings to the directory you wish to serve.
    [websites]
    path = /web_sites
    comment = Web Site Directory for OS X
    Other options are available:
      auth users = tridge, susan
    secrets file = /etc/rsyncd.secrets
    The /etc/rsyncd.secrets file would look something like this:
    tridge:mypass
    susan:herpass
    I don't use it. If you do, chmod the file to rw acces for root only. Make as many path mappings as you wish.

  4. Does it work? Test it.

    To see which maps you have set up in rsyncd.conf you'll need to type on the command line:
     % rsync localhost::
    This should return:
    web_sites       Web Site Directory
    For the one we set up.

  5. Start your engines:

    Know you are ready to do some sexy rsync'n. You do not have to set up rsync on the CLIENT machine as you did the server unless you want to (as a daemon). But if you want to "Go the other way" so to speak you will. The command and arguments for me on the CLIENT box is:
     % rsync -og --compress --recursive --times
    --perms --links --delete --verbose
    admin@10.0.0.150::web_sites /web_sites/
    [The rsync command should be one line, not three as shown here].

    I'm preserving owner, groups - compressing the stream, preserving times, permissions, symlinks (if any) - deleting files from the client not on the requesting client - verbose mode - I am admin@10.0.0.150 request my mapped drive in rsyncd.conf [web_sites] to update the web_site directory from the receiving server on /web_sites

    You can exclude your MP3's by adding "--exclude=*.mp3" to the arguments, as many excludes as you wish.

    Note that my directory is also called "/web_sites" on the CLIENT. Yours could be "Library/Webserver/Documents/" in keeping with the OS X default path. Could put it in /some_other_directory/tmp

    You can -HUP xinetd to start the services, or reboot. Whatever you wish to do.
You're good to go! Have fun.

Some reading:[Editor's note: Untested locally, but it seems quite useful!]

Comments (22)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20021023063424701