initial commit
This commit is contained in:
12
chapter7/function_elapsed_time
Executable file
12
chapter7/function_elapsed_time
Executable file
@@ -0,0 +1,12 @@
|
||||
elapsed_time ()
|
||||
{
|
||||
SEC=$1
|
||||
(( SEC < 60 )) && echo "[Elapsed time: $SEC seconds]\c"
|
||||
|
||||
(( SEC >= 60 && SEC < 3600 )) && echo "[Elapsed time: $(( SEC / 60 )) \
|
||||
min $(( SEC % 60 )) sec]\c"
|
||||
|
||||
(( SEC > 3600 )) && echo "[Elapsed time: $(( SEC / 3600 )) hr $(( (SEC % 3600) / 60 )) \
|
||||
min $(( (SEC % 3600) % 60 )) sec]\c"
|
||||
}
|
||||
|
||||
18
chapter7/function_ready_to_run
Executable file
18
chapter7/function_ready_to_run
Executable file
@@ -0,0 +1,18 @@
|
||||
ready_to_run ()
|
||||
{
|
||||
# set -x
|
||||
# This function looks for a file on the system
|
||||
# defined by the $READYTORUN_FILE variable. The
|
||||
# presents presence of this file indicates we are ready
|
||||
# to run this script. This file will contain a
|
||||
# number 1 or 2 identifying the day we are
|
||||
# working with.
|
||||
|
||||
if [ -r ${READYTORUN_FILE} ]
|
||||
then
|
||||
cat ${READYTORUN_FILE}
|
||||
else
|
||||
echo "NOT_READY"
|
||||
fi
|
||||
}
|
||||
|
||||
68
chapter7/function_verify_copy
Executable file
68
chapter7/function_verify_copy
Executable file
@@ -0,0 +1,68 @@
|
||||
verify_copy ()
|
||||
{
|
||||
#set -x
|
||||
|
||||
MYLOGFILE=${WORK_DIR}/verify_rsync_copy_day${DAY}.log
|
||||
>$MYLOGFILE
|
||||
ERROR=0
|
||||
|
||||
# Enclose this loop so we can redirect output to the log file
|
||||
# with one assignment at the bottom of the function
|
||||
{
|
||||
# Put a header for the verification log file
|
||||
|
||||
echo "\nRsync copy verification between $THIS_HOST and machines $MACHINE_LIST\n\n" >$MYLOGFILE
|
||||
|
||||
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
for LOC_MP in $(df | grep oradata_dm_[0-2][0-9] | awk '{print $7}')
|
||||
do
|
||||
LS_FILES=$(find $LOC_MP -type f)
|
||||
for FL in $LS_FILES
|
||||
do
|
||||
LOC_FS=$(ls -l $FL | awk '{print $5}' 2>&1)
|
||||
|
||||
# This sed statement changes the "m" to $DAY
|
||||
REM_FL=$(echo $FL | sed s/oradata_dm_/oradata_d${DAY}_/g)
|
||||
|
||||
REM_FS=$(rsh $M ls -l $REM_FL | awk '{print $5}' 2>&1)
|
||||
echo "Checking File: $FL"
|
||||
echo "Local $THIS_HOST size:\t$LOC_FS"
|
||||
echo "Checking Remote File: $REM_FL"
|
||||
echo "$M size:\t$REM_FS"
|
||||
if [ "$LOC_FS" -ne "$REM_FS" ]
|
||||
then
|
||||
echo "ERROR: File size mismatch between $THIS_HOST and $M"
|
||||
echo "File is: $FL"
|
||||
ERROR=1
|
||||
fi
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
if (( ERROR != 0 ))
|
||||
then
|
||||
# Record the failure in the log file
|
||||
|
||||
echo "\n\nRSYNC ERROR: $THIS_HOST Rsync copy failed...file size \
|
||||
mismatch...\n\n" | tee -a $MYLOGFILE
|
||||
|
||||
# Send email notification with file size log
|
||||
|
||||
mailx -r "$EMAIL_FROM" -s "RSYNC ERROR: $THIS_HOST Rsync copy failed\
|
||||
...file size mismatch -- log attached" data_support < $MYLOGFILE
|
||||
|
||||
echo "\nERROR: Rsync copy Failed!"
|
||||
echo "\n\nCheck log file: $MYLOGFILE\n\n"
|
||||
echo "\n...Exiting...\n"
|
||||
cleanup_exit 3
|
||||
return 3
|
||||
else
|
||||
echo "\nSUCCESS: Rsync copy completed successfully..."
|
||||
echo "\nAll file sizes match...\n"
|
||||
fi
|
||||
} | tee -a $MYLOGFILE
|
||||
|
||||
}
|
||||
|
||||
344
chapter7/generic_DIR_rsync_copy.bash
Executable file
344
chapter7/generic_DIR_rsync_copy.bash
Executable file
@@ -0,0 +1,344 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# SCRIPT: generic_DIR_rsync_copy.bash
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 1/14/2007
|
||||
# REV: 1.0
|
||||
# PLATFORM: Not platform dependent
|
||||
#
|
||||
# REQUIRED INSTALLATION: rsync must be
|
||||
# installed on the source and
|
||||
# all target systems
|
||||
#
|
||||
# REQUIRED FILE FOR OPERATION:
|
||||
# The file pointed to by the
|
||||
# $DIR_LIST_FILE variable refers
|
||||
# to the required file listing the
|
||||
# top level directory structures
|
||||
# that rsync is to replicate. The
|
||||
# DIR_LIST_FILE is defined in the
|
||||
# variables declarations section
|
||||
# of this shell script
|
||||
#
|
||||
# PURPOSE: This shell script is used to replicate
|
||||
# directory structures to one or more remote
|
||||
# machines using rsync in archive and
|
||||
# compressed mode. This script requires
|
||||
# a file referenced by the variable
|
||||
# DIR_LIST_FILE that lists the top level
|
||||
# of each of the local directory structures
|
||||
# that we want to replicate to one or more
|
||||
# remote machines
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
#
|
||||
# set -n # Uncomment to check script syntax
|
||||
# # without execution. Remember to put
|
||||
# # the comment back into the script or it
|
||||
# # will never execute.
|
||||
#
|
||||
#######################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
#######################################
|
||||
|
||||
# Define the target machines to copy data to.
|
||||
# To specify more than one host enclose the
|
||||
# hostnames in double quotes and put at least
|
||||
# one space between each hostname
|
||||
#
|
||||
# EXAMPLE: MACHINE_LIST="fred yogi booboo"
|
||||
|
||||
MACHINE_LIST="fred"
|
||||
|
||||
# The DIR_LIST_FILE variable define the shell script
|
||||
# required file listing the directory structures
|
||||
# to replicate with rsync.
|
||||
|
||||
DIR_LIST_FILE="rsync_directory_list.lst"
|
||||
|
||||
# Define the directory containing all of the
|
||||
# output files this shell script will produce
|
||||
|
||||
WORK_DIR=/usr/local/bin
|
||||
|
||||
# Define the rsync script log file
|
||||
|
||||
LOGFILE="generic_DIR_rsync_copy.log"
|
||||
|
||||
# Query the system for the hostname
|
||||
|
||||
THIS_HOST=$(hostname)
|
||||
|
||||
# Query the system for the UNIX flavor
|
||||
|
||||
OS=$(uname)
|
||||
|
||||
#######################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
#######################################
|
||||
|
||||
elapsed_time ()
|
||||
{
|
||||
SEC=$1
|
||||
(( SEC < 60 )) && echo -e "[Elapsed time: $SEC seconds]\c"
|
||||
|
||||
(( SEC >= 60 && SEC < 3600 )) && echo -e "[Elapsed time: \
|
||||
$(( SEC / 60 )) min $(( SEC % 60 )) sec]\c"
|
||||
|
||||
(( SEC > 3600 )) && echo -e "[Elapsed time: $(( SEC / 3600 )) \
|
||||
hr $(( (SEC % 3600) / 60 )) min $(( (SEC % 3600) % 60 )) \
|
||||
sec]\c"
|
||||
}
|
||||
|
||||
#######################################
|
||||
|
||||
verify_copy ()
|
||||
{
|
||||
# set -x
|
||||
|
||||
MYLOGFILE=${WORK_DIR}/verify_rsync_copy.log
|
||||
>$MYLOGFILE
|
||||
ERROR=0
|
||||
|
||||
# Enclose this loop so we can redirect output to the log file
|
||||
# with one assignment at the bottom of the function
|
||||
{
|
||||
# Put a header for the verification log file
|
||||
|
||||
echo -e "\nRsync copy verification starting between $THIS_HOST \
|
||||
and machine(s) $MACHINE_LIST\n\n" >$MYLOGFILE
|
||||
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
for DS in $(cat $DIR_LIST_FILE)
|
||||
do
|
||||
LS_FILES=$(find $DS -type f)
|
||||
for FL in $LS_FILES
|
||||
do
|
||||
LOC_FS=$(ls -l $FL | awk '{print $5}' 2>&1)
|
||||
REM_FS=$(ssh $M ls -l $FL | awk '{print $5}' 2>&1)
|
||||
echo "Checking File: $FL"
|
||||
echo -e "Local $THIS_HOST size:\t$LOC_FS"
|
||||
echo -e "Remote $M size:\t$REM_FS"
|
||||
if [ "$LOC_FS" -ne "$REM_FS" ]
|
||||
then
|
||||
echo "ERROR: File size mismatch between $THIS_HOST and $M"
|
||||
echo "File is: $FL"
|
||||
ERROR=1
|
||||
fi
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
if (( ERROR != 0 ))
|
||||
then
|
||||
# Record the failure in the log file
|
||||
|
||||
echo -e "\n\nRSYNC ERROR: $THIS_HOST Rsync copy failed...file\
|
||||
size mismatch...\n\n" | tee -a $MYLOGFILE
|
||||
|
||||
echo -e "\nERROR: Rsync copy Failed!"
|
||||
echo -e "\n\nCheck log file: $MYLOGFILE\n\n"
|
||||
echo -e "\n...Exiting...\n"
|
||||
|
||||
# Send email notification with file size log
|
||||
|
||||
# mailx -r from_someone@somewhere.??? -s "Rsync copy verification \
|
||||
#failed $THIS_HOST -- File size mismatch" \
|
||||
# somebody@fsf.com < $MYLOGFILE
|
||||
|
||||
sleep 2 # Give a couple of seconds to send the email
|
||||
|
||||
exit 3
|
||||
else
|
||||
echo -e "\nSUCCESS: Rsync copy completed successfully..."
|
||||
echo -e "\nAll file sizes match...\n"
|
||||
fi
|
||||
} | tee -a $MYLOGFILE
|
||||
|
||||
}
|
||||
|
||||
#######################################
|
||||
# BEGINNING OF MAIN
|
||||
#######################################
|
||||
|
||||
# We enclose the entire MAIN in curly craces
|
||||
# so that all output of the script is logged
|
||||
# in the $LOGFILE with a single output
|
||||
# redirection
|
||||
|
||||
{
|
||||
|
||||
# Save the last logfile with a .yesterday filename extension
|
||||
|
||||
cp -f $LOGFILE ${LOGFILE}.yesterday 2>/dev/null
|
||||
|
||||
# Zero out the $LOGFILE to start a new file
|
||||
|
||||
>$LOGFILE
|
||||
|
||||
# Start all of the rsync copy sessions at once by looping
|
||||
# through each of the mount points and issuing an rsync
|
||||
# command in the background, saving the PID of the
|
||||
# background process, and incrementing a counter.
|
||||
|
||||
echo -e "\nStarting a bunch of rsync sessions...$(date)\n"
|
||||
|
||||
# Initialize the rsync session counter, TOTAL
|
||||
# to zero
|
||||
|
||||
TOTAL=0
|
||||
|
||||
# Loop through each machine in the $MACHINE_LIST
|
||||
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
# Loop through all of the directory structures
|
||||
# listed in the $DIR_LIST_FILE and start an rsync
|
||||
# session in the background for each directory
|
||||
|
||||
for DS in $(cat $DIR_LIST_FILE)
|
||||
do
|
||||
# Ensure each directory structure has a trailing
|
||||
# forward slash to ensure an extra directory is
|
||||
# not created on the target
|
||||
|
||||
if ! $(echo "$DS" | grep -q '/$')
|
||||
then
|
||||
# If the directory structure does not
|
||||
# have a trailing forward slash, add it
|
||||
|
||||
DS="${DS}/"
|
||||
fi
|
||||
|
||||
# Start a timed rsync session in the background
|
||||
|
||||
time rsync -avz ${DS} ${M}:${DS} 2>&1 &
|
||||
|
||||
# Keep a running total of the number of rsync
|
||||
# sessions started
|
||||
(( TOTAL = TOTAL + 1 ))
|
||||
done
|
||||
done
|
||||
|
||||
# Sleep a 10 seconds before monitoring processes
|
||||
|
||||
sleep 10
|
||||
|
||||
# Query the process table for the number of rsync sessions
|
||||
# that continue executing and store that value in the
|
||||
# REM_SESSIONS variable
|
||||
|
||||
# Create a list of directory structures for an egrep list
|
||||
|
||||
EGREP_LIST= # Initialize to null
|
||||
|
||||
while read DS
|
||||
do
|
||||
if [ -z "$EGREP_LIST" ]
|
||||
then
|
||||
EGREP_LIST="$DS"
|
||||
else
|
||||
EGREP_LIST="|$DS"
|
||||
fi
|
||||
done < $DIR_LIST_FILE
|
||||
|
||||
REM_SESSIONS=$(ps x | grep "rsync -avz" | egrep "$EGREP_LIST" \
|
||||
| grep -v grep | awk '{print $1}' | wc -l)
|
||||
|
||||
# Give some feedback to the end user.
|
||||
|
||||
if (( REM_SESSIONS > 0 ))
|
||||
then
|
||||
echo -e "\n$REM_SESSIONS of $TOTAL rsync copy sessions require further updating..."
|
||||
echo -e "\nProcessing rsync copies from $THIS_HOST to both $MACHINE_LIST machines"
|
||||
echo -e "\nPlease be patient, this process may a very long time...\n"
|
||||
echo -e "Rsync is running [Start time: $(date)]\c"
|
||||
else
|
||||
echo -e "\nAll files appear to be in sync...verifying file sizes...Please wait...\n"
|
||||
fi
|
||||
|
||||
# While the rsync processes are executing this loop
|
||||
# will place a . (dot) every 60 seconds as feedback
|
||||
# to the end user that the background rsync copy
|
||||
# processes are still executing. When the remaining
|
||||
# rsync sessions are less than the total number of
|
||||
# sessions this loop will give feedback as to the
|
||||
# number of remaining rsync session, as well as
|
||||
# the elapsed time of the procesing, every 5 minutes
|
||||
|
||||
# Set the shell variable SECONDS to 10 to make up for the
|
||||
# time we slept while the rsync sessions started
|
||||
|
||||
SECONDS=10
|
||||
|
||||
# Initialize the minute counter to zero minutes
|
||||
|
||||
MIN_COUNTER=0
|
||||
|
||||
# Loop until all of the rsync sessions have completed locally
|
||||
|
||||
until (( REM_SESSIONS == 0 ))
|
||||
do
|
||||
sleep 60
|
||||
echo -e ".\c"
|
||||
REM_SESSIONS=$(ps x | grep "rsync -avz" | egrep "$EGREP_LIST" \
|
||||
| grep -v grep | awk '{print $1}' | wc -l)
|
||||
if (( REM_SESSIONS < TOTAL ))
|
||||
then
|
||||
(( MIN_COUNTER = MIN_COUNTER + 1 ))
|
||||
if (( MIN_COUNTER >= $(( TOTAL / 2 )) ))
|
||||
then
|
||||
MIN_COUNTER=0
|
||||
echo -e "\n$REM_SESSIONS of $TOTAL rsync sessions \
|
||||
remaining $(elapsed_time $SECONDS)\c"
|
||||
if (( REM_SESSIONS <= $(( TOTAL / 4 )) ))
|
||||
then
|
||||
echo -e "\nRemaining rsync sessions include:\n"
|
||||
ps aux | grep "rsync -avz" | egrep "$EGREP_LIST" \
|
||||
| grep -v grep
|
||||
echo
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
echo -e "\n...Local rsync processing completed on ${THIS_HOST}...$(date)"
|
||||
|
||||
echo -e "\n...Checking remote target machine(s): ${MACHINE_LIST}..."
|
||||
|
||||
# Just because the local rsync processes have completed does
|
||||
# not mean that the remote rsync processes have completed
|
||||
# execution. This loop verifies that all of the remote
|
||||
# rsync processes completed execution. The end user will
|
||||
# receive feedback if any of the remote processes are running.
|
||||
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
for DS in $(cat $DIR_LIST_FILE)
|
||||
do
|
||||
RPID=$(ssh $M ps x | grep rsync | grep $DS | grep -v grep | awk '{print $1}')
|
||||
until [ -z "$RPID" ]
|
||||
do
|
||||
echo "rsync is processing ${MP} on ${M}...sleeping one minute..."
|
||||
sleep 60
|
||||
RPID=$(ssh $M ps x | grep rsync | grep $DS | grep -v grep \
|
||||
| awk '{print $1}')
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
echo -e "\n...Remote rsync processing completed $(date)\n"
|
||||
|
||||
# Verify the copy process
|
||||
|
||||
verify_copy
|
||||
|
||||
echo -e "\nRsync copy completed $(date)"
|
||||
echo -e "\nElapsed time: $(elapsed_time $SECONDS)\n"
|
||||
|
||||
} 2>&1 | tee -a $LOGFILE
|
||||
|
||||
###############################################
|
||||
# END OF SCRIPT
|
||||
###############################################
|
||||
408
chapter7/generic_FS_rsync_copy.bash
Executable file
408
chapter7/generic_FS_rsync_copy.bash
Executable file
@@ -0,0 +1,408 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# SCRIPT: generic_FS_rsync_copy.bash
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 1/14/2008
|
||||
# REV: 1.2.1
|
||||
#
|
||||
# PURPOSE: This script is used to replicate
|
||||
#
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 ==> Normal execution.
|
||||
# 2 ==> Remote host is not pingable.
|
||||
# 3 ==> Copy verification failed. One or
|
||||
# more local files are not the same
|
||||
# size as the remote files.
|
||||
# 4 ==> No machines are defined to copy
|
||||
# data to.
|
||||
# 5 ==> Exited on a trapped signal.
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
#
|
||||
# set -n # Uncomment to check the script.s syntax
|
||||
# # without any execution. Do not forget to
|
||||
# # recomment this line!
|
||||
#
|
||||
########################################
|
||||
# REVISION LIST:
|
||||
########################################
|
||||
#
|
||||
# IF YOU MODIFY THIS SCRIPT, DOCUMENT THE CHANGE(S)!!!
|
||||
#
|
||||
########################################
|
||||
#
|
||||
# Revised by:
|
||||
# Revision Date:
|
||||
# Revision:
|
||||
#
|
||||
##############################################
|
||||
# DEFINE FILES AND GLOBAL VARIABLES HERE
|
||||
##############################################
|
||||
|
||||
# Define the target machines to copy data to.
|
||||
# To specify more than one host enclose the
|
||||
# hostnames in double quotes and put at least
|
||||
# one space between each hostname
|
||||
#
|
||||
# EXAMPLE: MACHINE_LIST="fred yogi booboo"
|
||||
|
||||
MACHINE_LIST="fred"
|
||||
|
||||
# The FS_PATTERN variable defines the regular expression
|
||||
# matching the filesystems we want to replicate with rsync.
|
||||
|
||||
FS_PATTERN="/data0[1-5]"
|
||||
|
||||
# Add /usr/local/bin to the PATH
|
||||
|
||||
export PATH=$PATH:/usr/local/bin
|
||||
|
||||
# Define the directory containing all of the
|
||||
# output files this shell script will produce
|
||||
|
||||
WORK_DIR=/usr/local/bin
|
||||
|
||||
# Define the rsync script log file
|
||||
|
||||
LOGFILE=${WORK_DIR}/generic_FS_rsync_copy.log
|
||||
|
||||
# Capture the shell script file name
|
||||
|
||||
THIS_SCRIPT=$(basename $0)
|
||||
|
||||
# Initialize the background process ID list
|
||||
# variable to NULL
|
||||
|
||||
BG_PID_LIST=
|
||||
|
||||
# Define the file containing a list of files to verify
|
||||
# the sizes match locally and remotely
|
||||
|
||||
LS_FILES=ls_output.dat
|
||||
>$LS_FILES
|
||||
|
||||
# Initialize the TOTAL variable to 0
|
||||
|
||||
TOTAL=0
|
||||
|
||||
# Query the system for the hostname
|
||||
|
||||
THIS_HOST=$(hostname)
|
||||
|
||||
# Query the system for the UNIX flavor
|
||||
|
||||
THIS_OS=$(uname)
|
||||
|
||||
##############################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
##############################################
|
||||
|
||||
verify_copy ()
|
||||
{
|
||||
# set -x
|
||||
|
||||
MYLOGFILE=${WORK_DIR}/verify_rsync_copy_day.log
|
||||
>$MYLOGFILE
|
||||
ERROR=0
|
||||
|
||||
# Enclose this loop so we can redirect output to the log file
|
||||
# with one assignment at the bottom of the function
|
||||
|
||||
{
|
||||
|
||||
# Put a header for the verification log file
|
||||
|
||||
echo -e "\nRsync copy verification between $THIS_HOST and machine(s) \
|
||||
$MACHINE_LIST\n\n" >> $MYLOGFILE
|
||||
|
||||
# Loop through each machine in the $MACHINE_LIST
|
||||
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
# Loop through each filesystem that matches the regular
|
||||
# regular expression point to by "$FS_PATTERN". It is
|
||||
# important that this variable is enclosed within double
|
||||
# quotes. Also note the column for the mount point is 6;
|
||||
# in AIX this should be changed to 7.
|
||||
|
||||
for MP in $(df | grep "$FS_PATTERN" | awk '{print $6}')
|
||||
do
|
||||
# For each filesystem mount point, $MP, execute a
|
||||
# find command to find all files in the filesystem.
|
||||
# and store them in the $LS_FILES file. We will use this
|
||||
# file list to verify the file sizes on the remote
|
||||
# machines match the file sizes on the local machine.
|
||||
|
||||
find $MP -type f > $LS_FILES
|
||||
|
||||
# Loop through the file list
|
||||
|
||||
for FL in $(cat $LS_FILES)
|
||||
do
|
||||
# Find the local file size
|
||||
LOC_FS=$(ls -l $FL | awk '{print $5}' 2>&1)
|
||||
# Find the remote file size using a remote shell command
|
||||
REM_FS=$(rsh $M ls -l $FL | awk '{print $5}' 2>&1)
|
||||
echo "Checking File: $FL"
|
||||
echo -e "Local $THIS_HOST size:\t$LOC_FS"
|
||||
echo -e "Remote $M size:\t$REM_FS"
|
||||
|
||||
# Ensure the file sizes match
|
||||
if [ "$LOC_FS" -ne "$REM_FS" ]
|
||||
then
|
||||
echo "ERROR: File size mismatch between $THIS_HOST and $M"
|
||||
echo "File is: $FL"
|
||||
ERROR=1
|
||||
fi
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
if (( ERROR != 0 ))
|
||||
then
|
||||
# Record the failure in the log file
|
||||
echo -e "\n\nRSYNC ERROR: $THIS_HOST Rsync copy failed...file size mismatch...\n\n" | tee -a $MYLOGFILE
|
||||
echo -e "\nERROR: Rsync copy Failed!"
|
||||
echo -e "\n\nCheck log file: $MYLOGFILE\n\n"
|
||||
echo -e "\n...Exiting...\n"
|
||||
return 3
|
||||
else
|
||||
echo -e "\nSUCCESS: Rsync copy completed successfully..."
|
||||
echo -e "\nAll file sizes match...\n"
|
||||
fi
|
||||
} | 2>&1 tee -a $MYLOGFILE
|
||||
|
||||
}
|
||||
|
||||
##############################################
|
||||
|
||||
elapsed_time ()
|
||||
{
|
||||
SEC=$1
|
||||
(( SEC < 60 )) && echo -e "[Elapsed time: $SEC seconds]\c"
|
||||
|
||||
(( SEC >= 60 && SEC < 3600 )) && echo -e "[Elapsed time: $(( SEC / 60 )) min $(( SEC % 60 )) sec]\c"
|
||||
|
||||
(( SEC > 3600 )) && echo -e "[Elapsed time: $(( SEC / 3600 )) hr $(( (SEC % 3600) / 60 )) min $(( (SEC % 3600) % 60 )) sec]\c"
|
||||
}
|
||||
|
||||
##############################################
|
||||
# BEGINNING OF MAIN
|
||||
##############################################
|
||||
|
||||
# Enclose the entire main part of the script in
|
||||
# curly braces so we can redirect all output of
|
||||
# the shell script with a single redirection
|
||||
# at the bottom of the script to the $LOGFILE
|
||||
|
||||
{
|
||||
|
||||
# Save a copy of the last log file
|
||||
|
||||
cp -f $LOGFILE ${LOGFILE}.yesterday
|
||||
|
||||
echo -e "\n[[ $THIS_SCRIPT started execution $(date) ]]\n"
|
||||
|
||||
# Ensure that target machines are defined
|
||||
|
||||
if [ -z "$MACHINE_LIST" ]
|
||||
then
|
||||
echo -e "\nERROR: No machines are defined to copy data to..."
|
||||
echo "...Unable to continue...Exiting..."
|
||||
exit 4
|
||||
fi
|
||||
|
||||
# Ensure the remote machines are reachable through
|
||||
# the network by sending 1 ping.
|
||||
|
||||
echo -e "Verifying the target machines are pingable...\n"
|
||||
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
echo "Pinging $M..."
|
||||
ping -c1 $M >/dev/null 2>&1
|
||||
if (( $? != 0 ))
|
||||
then
|
||||
echo -e "\nERROR: $M host is not pingable...cannot continue..."
|
||||
echo -e "...EXITING...\n"
|
||||
exit 2
|
||||
else
|
||||
echo "Pinging $M succeeded..."
|
||||
fi
|
||||
done
|
||||
|
||||
# Start all of the rsync copy sessions at once by looping
|
||||
# through each of the mount points and issuing an rsync
|
||||
# command in the background, and incrementing a counter.
|
||||
|
||||
echo -e "\nStarting a bunch of rsync sessions...\n"
|
||||
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
# Loop through each filesystem that matches the regular
|
||||
# expression point to by "$FS_PATTERN". It is
|
||||
# important that this variable is enclosed within double
|
||||
# quotes. Also note the column for the mount point is 6;
|
||||
# in AIX this should be changed to 7.
|
||||
|
||||
for MP in $(df | grep "$FS_PATTERN" | awk '{print $6}')
|
||||
do
|
||||
# Start the rsync session in the background
|
||||
|
||||
time rsync -avz ${MP}/ ${M}:${MP} 2>&1 &
|
||||
|
||||
# Keep a running total of the number of rsync
|
||||
# sessions started
|
||||
|
||||
(( TOTAL = TOTAL + 1 ))
|
||||
done
|
||||
done
|
||||
|
||||
# Sleep a few seconds before monitoring processes
|
||||
|
||||
sleep 10
|
||||
|
||||
# Find the number of rsync sessions that are still running
|
||||
|
||||
REM_SESSIONS=$(ps x | grep "rsync -avz" | grep "$FS_PATTERN" \
|
||||
| grep -v grep | awk '{print $1}' | wc -l)
|
||||
|
||||
# Give some feedback to the end user.
|
||||
|
||||
if (( REM_SESSIONS > 0 ))
|
||||
then
|
||||
echo -e "\n$REM_SESSIONS of $TOTAL rsync copy sessions require \
|
||||
further updating..."
|
||||
echo -e "\nProcessing rsync copies from $THIS_HOST to both \
|
||||
$MACHINE_LIST machines"
|
||||
echo -e "\nPlease be patient, this process may take a very long \
|
||||
time...\n"
|
||||
echo -e "Rsync is running [Start time: $(date)]\c"
|
||||
else
|
||||
echo -e "\nAll files appear to be in sync...verifying file sizes...\
|
||||
Please wait...\n"
|
||||
fi
|
||||
|
||||
# While the rsync processes are executing this loop
|
||||
# will place a . (dot) every 60 seconds as feedback
|
||||
# to the end user that the background rsync copy
|
||||
# processes are still executing. When the remaining
|
||||
# rsync sessions are less than the total number of
|
||||
# sessions (normally 36) this loop will give feedback
|
||||
# as to the number of remaining rsync session, as well
|
||||
# as the elapsed time of the processing, every 5 minutes..
|
||||
|
||||
SECONDS=10
|
||||
MIN_COUNTER=0
|
||||
|
||||
# Loop until all of the local rsync sessions have completed
|
||||
|
||||
until (( REM_SESSIONS == 0 ))
|
||||
do
|
||||
# sleep 60 seconds between loop iterations
|
||||
|
||||
sleep 60
|
||||
|
||||
# Display a dot on the screen every 60 seconds
|
||||
|
||||
echo -e ".\c"
|
||||
|
||||
# Find the number of remaining rsync sessions
|
||||
|
||||
REM_SESSIONS=$(ps x | grep "rsync -avz" | grep "$FS_PATTERN" \
|
||||
| grep -v grep | '{print $1}' | wc -l)
|
||||
|
||||
# Have any of the sessions completed? If so start giving
|
||||
# the user updates every 5 minutes, specifying the number
|
||||
# remaining rsync sessions and the elapsed time.
|
||||
|
||||
if (( REM_SESSIONS < TOTAL ))
|
||||
then
|
||||
# Count every 5 minutes
|
||||
(( MIN_COUNTER = MIN_COUNTER + 1 ))
|
||||
|
||||
# 5 minutes timed out yet?
|
||||
if (( MIN_COUNTER >= 5 ))
|
||||
then
|
||||
# Reset the minute counter
|
||||
MIN_COUNTER=0
|
||||
|
||||
# Update the user with a new progress report
|
||||
|
||||
echo -e "\n$REM_SESSIONS of $TOTAL rsync sessions \
|
||||
remaining $(elapsed_time $SECONDS)\c"
|
||||
|
||||
# Have three-fourths of the rsync sessions completed?
|
||||
|
||||
if (( REM_SESSIONS <= $(( TOTAL / 4 )) ))
|
||||
then
|
||||
# Display the list of remaining rsync sessions
|
||||
# that continue to run.
|
||||
|
||||
echo -e "\nRemaining rsync sessions include:\n"
|
||||
ps x | grep "rsync -avz" | grep "$FS_PATTERN" \
|
||||
| grep -v grep
|
||||
echo
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
echo -e "\n...Local rsync processing completed on $THIS_HOST...$(date)"
|
||||
echo -e "\n...Checking remote target machines: $MACHINE_LIST..."
|
||||
|
||||
# Just because the local rsync processes have completed does
|
||||
# not mean that the remote rsync processes have completed
|
||||
# execution. This loop verifies that all of the remote
|
||||
# rsync processes completed execution. The end user will
|
||||
# receive feedback if any of the remote processes are running.
|
||||
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
# Loop through each matching filesystem
|
||||
|
||||
for MP in $(df | grep "$FS_PATTERN" | awk '{print $7}')
|
||||
do
|
||||
# Find the remote process Ids.
|
||||
RPID=$(rsh $M ps x | grep rsync | grep $MP | grep -v grep \
|
||||
| awk '{print $1}')
|
||||
|
||||
# Loop while remote rsync sessions continue to run.
|
||||
# NOTE: I have never found remote rsync sessions running
|
||||
# after the local sessions have completed. This is just
|
||||
# an extra sanity check
|
||||
|
||||
until [ -z "$RPID" ]
|
||||
do
|
||||
echo "rsync is processing ${MP} on ${M}...sleeping one minute..."
|
||||
sleep 60
|
||||
RPID=$(rsh $M ps x | grep rsync | grep $MP | grep -v grep \
|
||||
| awk '{print $1}')
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
echo -e "\n...Remote rsync processing completed $(date)\n"
|
||||
|
||||
# Verify the copy process
|
||||
|
||||
verify_copy
|
||||
|
||||
# Check the verify_copy return code
|
||||
|
||||
if (( $? != 0 ))
|
||||
then
|
||||
exit 3
|
||||
fi
|
||||
|
||||
echo -e "\nRsync copy completed $(date)"
|
||||
|
||||
echo -e "\n[[ $THIS_SCRIPT completed execution $(date) ]]\n"
|
||||
echo -e "\n[[ Elapsed Time: $(elapsed_time $SECONDS) ]]\n"
|
||||
|
||||
} | 2>&1 tee -a $LOGFILE
|
||||
|
||||
###############################################
|
||||
# END OF SCRIPT
|
||||
###############################################
|
||||
33
chapter7/generic_rsync.bash
Executable file
33
chapter7/generic_rsync.bash
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# SCRIPT: generic_rsync.bash
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 11/18/2007
|
||||
# REV: 1.0
|
||||
# PURPOSE: This is a generic shell script to copy files
|
||||
# using rsync.
|
||||
#
|
||||
# set -n # Uncomment to check script syntax without execution
|
||||
# set -x # Uncomment to debug this script
|
||||
#
|
||||
# REV LIST:
|
||||
#
|
||||
#
|
||||
##############################################
|
||||
# DEFINE FILES AND VARIABLES HERE
|
||||
##############################################
|
||||
|
||||
# Define the source and destination files/directories
|
||||
|
||||
SOURCE_FL="/scripts/"
|
||||
DESTIN_FL="booboo:/scripts"
|
||||
|
||||
##############################################
|
||||
# BEGINNING OF MAIN
|
||||
##############################################
|
||||
|
||||
# Start the rsync copy
|
||||
|
||||
rsync -avz "$SOURCE_FL" "$DESTIN_FL"
|
||||
|
||||
# End of generic_rsync.bash
|
||||
545
chapter7/rsync_daily_copy.ksh
Executable file
545
chapter7/rsync_daily_copy.ksh
Executable file
@@ -0,0 +1,545 @@
|
||||
#!/bin/ksh
|
||||
#
|
||||
# SCRIPT: rsync_daily_copy.ksh
|
||||
# AUTHOR: Randy Michael
|
||||
# DATE: 11/10/2007
|
||||
# REV: 3.2.Prod
|
||||
#
|
||||
# PURPOSE: This script is used to replicate Oracle .dbf
|
||||
# files between the "master" DB server and the two
|
||||
# OLTP Oracle servers. The copy method used is
|
||||
# rsync. For this method to work, the Oracle DBA must
|
||||
# first put the database tables that reside in the
|
||||
# copy filesystems into READ-ONLY mode. Before starting
|
||||
# rsync copy sessions, this script searches for a file,
|
||||
# defined by the $READYTORUN_FILE variable, that is
|
||||
# placed on the system by the Oracle DBA team; when
|
||||
# the file is found this script will execute
|
||||
# all 36 rsync sessions, 18 to each OLTP server,
|
||||
# at the same time, then waits for all sessions to
|
||||
# complete, both locally and on the remote servers.
|
||||
# After the rsync copy sessions complete the copy is
|
||||
# verified by matching file sizes between the master
|
||||
# copy file and the target copy files. When verified
|
||||
# successful this script writes a file to the system,
|
||||
# defined by the $COMPLETE_FILE variable, to signal
|
||||
# the Oracle DBAs to put the DB back into READ-WRITE
|
||||
# mode, copy the metadata over, build the tables and
|
||||
# attach the DB on the OLTP side servers. Should a
|
||||
# failure occur a file, defined by the $RSYNCFAILED_FILE
|
||||
# variable, is written to the system to signal to the
|
||||
# Oracle DBA team an rsync copy process failure.
|
||||
#
|
||||
#
|
||||
# EXIT CODES:
|
||||
# 0 ==> Normal execution.
|
||||
# 1 ==> The value assigned to DAY is not
|
||||
# an integer 1 or 2.
|
||||
# 2 ==> Remote host is not pingable.
|
||||
# 3 ==> Copy verification failed. One or
|
||||
# more local files are not the same
|
||||
# size as the remote files.
|
||||
# 4 ==> No machines are defined to copy
|
||||
# data to.
|
||||
# 5 ==> Exited on a trapped signal.
|
||||
#
|
||||
# set -x # Uncomment to debug this script
|
||||
#
|
||||
# set -n # Uncomment to check the script's syntax
|
||||
# # without any execution. Do not forget to
|
||||
# # recomment this line!
|
||||
#
|
||||
########################################
|
||||
# REVISION LIST:
|
||||
########################################
|
||||
#
|
||||
# IF YOU MODIFY THIS SCRIPT DOCUMENT THE CHANGE(S)!!!
|
||||
#
|
||||
########################################
|
||||
#
|
||||
# Revised by: Randy Michael
|
||||
# Revision Date: 7/23/2007
|
||||
# Revision: Changed the script to process
|
||||
# all 36 mount points at a time.
|
||||
#
|
||||
########################################
|
||||
#
|
||||
# Revised by:
|
||||
# Revision Date:
|
||||
# Revision:
|
||||
#
|
||||
##############################################
|
||||
# DEFINE FILES AND GLOBAL VARIABLES HERE
|
||||
##############################################
|
||||
|
||||
typeset -i DAY
|
||||
EMAIL_FROM=data_support@gamma
|
||||
export PATH=$PATH:/usr/local/bin
|
||||
WORK_DIR=/usr/local/bin
|
||||
LOGFILE=${WORK_DIR}/rsync_daily_copy.log
|
||||
SEARCH_DIR=/orabin/apps/oracle/dbadm/general/bin
|
||||
READYTORUN_FILE=${SEARCH_DIR}/readytocopy.txt
|
||||
COMPLETE_FILE=${SEARCH_DIR}/copycomplete.txt
|
||||
RSYNCFAILED_FILE=${SEARCH_DIR}/copyfailed.txt
|
||||
MAILMESSAGEFILE=${WORK_DIR}/email_message.out
|
||||
THIS_SCRIPT=$(basename $0)
|
||||
BG_PID_LIST=
|
||||
TOTAL=0
|
||||
THIS_HOST=$(hostname)
|
||||
[[ $THIS_HOST = gamma ]] && MACHINE_LIST="alpha-rsync bravo-rsync"
|
||||
[[ $THIS_HOST = gamma-dg ]] && MACHINE_LIST="alpha-rsync bravo-rsync"
|
||||
|
||||
# Setup the correct echo command usage. Many Linux
|
||||
# distributions will execute in BASH even if the
|
||||
# script specifies Korn shell. BASH shell requires
|
||||
# we use echo -e when we use \n, \c, etc.
|
||||
|
||||
case $SHELL in
|
||||
*/bin/bash) alias echo="echo -e"
|
||||
;;
|
||||
esac
|
||||
|
||||
##############################################
|
||||
# DEFINE FUNCTIONS HERE
|
||||
##############################################
|
||||
|
||||
usage ()
|
||||
{
|
||||
echo "\nUSAGE: $THIS_SCRIPT Day"
|
||||
echo "\nWhere Day is 1 or 2\n"
|
||||
}
|
||||
|
||||
##############################################
|
||||
|
||||
cleanup_exit ()
|
||||
{
|
||||
# If this script is executing, then something failed!
|
||||
|
||||
[ $1 ] && EXIT_CODE=$1
|
||||
|
||||
echo "\n$THIS_SCRIPT is exiting on non-zero exit code: $EXIT_CODE"
|
||||
echo "\nPerforming cleanup..."
|
||||
echo "Removing $READYTORUN_FILE"
|
||||
rm -f $READYTORUN_FILE >/dev/null 2>&1
|
||||
echo "Removing $COMPLETE_FILE"
|
||||
rm -f $COMPLETE_FILE >/dev/null 2>&1
|
||||
echo "\nCreating $RSYNCFAILED_FILE"
|
||||
echo "\nRsync failed on $THIS_HOST with exit code $EXIT_CODE $(date)\n"\
|
||||
| tee -a $RSYNCFAILED_FILE
|
||||
echo "\nCleanup Complete...Exiting..."
|
||||
return $EXIT_CODE
|
||||
exit $EXIT_CODE
|
||||
}
|
||||
|
||||
##############################################
|
||||
|
||||
trap_exit ()
|
||||
{
|
||||
echo "\nERROR: EXITING ON A TRAPPED SIGNAL!\n"
|
||||
echo "\nRSYNC ERROR: $THIS_HOST -- Rsync process exited abnormally on a \
|
||||
trapped signal $(date)!" > $MAILMESSAGEFILE
|
||||
|
||||
mailx -r "$EMAIL_FROM" -s "SYNC ERROR: $THIS_HOST -- Rsync process exited \
|
||||
abnormally on a trapped signal!" data_support < $MAILMESSAGEFILE
|
||||
|
||||
sleep 2 # Allow the email to go out
|
||||
cleanup_exit 5
|
||||
return 5
|
||||
exit 5
|
||||
}
|
||||
|
||||
##############################################
|
||||
|
||||
verify_copy ()
|
||||
{
|
||||
#set -x
|
||||
|
||||
MYLOGFILE=${WORK_DIR}/verify_rsync_copy_day${DAY}.log
|
||||
>$MYLOGFILE
|
||||
ERROR=0
|
||||
|
||||
# Enclose this loop so we can redirect output to the log file
|
||||
# with one assignment at the bottom of the function
|
||||
{
|
||||
# Put a header for the verification log file
|
||||
|
||||
echo "\nRsync copy verification between $THIS_HOST and machines $MACHINE_LIST\n\n" >$MYLOGFILE
|
||||
|
||||
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
for LOC_MP in $(df | grep oradata_dm_[0-2][0-9] | awk '{print $7}')
|
||||
do
|
||||
LS_FILES=$(find $LOC_MP -type f)
|
||||
for FL in $LS_FILES
|
||||
do
|
||||
LOC_FS=$(ls -l $FL | awk '{print $5}' 2>&1)
|
||||
|
||||
# This sed statement changes the "m" to $DAY
|
||||
REM_FL=$(echo $FL | sed s/oradata_dm_/oradata_d${DAY}_/g)
|
||||
|
||||
REM_FS=$(rsh $M ls -l $REM_FL | awk '{print $5}' 2>&1)
|
||||
echo "Checking File: $FL"
|
||||
echo "Local $THIS_HOST size:\t$LOC_FS"
|
||||
echo "Checking Remote File: $REM_FL"
|
||||
echo "$M size:\t$REM_FS"
|
||||
if [ "$LOC_FS" -ne "$REM_FS" ]
|
||||
then
|
||||
echo "ERROR: File size mismatch between $THIS_HOST and $M"
|
||||
echo "File is: $FL"
|
||||
ERROR=1
|
||||
fi
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
if (( ERROR != 0 ))
|
||||
then
|
||||
# Record the failure in the log file
|
||||
|
||||
echo "\n\nRSYNC ERROR: $THIS_HOST Rsync copy failed...file size \
|
||||
mismatch...\n\n" | tee -a $MYLOGFILE
|
||||
|
||||
# Send email notification with file size log
|
||||
|
||||
mailx -r "$EMAIL_FROM" -s "RSYNC ERROR: $THIS_HOST Rsync copy failed\
|
||||
...file size mismatch -- log attached" data_support < $MYLOGFILE
|
||||
|
||||
echo "\nERROR: Rsync copy Failed!"
|
||||
echo "\n\nCheck log file: $MYLOGFILE\n\n"
|
||||
echo "\n...Exiting...\n"
|
||||
cleanup_exit 3
|
||||
return 3
|
||||
else
|
||||
echo "\nSUCCESS: Rsync copy completed successfully..."
|
||||
echo "\nAll file sizes match...\n"
|
||||
fi
|
||||
} | tee -a $MYLOGFILE
|
||||
|
||||
}
|
||||
|
||||
########################################
|
||||
|
||||
ready_to_run ()
|
||||
{
|
||||
# set -x
|
||||
# This function looks for a file on the system
|
||||
# defined by the $READYTORUN_FILE variable. The
|
||||
# presents presence of this file indicates we are ready
|
||||
# to run this script. This file will contain a
|
||||
# number 1 or 2 identifying the day we are
|
||||
# working with.
|
||||
|
||||
if [ -r ${READYTORUN_FILE} ]
|
||||
then
|
||||
cat ${READYTORUN_FILE}
|
||||
else
|
||||
echo "NOT_READY"
|
||||
fi
|
||||
}
|
||||
|
||||
##############################################
|
||||
|
||||
elapsed_time ()
|
||||
{
|
||||
SEC=$1
|
||||
(( SEC < 60 )) && echo "[Elapsed time: $SEC seconds]\c"
|
||||
|
||||
(( SEC >= 60 && SEC < 3600 )) && echo "[Elapsed time: $(( SEC / 60 )) \
|
||||
min $(( SEC % 60 )) sec]\c"
|
||||
|
||||
(( SEC > 3600 )) && echo "[Elapsed time: $(( SEC / 3600 )) hr $(( (SEC % 3600) / 60 )) \
|
||||
min $(( (SEC % 3600) % 60 )) sec]\c"
|
||||
}
|
||||
|
||||
##############################################
|
||||
# BEGINNING OF MAIN
|
||||
##############################################
|
||||
|
||||
# Set a trap
|
||||
|
||||
trap 'trap_exit' 1 2 3 5 6 11 14 15 17
|
||||
|
||||
# Save the old log file
|
||||
|
||||
cp -f $LOGFILE ${LOGFILE}.yesterday \
|
||||
>$LOGFILE
|
||||
|
||||
# Enclose the entire main part of the script in
|
||||
# curly braces so we can redirect all output of
|
||||
# the shell script with a single redirection
|
||||
# at the bottom of the script to the $LOGFILE
|
||||
|
||||
{
|
||||
echo "\n[[ $THIS_SCRIPT started execution $(date) ]]\n"
|
||||
|
||||
# Ensure that target machines are defined
|
||||
|
||||
if [ -z "$MACHINE_LIST" ]
|
||||
then
|
||||
echo "\nERROR: No machines are defined to copy data to..."
|
||||
echo "\nRSYNC ERROR: $THIS_HOST has no machines are defined \
|
||||
to copy data to..." > $MAILMESSAGEFILE
|
||||
mailx -r "$EMAIL_FROM" -s "RSYNC ERROR: $THIS_HOST has no machines \
|
||||
defined to copy data to" data_support < $MAILMESSAGEFILE
|
||||
echo "...Unable to continue...Exiting..."
|
||||
|
||||
cleanup_exit 4
|
||||
exit 4
|
||||
fi
|
||||
|
||||
# Checking for currently running versions of this script
|
||||
|
||||
echo "Checking for Currently Runnning Versions of this Script"
|
||||
|
||||
MYPID=$$ # Capture this scripts PID
|
||||
|
||||
MYOTHERPROCESSES=$(ps -ef | grep $THIS_SCRIPT | grep -v $MYPID \
|
||||
| grep -v grep | awk '{print $2}')
|
||||
|
||||
if [[ "$MYOTHERPROCESSES" != "" ]]
|
||||
then
|
||||
echo "\WARNING: Another version of this script is running...Killing it!"
|
||||
echo "Killing the following processe(es)...$MYOTHERPROCESSES"
|
||||
kill -9 $MYOTHERPROCESSES
|
||||
echo "\nNOTICE: Sleeping for 1 minute to allow both local
|
||||
and remote rsync sessions to terminate, if they exist...\n"
|
||||
sleep 60 # Allow any rsync sessions to stop both locally and remotely
|
||||
else
|
||||
echo "No other versions running...proceeding"
|
||||
fi
|
||||
|
||||
# Remove the file that indicates the rsync copy
|
||||
# is mounted and ready to use, if it exists.
|
||||
|
||||
rm -f $COMPLETE_FILE >/dev/null 2>&1
|
||||
|
||||
# Remove the file that indicates the rsync copy
|
||||
# failed and exited on a non-zero exit code, if
|
||||
# it exists.
|
||||
|
||||
rm -f $RSYNCFAILED_FILE >/dev/null 2>&1
|
||||
|
||||
|
||||
# Search for the file indicating we are ready to proceed
|
||||
|
||||
# Send notification
|
||||
|
||||
echo "\nDaily Rsync Copy Process Began on Host $THIS_HOST $(date)" \
|
||||
> $MAILMESSAGEFILE
|
||||
echo "\nStarted looking for $READYTORUN_FILE" >> $MAILMESSAGEFILE
|
||||
|
||||
mailx -r "$EMAIL_FROM" -s "Rsync Copy Process Began Looking for Startup \
|
||||
File on $THIS_HOST" data_support < $MAILMESSAGEFILE
|
||||
|
||||
echo "\nSearching for the file: ${READYTORUN_FILE}"
|
||||
RUN_STATUS=$(ready_to_run)
|
||||
until [[ $RUN_STATUS != "NOT_READY" ]]
|
||||
do
|
||||
date
|
||||
echo "$READYTORUN_FILE is not present...Sleeping 5 minutes..."
|
||||
sleep 300
|
||||
RUN_STATUS=$(ready_to_run)
|
||||
done
|
||||
date
|
||||
echo "Found file: $READYTORUN_FILE -- Ready to proceed..."
|
||||
DAY=$RUN_STATUS
|
||||
|
||||
|
||||
# Test the value assigned to the DAY variable
|
||||
|
||||
echo "Testing variable assignment for the DAY variable"
|
||||
|
||||
if (( DAY != 1 && DAY != 2 ))
|
||||
then
|
||||
echo "\nRSYNC ERROR: $THIS_HOST -- The value assigned to \
|
||||
the DAY variable" > $MAILMESSAGEFILE
|
||||
echo "==> $DAY - is not an integer 1 or 2...Exiting..."
|
||||
>> $MAILMESSAGEFILE
|
||||
mailx -r "$EMAIL_FROM" -s "ERROR: $THIS_HOST -- Value assigned \
|
||||
to the DAY variable $DAY is invalid" data_support < $MAILMESSAGEFILE
|
||||
|
||||
usage
|
||||
cleanup_exit
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ensure the remote machines are reachable through
|
||||
# the network by sending 1 ping.
|
||||
|
||||
echo "Verifying the target machines are pingable...\n"
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
echo "Pinging $M..."
|
||||
ping -c1 $M >/dev/null 2>&1
|
||||
if (( $? != 0 ))
|
||||
then
|
||||
echo "RSYNC ERROR: $M is not pingable from $THIS_HOST" \
|
||||
> $MAILMESSAGEFILE
|
||||
echo "The rsync copy process cannot continue...Exiting" \
|
||||
>> $MAILMESSAGEFILE
|
||||
mailx -r "$EMAIL_FROM" -s "RSYNC ERROR: $M is not pingable
|
||||
from $THIS_HOST" data_support < $MAILMESSAGEFILE
|
||||
|
||||
echo "\nERROR: $M host is not pingable...cannot continue..."
|
||||
echo "...EXITING...\n"
|
||||
sleep 2
|
||||
cleanup_exit 2
|
||||
exit 2
|
||||
else
|
||||
echo "Pinging $M succeeded..."
|
||||
fi
|
||||
done
|
||||
|
||||
# Notify start of rsync processing by email
|
||||
|
||||
echo "Rsync Copy Process for Day $DAY Starting Execution on \
|
||||
$THIS_HOST $(date)" > $MAILMESSAGEFILE
|
||||
|
||||
mailx -r "$EMAIL_FROM" -s "Rsync copy process for day $DAY starting \
|
||||
on $THIS_HOST" data_support < $MAILMESSAGEFILE
|
||||
|
||||
# Start all of the rsync copy sessions at once by looping
|
||||
# through each of the mount points and issuing an rsync
|
||||
# command in the background, and incrementing a counter.
|
||||
|
||||
echo "\nStarting a bunch of rsync sessions...\n"
|
||||
|
||||
# This script copies from the DM filesystems to the
|
||||
# Day 1 or Day 2 filesystems on the OLTP servers.
|
||||
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
for LOC_MP in $(df | grep oradata_dm_[0-2][0-9] | awk '{print $7}')
|
||||
do
|
||||
# This sed statement changes the "m" to $DAY
|
||||
REM_MP=$(echo $LOC_MP | sed s/m/$DAY/g)
|
||||
time rsync -avz ${LOC_MP}/ ${M}:${REM_MP} 2>&1 &
|
||||
(( TOTAL = TOTAL + 1 ))
|
||||
done
|
||||
done
|
||||
|
||||
# Sleep a few seconds before monitoring processes
|
||||
|
||||
sleep 10
|
||||
|
||||
# Give some feedback to the end user.
|
||||
|
||||
REM_SESSIONS=$(ps -ef | grep "rsync -avz" | grep oradata_dm_[0-2][0-9] \
|
||||
| grep -v grep | awk '{print $2}' | wc -l)
|
||||
|
||||
if (( REM_SESSIONS > 0 ))
|
||||
then
|
||||
echo "\n$REM_SESSIONS of $TOTAL rsync copy sessions require further updating..."
|
||||
echo "\nProcessing rsync copies from $THIS_HOST to both $MACHINE_LIST machines"
|
||||
echo "\nPlease be patient, this process may take longer than two hours...\n"
|
||||
echo "Rsync is running [Start time: $(date)]\c"
|
||||
else
|
||||
echo "\nAll files appear to be in sync...verifying file sizes...Please wait...\n"
|
||||
fi
|
||||
|
||||
# While the rsync processes are executing this loop
|
||||
# will place a . (dot) every 60 seconds as feedback
|
||||
# to the end user that the background rsync copy
|
||||
# processes are still executing. When the remaining
|
||||
# rsync sessions are less than the total number of
|
||||
# sessions (normally 36) this loop will give feedback
|
||||
# as to the number of remaining rsync session, as well
|
||||
# as the elapsed time of the procesing, every 5 minutes.
|
||||
|
||||
SECONDS=10
|
||||
MIN_COUNTER=0
|
||||
|
||||
until (( REM_SESSIONS == 0 ))
|
||||
do
|
||||
sleep 60
|
||||
echo ".\c"
|
||||
REM_SESSIONS=$(ps -ef | grep "rsync -avz" | grep oradata_dm_[0-2][0-9] \
|
||||
| grep -v grep | awk '{print $2}' | wc -l)
|
||||
if (( REM_SESSIONS < TOTAL ))
|
||||
then
|
||||
(( MIN_COUNTER = MIN_COUNTER + 1 ))
|
||||
if (( MIN_COUNTER >= 5 ))
|
||||
then
|
||||
MIN_COUNTER=0
|
||||
echo "\n$REM_SESSIONS of $TOTAL rsync sessions \
|
||||
remaining $(elapsed_time $SECONDS)\c"
|
||||
if (( REM_SESSIONS <= $(( TOTAL / 4 )) ))
|
||||
then
|
||||
echo "\nRemaining rsync sessions include:\n"
|
||||
ps -ef | grep "rsync -avz" | grep oradata_dm_[0-2][0-9] \
|
||||
| grep -v grep
|
||||
echo
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
echo "\n...Local rsync processing completed on $THIS_HOST...$(date)"
|
||||
echo "\n...Checking remote target machines: $MACHINE_LIST..."
|
||||
|
||||
# Just because the local rsync processes have completed does
|
||||
# not mean that the remote rsync processes have completed
|
||||
# execution. This loop verifies that all of the remote
|
||||
# rsync processes completed execution. The end user will
|
||||
# receive feedback if any of the remote processes are running.
|
||||
|
||||
for M in $MACHINE_LIST
|
||||
do
|
||||
for LOC_MP in $(df | grep oradata_dm_[0-2][0-9] | awk '{print $7}')
|
||||
do
|
||||
# This sed statement changes the "m" to $DAY
|
||||
REM_MP=$(echo $LOC_MP | sed s/m/$DAY/g)
|
||||
|
||||
RPID=$(rsh $M ps -ef | grep rsync | grep $REM_MP \
|
||||
| grep -v grep | awk '{print $2}')
|
||||
|
||||
until [ -z "$RPID" ]
|
||||
do
|
||||
echo "rsync is processing ${REM_MP} on ${M}...sleeping one minute..."
|
||||
sleep 60
|
||||
RPID=$(rsh $M ps -ef | grep rsync | grep $REM_MP \
|
||||
| grep -v grep | awk '{print $2}')
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
echo "\n...Remote rsync processing completed $(date)\n"
|
||||
|
||||
# Verify the copy process
|
||||
|
||||
verify_copy
|
||||
if (( $? != 0 ))
|
||||
then
|
||||
exit 3
|
||||
fi
|
||||
|
||||
# Write to the $COMPLETE_FILE to signal the Oracle DBAs
|
||||
# that the copy has completed
|
||||
|
||||
echo "Rsync copy from $THIS_HOST to machines $MACHINE_LIST \
|
||||
completed successfully $(date)" | tee -a $COMPLETE_FILE
|
||||
|
||||
# Remove the ready to run file
|
||||
|
||||
rm -f $READYTORUN_FILE >/dev/null 2>&1
|
||||
|
||||
# Notify completion by email
|
||||
|
||||
echo "Rsync Copy for Day $DAY Completed Successful Execution \
|
||||
on $THIS_HOST $(date)\n" > $MAILMESSAGEFILE
|
||||
elapsed_time $SECONDS >> $MAILMESSAGEFILE
|
||||
mailx -r "$EMAIL_FROM" -s "Rsync copy for day $DAY completed \
|
||||
successfully on $THIS_HOST" data_support < $MAILMESSAGEFILE
|
||||
|
||||
echo "\nRsync copy completed $(date)"
|
||||
|
||||
echo "\n[[ $THIS_SCRIPT completed execution $(date) ]]\n"
|
||||
|
||||
exit 0
|
||||
|
||||
} | 2>&1 tee -a $LOGFILE
|
||||
|
||||
###############################################
|
||||
# END OF SCRIPT
|
||||
###############################################
|
||||
Reference in New Issue
Block a user