Home > UNIX > Use rsync, ssh, and cron to synchronize files between AIX hosts

Use rsync, ssh, and cron to synchronize files between AIX hosts

Recently I had to synchronize files between AIX hosts. There are a variety of methods to use. The method I chose I using rsync, openssh, and cron.  I chose rsync because of its speed and versatility when dealing with file attributes.

The first step was to install rsync from the AIX Toolbox for LINUX Aplications  cd (openSSH and Cron were already installed on these boxes). Rsync has a dependency upon popt. The rpms can be found on the cd in RPMS/ppc. Here are the commands I ran to install them:

 $ rpm -Uhv popt-1.7-2.aix5.1.ppc.rpm
popt                        ##################################################

$ rpm -Uhv rsync-2.6.2-1.aix5.1.ppc.rpm
rsync                       ##################################################

The next step is to generate a key for SSH to utilize. The key is generated with no password.  There are security ramifications using this method, study up on SSH if you need further info.

root@testhost# ssh-keygen -t dsa -b 1024 -f /home/admin/bin/jbhqts03-rsync-key
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase):  [I pressed enter]
Enter same passphrase again:   [I pressed enter]
Your identification has been saved in /home/admin/bin/testhost-rsync-key.
Your public key has been saved in /home/admin/bin/testhost-rsync-key.pub.
The key fingerprint is:
f9:8c:4b:5f:08:cb:4d:47:9c:d7:43:81:6e:4a:33:9e root@testhost

The pub file must now be copied to the remote host. I did this as follows:

scp /home/admin/bin/testhost-rsync-key.pub remotehostuser@remotehost:/home/remotehostuser/

I then logged into the remote host. In that users home directory I did the following:

$ if [ ! -d .ssh ]; then mkdir .ssh ; chmod 700 .ssh ; fi
$ mv testhost -rsync-key.pub .ssh/
$ cd .ssh/
$ if [ ! -f authorized_keys ]; then touch authorized_keys ; chmod 600 authorized_keys ; fi
$ cat testhost -rsync-key.pub >> authorized_keys

At this point you should now be able to use rsync and SSH to synchronize files between hosts in a cron job. Please study SSH to determine how to harden the security.

An example of doing this is the user synchronization script I am using. This script is done from the point of view of the remote host. I am doing a pull on these files.

#!/usr/bin/ksh
# Get new /etc/passwd & /etc/group files from mainhost
# Overwrite existing files
rsync -goptvz -e “ssh -i /home/admin/bin/remotehost-rsync-key” mainhost:/etc/passwd /etc/passwd
rsync -goptvz -e “ssh -i /home/admin/bin/remotehost-rsync-key” mainhost:/etc/group /etc/group
# Get new files from /etc/security from mainhost
# Overwrite existing files
rsync -goptvz -e “ssh -i /home/admin/bin/remotehost-rsync-key” mainhost:/etc/security/passwd /etc/security/passwd
rsync -goptvz -e “ssh -i /home/admin/bin/remotehost-rsync-key” mainhost:/etc/security/group /etc/security/group

In crontab I set this script to run every 10 minutes and log the output.

0,10,20,30,40,50 * * * * /home/admin/bin/usersync.ksh >> /home/admin/logs/usersync.out 2>&1

There are more ‘elegant’ ways to script this, but I’m an administrator not a programmer. I want simple and easy to understand.

The rsync options chosen are:

-g = preserve group

-o = preserve owner

-p = preserve permissions

-t = preserve modification times

-v = verbose (for the log)

-z = compress files during transfer

As you can see the options insure the passwd and groups files on the remote host keep the same attributes as on the original host. This is a primary reason I chose the rsync method.

Advertisements
Categories: UNIX
  1. Steve Schnepp
    July 24, 2012 at 04:21

    The “-a” (archive) option is also very convenient as it bundles all the useful mirroring options ‘-rlptgoD’

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: