为实现一键发布,最好是可以免密码登陆服务器,通过创建RSA密钥对,将公钥写入目标服务器来实现免密登陆,参考这里。
一键发布
发布脚本 deploy.sh
#!/bin/sh
currentDir=`cd ..;pwd`
localJarDir="${currentDir}/target/"
# package send/receive/mail service
packageApp(){
cd ${currentDir}/send-service/
mvn package
cd ${currentDir}/receive-service/
mvn package
cd ${currentDir}/mail-service/
mvn package
# copy to target directory
if [ ! -d "$localJarDir" ]; then
mkdir -p ${localJarDir}
fi
echo "copy jar to $localJarDir"
cp ${currentDir}/send-service/target/*.jar ${localJarDir}
cp ${currentDir}/receive-service/target/*.jar ${localJarDir}
cp ${currentDir}/mail-service/target/*.jar ${localJarDir}
}
# upload & restart application
appPath="/app/jar/"
appBackPath="/app/jar/backup/"
user="hello"
aliEcs1="1.1.1.134"
aliEcs2="1.1.1.135"
# shutdown application
shutdownApp(){
ssh -tt ${user}@$1<<EOF
cd ${appPath}
sh shutdown.sh
exit
EOF
}
# start application
startApp(){
ssh -tt ${user}@$1<<EOF
cd ${appPath}
sh startup.sh
exit
EOF
}
# backup application
# ssh -tt ${user}@$1<<EOF
# cd $appPath
# jarFileList=`ls | grep jar | awk '{print $1}'`
# for file in $jarFileList; do
# if [ -f $file ]; then
# cp $file $appBackPath
# fi
# done
# exit
# EOF
# ssh $user@$1 'for i in /app/jar/*.jar ; do echo $i; done'
# fileDate=`date -r $file '+%Y-%m-%d' | awk '{print $1}'`
# cp $file "$appBackPath/$file.$fileDate"
backupApp(){
ssh -tt ${user}@$1<<EOF
cd ${appPath}
sh backup.sh
exit
EOF
}
# upload to server
# ftp or sftp
uploadFile(){
sftp ${user}@$1<<EOF
put -r ${localJarDir}*.jar ${appPath}
quit
EOF
}
# deployApp $aliEcs1
deployApp(){
shutdownApp $1
backupApp $1
uploadFile $1
startApp $1
}
echo "********************************************** Package app application **********************************************"
packageApp
echo "********************************************** Deploy app to AliECS 1 **********************************************"
deployApp ${aliEcs1}
echo "********************************************** Deploy app to AliECS 2 **********************************************"
deployApp ${aliEcs2}
启停脚本
startup.sh
#!/bin/sh
app_version="0.1.0"
profile_active="prod"
#profile_active="test"
java_cmd="nohup java -jar -Duser.language=en -Dspring.profiles.active=${profile_active}"
echo "Start send service."
${java_cmd} send-service-${app_version}.jar > send-service.log &
sleep 2s
echo "Start receive service."
${java_cmd} receive-service-${app_version}.jar > receive-service.log &
sleep 3s
echo "Start mail service."
${java_cmd} mail-service-${app_version}.jar > mail-service.log &
shutdown.sh
#!/bin/sh
send_service_id=`ps -ef|grep send-service |grep -v grep | gawk '{print $2}'`
receive_service_id=`ps -ef|grep receive-service |grep -v grep | gawk '{print $2}'`
mail_service_id=`ps -ef|grep mail-service |grep -v grep | gawk '{print $2}'`
echo "Start to shutdown send service."
kill -9 ${send_service_id}
echo "Start to shutdown receive service."
kill -9 ${receive_service_id}
echo "Start to shutdown mail service."
kill -9 ${mail_service_id}
备份脚本
backup.sh
#!/bin/sh
appPath="/app/jar/"
appBackPath="/app/jar/backup/"
cd ${appPath}
jarFileList=`ls | grep jar | awk '{print $1}'`
for file in ${jarFileList}; do
fileDate=`date -r ${file} '+%Y-%m-%d' | awk '{print $1}'`
if [[ -f ${file} ]]; then
mv ${file} "$appBackPath$file.$fileDate"
fi
done
转移历史文件
#!/bin/bash
#待转出文件所在目录
spath='/ap/spath/'
#目标目录
dpath='/ap/dpath/'
#多少天前的文件
days=30
if [ ! -d "$dpath" ]; then
mkdir -p $dpath
fi
cd "$spath"
#查找30天之前的文件
fileList=`find $spath* -mtime +$days |awk '{print $1}'`
for file in $fileList
do
if [ -f $file ]; then
mv $file $dpath
fi
done
ref:
https://stackoverflow.com/questions/305035/how-to-use-ssh-to-run-a-shell-script-on-a-remote-machine
https://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
https://my.oschina.net/shiyusen/blog/743153