简介

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

参数说明

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

使用

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

# POWER BY weijie_zhan@hotmail.com

# 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

原文地址:http://www.cnblogs.com/97jay/p/16895923.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性