Skip to content

Instantly share code, notes, and snippets.

@pisceanfoot
Created August 2, 2016 03:45
Show Gist options
  • Save pisceanfoot/bba4d32709188c75cb17452571f2954d to your computer and use it in GitHub Desktop.
Save pisceanfoot/bba4d32709188c75cb17452571f2954d to your computer and use it in GitHub Desktop.
rsync copy
echo '------------------------'
echo 'start copy and restart'
echo '------------------------'
SERVICE_NAME=$1
echo ------------------------
echo $WORKSPACE
echo 'target server:' $Live_WEB
echo 'remote user:' $REMOTE_USER
echo 'service name' $SERVICE_NAME
echo ------------------------
# check environment
ssh $REMOTE_USER@$Live_WEB << END
if [ ! -d "website" ]; then
mkdir website
[ \$? -gt 0 ] && echo "create folder website error" && exit 1
fi
cd website
if [ ! -d "$SERVICE_NAME" ]; then
mkdir $SERVICE_NAME
[ \$? -gt 0 ] && echo "create folder service error:" $SERVICE_NAME && exit 1
fi
exit 0
END
[ $? -gt 0 ] && exit 1
echo ------------------------
echo 'start copy from: ' ${WORKSPACE}
echo 'to ' $REMOTE_USER@$Live_WEB:~/website/$SERVICE_NAME
echo ------------------------
if [ -f "${WORKSPACE}/rsync-exclude.txt" ]; then
echo 'use rsync and exclude files'
echo "${WORKSPACE}/rsync-exclude.txt"
rsync -rlptz --delete-after --exclude-from "${WORKSPACE}/rsync-exclude.txt" ${WORKSPACE}/ $REMOTE_USER@$Live_WEB:~/website/$SERVICE_NAME
[ $? -gt 0 ] && echo "copy to remote server error" && exit 1
else
echo 'use scp'
rsync -rlptz --delete-after ${WORKSPACE}/ $REMOTE_USER@$Live_WEB:~/website/$SERVICE_NAME
[ $? -gt 0 ] && echo "copy to remote server error" && exit 1
fi
echo "end copy ================="
# try restart node
ssh $REMOTE_USER@$Live_WEB << END
echo 'startup on remote server'
if [ -n "$PORT" ]; then
echo 'use port' $PORT
export PORT
fi
if [ -n "$APP_NAME" ]; then
echo 'set app name' $APP_NAME
export APP_NAME
fi
if [ -n "$SERVICE_FARM" ]; then
echo 'set farm' $SERVICE_FARM
export SERVICE_FARM
fi
cd ~/website/$SERVICE_NAME
[ \$? -gt 0 ] && echo "folder not found ~/website/"$SERVICE_NAME && exit 1
echo 'restart node'
bin/stop.sh "$APP_NAME"
echo 'restart node completed'
sleep 1s
echo 'try start node'
bin/start.sh "$PORT" "$APP_NAME" "$SERVICE_FARM"
[ \$? -gt 0 ] && echo "copy to remote server error" && exit 1
pm2 save
exit 0
END
[ $? -gt 0 ] && exit 1
echo =======================end=================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment