Thursday, March 1, 2012

Sample Rsync script for syncing set of remote server file-systems using BASH


#!/bin/bash
#----------------------------------------------------------------------------------------------------------------------------------------------------------------
# Description      : This script can be used to sync the filesystems one server to another.
# Mode of exection : As 'root' user from Cron using the following syntax: '/opt/rsync_seq.sh >> /var/log/rsync/rsync.log 2>&1'
# Date of Creation : 01-Mar-2012
# Version          : 2.1

# DETAILS OF FILESYSTEMS TO BE SYNCED from 'Source Machine' -----> 'Remote machine'
#  mb-chic-pap01:/ua1001   ----->  mb-365c-pap01:/ua1001
#  mb-chic-pap01:/ua1002   ----->  mb-365c-pap01:/ua1002
#  mb-chic-pap01:/us1001   ----->  mb-365c-pap01:/us1001

#----------------------------------------------------------------------------------------------------------------------------------------------------------------

# Define the mail address to which Rsync status to be sent
MAIL_ADD='ashok_sysad@hotmail.com'

# Define the path of Log file
RSYNC_LOG='/var/log/rsync/rsync.log'

#Define functions for Mail Text
SUCCESS()
{
echo -e "\nRsync Completed !!!!! \nHostname: `hostname`\nCommand executed to sync: $RSYNC\nJob Run date & time : $ST_TIME\n\nScript Name: $0 \n\nLast few lines from Rsync log files. $RSYNC_LOG: \n`tail -10 $RSYNC_LOG` " | mail -s "Rsync is Finished!!!!! " $MAIL_ADD
}

FAILED()
{
 echo -e "\nRsync Failed !!!!! \n\tHostname: `hostname`\nCommand executed to sync: $RSYNC\nJob Run date & time : $ST_TIME\n\nScript Name: $0 \n\nLast few lines from Rsync log files. $RSYNC_LOG: \n`tail -10 $RSYNC_LOG` " | mail -s "Rsync Failed !!!!! " $MAIL_ADD
}

#----------------------------------------------------------------------------------------------------------------------------------------------------------------

# /ua1001   ----->  mb-365c-pap01:/ua1001

# Setting the Rsync command-set specifying the Filesystem details and required switches.
RSYNC='rsync -atlrzvuop --progress /ua1001/ root@mb-365c-pap01:/ua1001/'

# Setting this variable to capture the Rsync status
PCOUNT=`ps ax |grep -v grep |grep 'rsync -atlrzvuop --progress /ua1001/ root@mb-365c-pap01:/ua1001/' | wc -l`
ST_TIME=`date`

# Execution starts from here
        if [ $PCOUNT -eq " " ] || [ $PCOUNT -lt 1 ]
        then
                echo "Sync start: $ST_TIME"
                $RSYNC
                if [ $? -ne "0" ]; then
                        echo "Rsync Failed: `date`"
                        FAILED
                else
                        echo "\n\nSync completed: `date`"
                        SUCCESS
                fi
        else
                echo "Another Rsync process is running.Exiting !!!!!!!!!! "
                exit
        fi

#----------------------------------------------------------------------------------------------------------------------------------------------------------------

# /ua1002   ----->  mb-365c-pap01:/ua1002

# Setting the Rsync command-set specifying the Filesystem details and required switches.
RSYNC='rsync -atlrzvuop --progress /ua1002/ root@mb-365c-pap01:/ua1002/'

# Setting this variable to capture the Rsync status
PCOUNT=`ps ax |grep -v grep |grep 'rsync -atlrzvuop --progress /ua1002/ root@mb-365c-pap01:/ua1002/' | wc -l`
ST_TIME=`date`

# Execution starts from here
        if [ $PCOUNT -eq " " ] || [ $PCOUNT -lt 1 ]
        then
                echo "Sync start: $ST_TIME"
                $RSYNC
                if [ $? -ne "0" ]; then
                        echo "Rsync Failed: `date`"
                        FAILED
                else
                        echo "\n\nSync completed: `date`"
                        SUCCESS
                fi
        else
                echo "Another Rsync process is running.Exiting !!!!!!!!!! "
                exit
        fi

#----------------------------------------------------------------------------------------------------------------------------------------------------------------

# /us1001   ----->  mb-365c-pap01:/us1001

# Setting the Rsync command-set specifying the Filesystem details and required switches.
RSYNC='rsync -atlrzvuop --progress /us1001/ root@mb-365c-pap01:/us1001/'

# Setting this variable to capture the Rsync status
PCOUNT=`ps ax |grep -v grep |grep 'rsync -atlrzvuop --progress /us1001/ root@mb-365c-pap01:/us1001/' | wc -l`
ST_TIME=`date`

# Execution starts from here
        if [ $PCOUNT -eq " " ] || [ $PCOUNT -lt 1 ]
        then
                echo "Sync start: $ST_TIME"
                $RSYNC
                if [ $? -ne "0" ]; then
                        echo "Rsync Failed: `date`"
                        FAILED
                else
                        echo "\n\nSync completed: `date`"
                        SUCCESS
                fi
        else
                echo "Another Rsync process is running.Exiting !!!!!!!!!! "
                exit
        fi

Popular Posts

About Me

My photo
The intent of this blog is to share my work experience and spread some smart solutions on Linux to System Administrators. I'm hoping the solutions shared in this Blog would be helpful and come as a handy for Viewers. Brief about me: I have 18+ years work experience in System and Cloud Administration domain, primarily works on VMware Cloud Products (vSphere, vCloud Director, vRealize Automation, NSX Adv. Load Balancer, vROps).