Directory Replication Between Two Linux Servers

Replicate one directory between two servers without putting additional system or network resources.
1. First it checks the latest file in remote Server (source machine which is actually generating log files).
2. Checks if this files exist locally or not.
3. Creates the file with same name if doesn’t exists.

4. If file exists locally, it checks the number of lines in source.

5. Compares number of lines with local copy.
6. If Number of Lines in Source is greater then Number of Lines in local copy, it gets the new lines.
7. Appends the new lines from Source machine to local copy.
__________________________________________________________________________________________________
#!/bin/bash

file=`ssh root@192.168.1.99 “ls -t /usr/local/src/folder1” | head -1`
nsrc=`ssh root@192.168.1.99 “wc -l /usr/local/src/folder1/$file”  | awk ‘{print $1}’`
ndst=`touch folder1/$file`
ndst=`wc -l folder1/$file  | awk ‘{print $1}’`

declare -i start
start=$ndst+1

for i in $(seq $start $nsrc); do
update=`ssh root@192.168.1.99 “sed -n “${i}{p}” /usr/local/src/folder1/$file”`
echo $update >> /usr/local/src/folder1/$file
done

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.