首页 > 其他分享 >搭配Jenkins使用的服务器部署备份升级脚本

搭配Jenkins使用的服务器部署备份升级脚本

时间:2022-11-16 15:02:46浏览次数:56  
标签:fi 备份 backupPath echo item 服务器 file path Jenkins

简介

主要搭配Jenkins使用,Jenkins将编译好的二进制文件上传至服务器指定文件夹中,然后执行该脚本进行原有程序备份并替换为新版程序。

参数说明

  • backupList 需要备份的文件列表,使用的是shell中的数据,使用一个空格分割,不限制数量
  • backupPath 需要备份的路径
  • backupLimit 备份数量限制
  • upgrageFilePath 新版本程序路径,一般为jenkins上传上来的临时路径

使用

  • ./脚本名.sh B 备份
  • ./脚本名.sh U 升级
  • ./脚本名.sh BU 备份后升级
#!/bin/bash

# POWER BY [email protected]

# Backup item list: Do not use ",", Please use one white space to split.
backupList=("/data/web/dist" "/data/api/api.jar")
backupPath="/data/bak"
# Backup file quantity limit
backupLimit=5
upgradeFilePath="/data/bin"

#backup
bak() {
    if [ ! -d $backupPath ]; then
        echo "${backupPath} not existed"
        echo "execute mkdir -p ${backupPath}"
        mkdir -p ${backupPath}
    fi
    for item in "${backupList[@]}"; do
        if [ -e $item ]; then
            if test -d $item; then
                folderBak $item
            else
                fileBak $item
            fi
            removeOutLimitFile $item
        else
            echo "${item} not existed"
        fi
    done
}

# folder bakup
folderBak() {
    path=$1
    d=$(date "+%Y%m%d%H%M%S")
    folderName=${path##*/}
    tar czvf "${folderName}.tar.gz.${d}" $path
    mv "${folderName}.tar.gz.${d}" ${backupPath}
}

# file bakup
fileBak() {
    path=$1
    d=$(date "+%Y%m%d%H%M%S")
    fileName=${path##*/}
    cp $path "${backupPath}/${fileName}.${d}"
}

upgrade() {
    echo "Upgrage Process"
    for item in "${backupList[@]}"; do
        echo "Upgrage ${item}"
        if test $item = "/"; then
            echo "Illegal input!"
            echo "Illegal input!"
            echo "Illegal input!"
            echo "Illegal input!"
            return 0
        fi
        name=${item##*/}
        echo "${path}"
        if [ -e "${upgradeFilePath}/${name}" ]; then
            rm -rf $item
            path=$(dirname ${item})
            if [ ! -d ${path} ]; then
                echo "${path} not existed!"
                echo "Execute mkdir -p ${path}"
                mkdir -p ${path}
            fi
            cp -r "${upgradeFilePath}/${name}" $item
            echo "Upgrage ${item} succeed!"
        else
            echo "${upgradeFilePath}/${name} not existed!"
        fi
    done
}

removeOutLimitFile() {
    echo "Remove out limit file"
    path=$1
    name=${path##*/}
    i=0
    for file in $(ls -lt "/data/bak"); do
        if [[ $file =~ $name ]]; then
		i=$[$i+1]
		if [ $i -gt $backupLimit ];then
			echo "Remove out limit backup file ${file}"
			rm -f "${backupPath}/${file}"
		fi
	fi
    done
}

if [ $# == 0 ]; then
    echo "No argument"
    echo "B: backup, U: upgrage, BU: backup and upgrage"
else
    if [ $1 == "B" ]; then
        bak
    elif [ $1 == "U" ]; then
        upgrade
    elif [ $1 == "BU" ]; then
        bak
        upgrade
    else
        echo "Illegal input"
    fi
fi

标签:fi,备份,backupPath,echo,item,服务器,file,path,Jenkins
From: https://www.cnblogs.com/97jay/p/16895923.html

相关文章