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.

file=`ssh root@ “ls -t /usr/local/src/folder1” | head -1`
nsrc=`ssh root@ “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

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


Leave a Reply

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

You are commenting using your 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.