#! /bin/bash
#**********************************************************************
#* Auteur : Martin VINCENT (MVincent) [email protected]
#* Fichier : sync.scp
#* Répertoire : /ftp:[email protected]:/sacc/prod/cron/
#* Créé le : 20 juin 2007 15:35:00 (-0500)
#* Modifié le : 29 juillet 2011 12:55:01 (-0400) par : mvincent
#* Sauvegarde # : 36
#* Ver. CVS GMT : $Id: sync.scp,v 1.9 2009/11/10 21:18:20 admin Exp $
#**********************************************************************
# simplify synchronisation of data to servers, by default,
# without any params or arguments, it behaves like /usr/local/bin/sync_servers
################################################
## VARIABLES (SOME ARE DEFAULTS THAT COULD
## BE OVERWRITTEN BY PARAMETERS )
################################################
EXEC="/usr/bin/rsync "
EXECSSH="/usr/bin/ssh "
EXECSCP="/usr/bin/scp "
backupServersList="/www_sa/sacc/prod/php/conf/BACKUPserversList.txt";
execcfgServersList="/www_sa/sacc/prod/php/conf/EXECCFGserversList.txt";
dictioServersList="/www_sa/sacc/prod/php/conf/DICTIOserversList.txt";
SERVERS="";
# these can be overwritten by parameters on the CLI
EXCLUSIONS=" " # ex : EXCLUSIONS="--exclude=weblogs "
DEST="/local/htdocs "
SOURCE="/local/htdocs/ "
PARAMS=" -e /usr/bin/ssh -z -a "
VERBOSE=""
TMP_SERVERS=" "
BATCHMODE="0"
SERVERGROUPNAME=""
TMPDIR="/dev/shm/" #dir used for temp files in batch mode
# pour la fonction cecho plus bas
black='\E[30;38m'
red='\E[31;38m'
green='\E[32;38m'
yellow='\E[33;38m'
blue='\E[34;38m'
magenta='\E[35;38m'
cyan='\E[36;38m'
white='\E[37;38m'
bold="\033[1m"
NONE="\033[0;38m"
################################################
## MAIN FUNCTION
################################################
main()
{
# get default list
SERVERLIST="";
GetServersList $bakupServersList
SERVERLIST="$SERVERLIST $GSLSERVERS";
SERVERS=$SERVERLIST
# below, we override teh default list if the case be
# add -v if not quiet mode
PARAMS=$PARAMS$VERBOSE
#####
if [ "$TMP_EXCLUSIONS" != "" ]
then
EXCLUSIONS=$TMP_EXCLUSIONS
fi
#####
if [ "$TMP_SOURCE" != "" ]
then
SOURCE=$TMP_SOURCE
fi
#####
if [ "$TMP_DEST" != "" ]
then
DEST=$TMP_DEST
fi
#####
if [ "$TMP_PARAMS" != "" ]
then
PARAMS=$PARAMS$TMP_PARAMS
fi
#####
if [ "$TMP_SERVERS" != " " ]
then
SERVERS=$TMP_SERVERS
fi
# rsync en mode batch : devrait etre plus rapide surtout sur multitude de petits fichiers,
# les tests ont cependant jusqu'a maintenant demontrer le contraire :/
# Donc retester avant d'utiliser en prod
if [ "$BATCHMODE" == 1 ]
then
# transformation de la liste en tableau
SERVERS=( $SERVERS )
# faire write batch
BATCHFILENAME="$TMPDIR$SERVERGROUPNAME."`date +"%d%b%Y-%a-%Hh%Mm%Ns"`".tmp"
MACHINE=${SERVERS[0]};
WRITEPARAMS="--write-batch="$BATCHFILENAME" "
READPARAMS=" -a --read-batch=- "
cecho "INFO : synchronising " $NONE "-n";
cecho " $SOURCE" $cyan "-n";
cecho " on " $NONE "-n" ;
cecho "$MACHINE" $red "-n"
cecho ":$DEST" $cyan ""
if [ "$VERBOSE" != "" ]
then
cecho "INFO : using " $white "-n"
cecho "BATCH MODE " $yellow "-n"
cecho "command : $EXEC $WRITEPARAMS$PARAMS$EXCLUSIONS " $white "-n"
cecho "$SOURCE $MACHINE:$DEST" $white ""
fi
# DO THE ACTUAL RSYNC
$EXEC $WRITEPARAMS$PARAMS$EXCLUSIONS $SOURCE $MACHINE:$DEST
# on elimine le premier serveur, vu que la commande --write-batch l'a d�j� synchronis�
unset SERVERS[0]
for MACHINE in ${SERVERS[@]}
do
cecho "INFO : synchronising " $NONE "-n";
cecho " $SOURCE" $cyan "-n";
cecho " on " $NONE "-n" ;
cecho "$MACHINE" $red "-n"
cecho ":$DEST" $cyan ""
if [ "$VERBOSE" != "" ]
then
cecho "INFO : using " $white "-n"
cecho "BATCH MODE " $yellow "-n"
cecho "command : $EXECSSH $MACHINE $EXEC " $white "-n"
cecho "$READPARAMS$PARAMS$EXCLUSIONS $DEST < $BATCHFILENAME" $white ""
fi
# DO THE ACTUAL RSYNC
$EXECSSH $MACHINE $EXEC $READPARAMS$PARAMS$EXCLUSIONS $DEST < $BATCHFILENAME
echo
done
rm $BATCHFILENAME
rm $BATCHFILENAME.sh
else
# rsync classique
for MACHINE in $SERVERS ;
do
cecho "INFO : synchronising " $NONE "-n";
cecho " $SOURCE" $cyan "-n";
cecho " on " $NONE "-n" ;
cecho "$MACHINE" $red "-n"
cecho ":$DEST" $cyan ""
if [ "$VERBOSE" != "" ]
then
cecho "INFO : using command : $EXEC $PARAMS$EXCLUSIONS $SOURCE" $white "-n"
cecho "$MACHINE:$DEST" $white ""
fi
# DO THE ACTUAL RSYNC
$EXEC $PARAMS$EXCLUSIONS $SOURCE $MACHINE:$DEST
echo
done
fi
}
################################################
## FUNCTIONS
################################################
# set up a 'usage' routine
# ------------------------
usage()
{
[ "$1" ] && ( echo $* ; echo "" )
cat <<!
Usage : $0 [ -e "exclusion"] [ -d destination_dir ] [ -s source_dir ]
[ -p rsync_parameters ] [ -h for help] [ -v for verbose mode] [ -b for batch mode (faster ?) ]
[ -g "SAbackupServers" | "EXECCFGservers" | "DICTIOservers" ] [server_list]
NOTES : DEFAULT behavior (withour any parameter) is similar to
/usr/local/bin/sync_servers
ie : "$EXEC $PARAMS$EXCLUSIONS $SOURCE [SERVER]:$DEST"
: -e options must be enclosed in "", especially if they contain regexp { ie : configs_* }
: -e, -p and -v must be invoked more that once to specify more than one
exclusion, or parameter or increase verbosity
: -g : select group of Feed servers or SAcc servers
: -b : batch mode, use rsync\'s --write/read-batch for (faster ?) transfers
!
exit 4
}
################################################
# This function will tell us if the line has a comment or
# blank line and need to be skipped - $1 is the line in the file
# Returns 1 indicates skip the line else dont skip
################################################
CanLineBeSkipped()
{
echo $line | grep "^#" > /dev/null
if [ $? == 0 ]
then
return 1
fi
if [ "$line" == "" ]
then
return 1
fi
return 0
}
################################################
# GETSERVERSLIST FUNCTION
################################################
GetServersList()
{
serversList=$1;
GSLSERVERS="";
if [ -r $serversList ]
then
while read line; do
CanLineBeSkipped $line
if [ "$?" == "1" ] || [ "$?" == "2" ]
then
continue
fi
GSLSERVERS=$GSLSERVERS" ${line}";
done < $serversList
GSLSERVERS=$GSLSERVERS" ${line}";
else
MESSAGE="$serversList is not readable.";
echo $MESSAGE
fi
return 0;
}
################################################
# CECHO FUNCTION : outputs echo with color
################################################
cecho() # Color-echo.
# Argument $1 = message
# Argument $2 = color
{
local default_msg=""
# Doesn't really need to be a local variable.
local noCarriageReturn=""
message=${1:-$default_msg} # Defaults to default message.
color=${2:-$white} # Defaults to black, if not specified.
carriageReturn=${3:-$noCarriageReturn} # Defaults to no $noCarriageReturn
echo -n -e "$bold$color"
echo $carriageReturn "$message"
echo -n -e $NONE
return
}
# collect options and arguments
while getopts ":e:d:s:p:g:hvb" optname
do
case "$optname" in
"e")
echo "INFO : Option $optname is specified with value ${OPTARG} "
TMP_EXCLUSIONS=$TMP_EXCLUSIONS"--exclude=${OPTARG} "
;;
"d")
echo "INFO : Option $optname is specified with value ${OPTARG} "
TMP_DEST=$TMP_DEST"${OPTARG} "
;;
"s")
echo "INFO : Option $optname is specified with value ${OPTARG} "
TMP_SOURCE="${OPTARG} "
;;
"p")
echo "INFO : Option $optname is specified with value ${OPTARG} "
TMP_PARAMS=$TMP_PARAMS"${OPTARG} "
;;
"v")
echo "INFO : Option $optname is specified : verbose output is ON "
VERBOSE=$VERBOSE"-v "
;;
"b")
echo "INFO : Option $optname is specified : rsync BATCH MODE is ON "
BATCHMODE="1"
;;
"g")
echo "INFO : Option $optname is specified with value ${OPTARG} "
SERVERGROUPNAME=$OPTARG
if [ "$OPTARG" = "SAbackupServers" ]
then
GetServersList $backupServersList
SABACKUPSERVERS=$GSLSERVERS;
TMP_SERVERS=$SABACKUPSERVERS
fi
if [ "$OPTARG" = "EXECCFGservers" ]
then
GetServersList $execcfgServersList
EXECCFGSERVERS=$GSLSERVERS;
TMP_SERVERS=$EXECCFGSERVERS
fi
if [ "$OPTARG" = "DICTIOservers" ]
then
GetServersList $dictioServersList
DICTIOSERVERS=$GSLSERVERS;
TMP_SERVERS=$DICTIOSERVERS
fi
;;
"h")
usage
;;
"?")
echo "ERROR : Unknown option $OPTARG, try < $0 -h > for help"
;;
":")
echo "ERROR : No argument value for option $OPTARG, try < $0 -h > for help"
exit 3
;;
*)
# Should not occur
echo "ERROR : Unknown error while processing options, try < $0 -h > for help"
;;
esac
done
# OPTIND is now pointing at first argument
for p in "${@:$OPTIND}"
do
TMP_SERVERS=$TMP_SERVERS"$p "
done
# finally call main
echo "INFO : starting on " `date +"%Y%m%d-%a-%Hh%Mm%Ss"` ;
time main
echo "INFO : done on " `date +"%Y%m%d-%a-%Hh%Mm%Ss"` ;
# http://www.ibm.com/developerworks/library/l-bash-parameters.html?ca=drs-
# prints options
# ------------------------
#showopts () {
# while getopts ":e:" optname
# do
# case "$optname" in
# "e")
# echo "Option $optname is specified with value for option $OPTARG "
# TMP_EXCLUSIONS=$TMP_EXCLUSIONS" --exclude=$OPTARG"
# ;;
# "?")
# echo "Unknown option $OPTARG"
# ;;
# ":")
# echo "No argument value for option $OPTARG"
# ;;
# *)
# # Should not occur
# echo "Unknown error while processing options"
# ;;
# esac
# done
#
# return $OPTIND
#}
# prints arguments
# ------------------------
#showargs () {
# for p in "$@"
# do
# echo "[$p]"
# done
#}
# optinfo contient l'output des 'echo' de la f() showopts
# optinfo=$(showopts "$@")
# $? contient la val de OPTIND retourne par la f() showopts
# argstart=$?
# arginfo contient l'output des 'echo' de la f() showargs
#arginfo=$(showargs "${@:$argstart}")
#echo "Arguments are:"
#echo "$arginfo"
#echo "Options are:"
#echo "$optinfo"
vendredi 29 juillet 2011
sync.scp
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire