一段部署shell
#!/usr/bin/env bash
# 目标服务器IP
HOST=$1
# 目标服务器端口
PORT=225
# 部署目录
DEPLOY_DIR=$2
# 环境
ENV=$3
# 备份目录
BAK_DIR="/deploy_bak/"
BAK_FILE="${BAK_DIR}api_"$(date +%Y%m%d%H%M%S)".tar.gz"
RUNTIME_DIR="${DEPLOY_DIR}/runtime"
echo "---部署开始---"
echo "---备份---"
# 仅备份config目录
ssh -p ${PORT} root@${HOST} "mkdir -p ${BAK_DIR} && cd ${DEPLOY_DIR} && tar -cvzf ${BAK_FILE} config"
echo "---同步文件---"
ssh -p ${PORT} root@${HOST} "mkdir -p ${DEPLOY_DIR}"
rsync -raz --progress -e "ssh -p ${PORT}" src/* root@${HOST}:${DEPLOY_DIR}
#scp -r -P ${PORT} src/* root@${HOST}:${DEPLOY_DIR}
echo "---更新依赖---"
ssh -p ${PORT} root@${HOST} "cd ${DEPLOY_DIR} && /usr/local/php/bin/php /plattxu2/composer.phar install"
if [ "$ENV" != "" ]; then
echo "---创建配置---"
ssh -p ${PORT} root@${HOST} "cd ${DEPLOY_DIR} && rm -rf config && cp -r config.${ENV} config"
else
echo "---未创建配置---"
fi
echo "---配置文件权限---"
ssh -p ${PORT} root@${HOST} "chown -R root:root ${DEPLOY_DIR} && chmod -R 755 ${DEPLOY_DIR}"
ssh -p ${PORT} root@${HOST} "mkdir -p ${RUNTIME_DIR} && chmod -R 777 ${RUNTIME_DIR}"
echo "---部署结束---"