Skip to content

Instantly share code, notes, and snippets.

@gotnix
Created August 13, 2012 08:41
Show Gist options
  • Save gotnix/3338301 to your computer and use it in GitHub Desktop.
Save gotnix/3338301 to your computer and use it in GitHub Desktop.
Sync app directory.
#!/bin/bash
#
# SCRIPT: rsync_app
# AUTHOR: terry zheng
# E-MAIL: jfhgmv@gmail.com
# DATE: Mon Aug 13 10:33:38 2012
# REV: 1.0.A (Valid are A, B, D, T and P)
# (for Alpha, Beta, Dev, Test and Production)
#
# PLATFORM: GNU/Linux (SPECIFY: AIX, FreeBSD, GNU/Linux, HP-UX, Solaris
# or Not platform dependent)
#
# PURPOSE: Give a clear, and if necessary, long, description of the purpose
# of the shell script. This is will also help you stay focused
# on the task at hand.
#
# REV LIST:
# DATE: DATE_of_REVSION
# BY: AUTHOR_of_MODIFICATION
# MODIFICATION: Describe what was modified, new features, etc--
#
#
# set -n # Uncomment to check script syntax, without execution.
# # NOTE: Do not forget to put the comment back in or
# # the shell script will not execute!
#
# set -x # Uncommet to debug this shell script.
#
################################################################
# DEFINE FILES AND VARIABLES HERE
################################################################
PID_F="/var/tmp/run/rsync_app.pid"
LOG_F="/var/tmp/log/rsync_app-$(date +%Y%m%d).log"
SCRIPT=`basename ${0}`
USER="root"
DST_DIR="/tmp/test/"
APP1_REM_HOST_LST=("172.16.16.52" "172.16.16.53")
APP2_REM_HOST_LST=("172.16.16.54" "172.16.16.55")
APP1_LOC_DIR="/tmp/test/app1/"
APP2_LOC_DIR="/tmp/test/app2/"
################################################################
# DEFINE FUNCTIONS HERE
################################################################
ERRTRAP()
{
echo "[LINE:$1] Error: Command or function exited with status $?"
}
trap 'ERRTRAP $LINENO' ERR
## Define lock file
if [ -f ${PID_F} ] ; then
old_pid=$(cat ${PID_F})
ps -p ${old_pid} | grep ${old_pid} >/dev/null 2>&1
[ $? -eq 0 ] && \
echo "${SCRIPT} is already runnig, PID is ${old_pid}" && exit
else
echo $$ > ${PID_F} || exit 43
fi
trap "rm -f ${PID_F} ; exit" INT TERM EXIT
################################################################
sync_app ()
{
# Uesage: sync_app local_source_directory remote_server_address
if ! $(echo ${1} | grep -q '/$') ; then
SRC_DIR="${1}/"
else
SRC_DIR="${1}"
fi
REM_HOST=${2}
rsync -avz --progress --delete --log-file="${LOG_F}" ${SRC_DIR} ${USER}@${REM_HOST}:${DST_DIR} 2>&1 &
}
################################################################
# BEGINNING OF MAIN
################################################################
for app_name in $*
do
case ${app_name} in
app1)
for S in ${APP1_REM_HOST_LST[*]}
do
sync_app ${APP1_LOC_DIR} ${S}
done
;;
app2)
for S in ${APP2_REM_HOST_LST[*]}
do
sync_app ${APP2_LOC_DIR} ${S}
done
;;
*)
echo $"Useage: ${SCRIPT} <app1_name | app2_name | ... >, such as:"
echo "app1 == /tmp/test/app1/"
echo "app2 == /tmp/test/app2"
exit 44
;;
esac
done
echo "App synchronous completed."
rm -f ${PID_F}
exit 0
##---------------------- End of script -----------------------##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment