1.jenkins部署脚本
# !/bin/sh #防止shell里使用nohup不能后台运行直接挂掉 BUILD_ID=DONTKILLME jarpath="/home/wowed/wowed-account" #切换到jar包路径 cd ${jarpath} #获取当前服务的进程是否存在,注意jenkins和centos写法不一样,防止jenkins被杀死 #要加上grep -v grep这个过滤操作 pid=`ps -ef|grep wowed-account-service|grep -v grep|awk '{print $2}'` #如果存在的话 if [[ -n "$pid" ]] then echo ' 当前服务已经启动,现在进行重启操作... ' kill -9 $pid killsuccess=$? sleep 3s if [[ ${killsuccess} == '0' ]] then echo "服务关闭成功" cd ${jarpath} nohup java -jar wowed-account-service-1.0.jar >/dev/null 2>&1 & else echo "服务关闭失败" fi else echo '服务进程不存在直接启动' cd ${jarpath} nohup java -jar wowed-account-service-1.0.jar >/dev/null 2>&1 & fi cp /dev/null nohup.out echo "部署完成..."
2.jenkins部署docker脚本
SERVER_NAME=springbootadmin #容器id CID=$(docker ps | grep "$SERVER_NAME" |grep -v grep| awk '{print $1}') #镜像id IID=$(docker images | grep "$SERVER_NAME" |grep -v grep| awk '{print $3}') # 构建docker镜像 if [ -n "$IID" ]; then echo "存在$SERVER_NAME镜像,IID=$IID" docker stop $SERVER_NAME # 停止运行中的容器 docker rm $SERVER_NAME ##删除原来的容器 docker rmi $IID ## 删除原来的镜像 else echo "不存在$SERVER_NAME镜像,开始构建镜像" fi cd /home/wowed/springbootadminnew docker build -t $SERVER_NAME . docker run -d --name $SERVER_NAME -p 10000:10000 $SERVER_NAME
3.jenkins部署jar 脚本
#!/bin/sh BUILD_ID=DONTKILLME source ~/.bash_profile export PATH="/usr/java/jdk1.8.0_131:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:." APP_NAME=wowed-account-service-1.0.jar APP_FOLDER=/home/wowed/wowed-account #Serivce方式启动的命令 SERVICE_NAME=nl_wowed_account APP="$APP_FOLDER/$APP_NAME" #初始化配置文件 APP_CONFIG=${APP_NAME%%.jar*}.conf APP_CONFIG_CONTENT='JAVA_HOME=/usr/java/jdk1.8.0_131\n JAVA_OPTS="-server -Xmx512M -Xms512M -Dfile.encoding=UTF-8"\n RUN_ARGS="--spring.profiles.active=dev"\n PID_FOLDER="/home/wowed/wowed-account/pids"\n LOG_FOLDER=/home/wowed/logs' if [[ -f "$APP_FOLDER/$APP_CONFIG" ]]; then echo "App config file already existed." else echo "App config file does not exist, I will create it..." echo -e $APP_CONFIG_CONTENT > $APP_FOLDER/$APP_CONFIG echo "Create app config file completion. [ $APP_FOLDER/$APP_CONFIG ]" fi echo "Stop and start $APP" #判断/etc/init.d/下是否已经存在软链接 count=`ps -ef|grep java|grep $APP_NAME|grep -v grep |wc -l` if [ $count != 0 ];then boot_id=`ps -ef|grep java|grep $APP_NAME|grep -v grep | awk '{print $2}'` echo "kill $boot_id" kill -9 $boot_id fi mkdir -p ${APP_FOLDER}/pids sudo ln -s $APP /etc/init.d/$SERVICE_NAME chmod +x $APP service nl_wowed_account start echo "service $SERVICE_NAME start." ==============千万要注意:这种方式启动不能勾选Exec in pty ? 这个选项================
4.jenkins单服务脚本
#!/bin/sh source ~/.bash_profile APP_NAME=wowed-account-service-1.0.jar APP_FOLDER=/home/wowed/wowed-account #Serivce方式启动的命令 SERVICE_NAME=nl_wowed_account APP="$APP_FOLDER/$APP_NAME" #初始化配置文件 APP_CONFIG=${APP_NAME%%.jar*}.conf APP_CONFIG_CONTENT='JAVA_HOME=/usr/java/jdk1.8.0_131\n JAVA_OPTS="-server -Xmx512M -Xms512M -Dfile.encoding=UTF-8"\n RUN_ARGS="--spring.profiles.active=dev"\n PID_FOLDER="/home/wowed/wowed-account/pids"\n LOG_FOLDER=/home/wowed/logs' if [[ -f "$APP_FOLDER/$APP_CONFIG" ]]; then echo "App config file already existed." else echo "App config file does not exist, I will create it..." echo -e $APP_CONFIG_CONTENT > $APP_FOLDER/$APP_CONFIG echo "Create app config file completion. [ $APP_FOLDER/$APP_CONFIG ]" fi echo "Stop and start $APP" #判断/etc/init.d/下是否已经存在软链接 count=`ps -ef|grep java|grep $APP_NAME|grep -v grep |wc -l` if [ $count != 0 ];then boot_id=`ps -ef|grep java|grep $APP_NAME|grep -v grep | awk '{print $2}'` echo "kill $boot_id" kill -9 $boot_id fi mkdir -p ${APP_FOLDER}/pids chmod +x $APP sudo ln -s $APP /etc/init.d/$SERVICE_NAME service $SERVICE_NAME start echo "service $SERVICE_NAME start."
5.mysql备份脚本
#mysql定时备份脚本,以小时为单位进行备份,只需要修改mysqlip以及在centos里面配置一个cron表达式即可 #=====================定时任务============================ crontab -e 就可以打开一个文件进行编辑 #每整点执行一次: 0 */1 * * * /home/wowed/mysqlbak.sh #=========================================================== #!/bin/bash mysqlip=192.168.25.111 cd ~ path="$(pwd)/mysqlbak/$(date +%Y%m%d%H)" echo ${path} if [ -e $path ] then echo "mysql备份目录存在,不创建目录" else echo "mysql备份目录不存在,创建目录" mkdir -p ${path} fi systemdatabase1=information_schema systemdatabase2=mysql systemdatabase3=nacos systemdatabase4=performance_schema systemdatabase5=sys databaseNames=`mysql -uroot -proot1234% -h${mysqlip} -P3306 -e "show databases"|awk 'NR>1{print $1}'` for databaseName in ${databaseNames} do if [ $databaseName = ${systemdatabase1} ] || [ $databaseName = ${systemdatabase2} ] || [ $databaseName = ${systemdatabase3} ] || [ $databaseName = ${systemdatabase4} ]|| [ $databaseName = ${systemdatabase5} ] then echo " ${databaseName} 不进行备份" else echo " ${databaseName} 开始备份" mysqldump -h${mysqlip} -uroot -proot1234% --databases ${databaseName} > "${path}/${databaseName}.sql" fi done
6.mysql恢复脚本
#mysql还原备份脚本,执行命令如下: sh mysqlrebak.sh 2021122411 第一个参数为数据库备份的时间,精确到小时,也就是还原某小时备份的数据库 #!/bin/bash mysqlip=192.168.25.111 mysqlbakdate=${1} if [ -z "$mysqlbakdate" ] then echo "未输入还原目录!!!" exit; fi path="/home/wowed/mysqlbak/${mysqlbakdate}" if [ -d ${path} ] then echo "备份文件夹存在" else echo "备份文件夹不存在,退出程序" exit -1 fi #获取路径下的所有文件 allsqlfile=$(ls $path) #遍历文件然后还原数据库 for filename in $allsqlfile do database=${filename%%.*} echo "开始还原数据库${database}" echo "${path}/${filename}" mysql -u root -proot1234% -h${mysqlip} <<EOF source ${path}/${filename}; EOF done exit;
7.启动脚本
# !/bin/sh #防止shell里使用nohup不能后台运行直接挂掉 BUILD_ID=DONTKILLME jarpath="/home/wowed/wowed-account" #切换到jar包路径 cd ${jarpath} #获取当前服务的进程是否存在 pid=`ps -ef|grep wowed-account-service|grep jar|awk '{print $2}'` #如果存在的话 if [[ -n "$pid" ]] then echo ' 当前服务已经启动,现在进行重启操作... ' kill -9 $pid killsuccess=$? if [[ ${killsuccess} == '0' ]] then echo "服务关闭成功" cd ${jarpath} nohup java -jar wowed-account-service-1.0-SNAPSHOT.jar >/dev/null 2>&1 & else echo "服务关闭失败" fi else echo '服务进程不存在直接启动' cd ${jarpath} nohup java -jar wowed-account-service-1.0-SNAPSHOT.jar >/dev/null 2>&1 & fi cp /dev/null nohup.out echo "部署完成..." tail -200f /home/wowed/logs/wowed-account.log 8.停止脚本 #!/bin/bash A=`pgrep -f wowed-account | wc -l` if [ $A -gt 0 ];then B=`pgrep -f wowed-account` kill -9 $B fi