Wednesday, January 25, 2012

Using rsync to deploy from staging server to production

cat rsync_a_to_b.sh
# script that synchronizes files from location A to location B via a local temp directory
USER_A=ec2-user
HOST_A="$1"
PATH_A="$2"
USER_B=ec2-user
HOST_B="$3"
PATH_B="$4"

if [ -z $HOST_A ]; then # -n tests to see if the argument is non empty
echo "Missing source host argument, usage: [cmd] sourceHost sourcePath targetHost targetPath"
exit 1
fi

if [ -z $PATH_A ]; then # -n tests to see if the argument is non empty
echo "Missing source path argument, usage: [cmd] sourceHost sourcePath targetHost targetPath"
exit 1
fi

if [ -z $HOST_B ]; then # -n tests to see if the argument is non empty
echo "Missing target host argument, usage: [cmd] sourceHost sourcePath targetHost targetPath"
exit 1
fi

if [ -z $PATH_B ]; then # -n tests to see if the argument is non empty
echo "Missing target path argument, usage: [cmd] sourceHost sourcePath targetHost targetPath"
exit 1
fi

LOCAL_DIR=/tmp/rsync/${HOST_A}${PATH_A}

echo STEP 1: Syncing from ${HOST_A}:${PATH_A} to localhost:${LOCAL_DIR}..
mkdir -p ${LOCAL_DIR}

rsync -e ssh -rcav --delete-after \
${USER_A}@${HOST_A}:${PATH_A} \
--exclude '.svn/**' \
${LOCAL_DIR}

echo STEP 2: Syncing from localhost:${LOCAL_DIR} to ${HOST_B}:${PATH_B}..

rsync -e ssh -rcav --delete-after \
$LOCAL_DIR/* \
${USER_B}@${HOST_B}:${PATH_B}

echo DONE.

------------------------------

Then write a script to use the above function for every directory, like this:

cat push_stage_to_prod.sh
BASE=./

SOURCE_HOST=[mystagehost]
TARGET_HOST=[myprodhost]

echo Pushing entire application codebase from ${SOURCE_HOST} to ${TARGET_HOST}...

$BASE/rsync_a_to_b.sh $SOURCE_HOST \
'/my/app/tomcat/webapps/ROOT/WEB-INF/*.xml' \
$TARGET_HOST \
/my/app/tomcat/webapps/ROOT/WEB-INF/

$BASE/rsync_a_to_b.sh $SOURCE_HOST \
'/my/app/tomcat/webapps/ROOT/WEB-INF/classes/*' \
$TARGET_HOST \
/my/app/tomcat/webapps/ROOT/WEB-INF/classes/

$BASE/rsync_a_to_b.sh $SOURCE_HOST \
'/my/app/tomcat/webapps/ROOT/WEB-INF/lib/*' \
$TARGET_HOST \
/my/app/tomcat/webapps/ROOT/WEB-INF/lib/

$BASE/rsync_a_to_b.sh $SOURCE_HOST \
'/my/app/lib/m2reposymlink/*' \
$TARGET_HOST \
/my/app/lib/m2reposymlink/

$BASE/rsync_a_to_b.sh $SOURCE_HOST \
'/my/app/webcode/*' \
$TARGET_HOST \
/my/app/webcode/

0 comments:

Stats