X-OpenWrt/Scripts/AutoUpdate.sh

197 lines
6.5 KiB
Bash
Raw Normal View History

2020-09-19 07:37:55 +00:00
#!/bin/bash
# https://github.com/Hyy2001X/AutoBuild-Actions
# AutoBuild Module by Hyy2001
# AutoUpdate for Openwrt
2020-09-19 07:37:55 +00:00
Version=V4.7
2020-09-27 03:40:52 +00:00
DEFAULT_DEVICE=d-team_newifi-d2
2020-09-19 07:37:55 +00:00
Github=https://github.com/Hyy2001X/AutoBuild-Actions
2020-09-23 09:43:46 +00:00
2020-09-28 14:28:40 +00:00
TIME() {
2020-10-05 11:01:23 +00:00
echo -ne "\n[$(date "+%H:%M:%S")] "
2020-09-25 01:51:26 +00:00
}
2020-11-17 06:14:17 +00:00
Github_Download=${Github}/releases/download/AutoUpdate
2020-10-05 11:01:23 +00:00
Author=${Github##*com/}
2020-11-17 06:14:17 +00:00
Github_Tags=https://api.github.com/repos/${Author}/releases/latest
2020-09-19 07:37:55 +00:00
cd /etc
2020-11-17 06:14:17 +00:00
CURRENT_VERSION=$(awk 'NR==1' openwrt_info)
CURRENT_DEVICE=$(jsonfilter -e '@.model.id' < "/etc/board.json" | tr ',' '_')
2020-11-17 06:14:17 +00:00
clear && echo "Openwrt-AutoUpdate Script ${Version}"
2020-12-23 14:28:57 +00:00
Input_Option="$1"
if [[ -z "${Input_Option}" ]];then
2020-12-21 14:16:18 +00:00
Upgrade_Options="-q" && TIME && echo "执行: 保留配置更新固件[静默模式]"
2020-09-28 14:28:40 +00:00
else
2020-12-23 14:28:57 +00:00
case ${Input_Option} in
2020-12-21 14:16:18 +00:00
-n)
TIME && echo "执行: 更新固件(不保留配置)"
2020-12-21 14:16:18 +00:00
;;
-q)
TIME && echo "执行: 保留配置更新固件[静默模式]"
;;
-v)
TIME && echo "执行: 保留配置更新固件[详细日志]"
2020-12-21 14:16:18 +00:00
;;
-f)
2020-11-17 06:14:17 +00:00
Force_Update="1"
2020-12-21 14:16:18 +00:00
Upgrade_Options="-q"
TIME && echo "执行: 强制更新固件(保留配置)"
2020-12-21 14:16:18 +00:00
;;
2020-12-23 14:28:57 +00:00
-u)
AutoUpdate_Mode="1"
Upgrade_Options="-q"
;;
-s)
Stable_Mode="1"
Upgrade_Options="-q"
TIME && echo "执行: 更新固件到稳定版本(保留配置)"
;;
2020-12-21 14:16:18 +00:00
*)
echo -e "\nUsage: bash /bin/AutoUpdate.sh [<Option>]"
echo -e "\n可使用的选项:"
echo " -f 强制更新固件,自动下载以及安装依赖包,跳过版本号验证(保留配置)"
2020-12-21 14:16:18 +00:00
echo " -q 更新固件并保留配置[静默模式]"
echo " -v 更新固件并保留配置[详细日志]"
echo " -n 更新固件(不保留配置)"
echo " -u 适用于定时更新的参数,自动下载以及安装依赖包(保留配置)"
echo " -s 更新固件到最新的稳定版本(保留配置)"
2020-12-21 14:16:18 +00:00
echo -e "\n项目地址: ${Github}"
echo -e "默认设备: ${DEFAULT_DEVICE}\n"
exit
;;
esac
if [[ ! "${Force_Update}" == "1" ]] && [[ ! "${AutoUpdate_Mode}" == "1" ]] && [[ ! "${Stable_Mode}" == "1" ]];then
2020-12-23 14:28:57 +00:00
Upgrade_Options="${Input_Option}"
fi
2020-09-29 06:17:03 +00:00
fi
2020-11-17 06:14:17 +00:00
opkg list | awk '{print $1}' > /tmp/Package_list
2020-12-23 14:28:57 +00:00
if [[ ! "${Force_Update}" == "1" ]] && [[ ! "${AutoUpdate_Mode}" == "1" ]];then
2020-12-21 14:16:18 +00:00
grep "curl" /tmp/Package_list > /dev/null 2>&1
if [[ ! $? -ne 0 ]];then
Google_Check=$(curl -I -s --connect-timeout 5 www.google.com -w %{http_code} | tail -n1)
[ ! "$Google_Check" == 200 ] && TIME && echo "Google 连接失败,可能导致固件下载速度缓慢!"
fi
2020-10-02 18:06:37 +00:00
fi
grep "wget" /tmp/Package_list > /dev/null 2>&1
2020-11-17 06:14:17 +00:00
if [[ $? -ne 0 ]];then
2020-12-23 14:28:57 +00:00
if [[ "${Force_Update}" == "1" ]] || [[ "${AutoUpdate_Mode}" == "1" ]];then
2020-12-05 17:45:06 +00:00
Choose="Y"
else
TIME && read -p "未安装[wget],是否执行安装?[Y/n]:" Choose
fi
if [[ "${Choose}" == Y ]] || [[ "${Choose}" == y ]];then
TIME && echo -e "开始安装[wget],请耐心等待...\n"
opkg update > /dev/null 2>&1
opkg install wget
else
TIME && echo "用户已取消安装,即将退出更新脚本..."
sleep 2
exit
fi
fi
2020-11-17 06:14:17 +00:00
if [[ -z "${CURRENT_VERSION}" ]];then
2020-12-03 11:25:21 +00:00
TIME && echo "警告: 当前固件版本获取失败!"
2020-11-17 06:14:17 +00:00
CURRENT_VERSION="未知"
2020-09-19 07:37:55 +00:00
fi
2020-11-17 06:14:17 +00:00
if [[ -z "${CURRENT_DEVICE}" ]];then
[[ "${Force_Update}" == "1" ]] && exit
2020-12-03 11:25:21 +00:00
TIME && echo "警告: 当前设备名称获取失败,使用预设名称[$DEFAULT_DEVICE]"
2020-11-17 06:14:17 +00:00
CURRENT_DEVICE="${DEFAULT_DEVICE}"
2020-09-26 17:14:39 +00:00
fi
2020-12-03 11:25:21 +00:00
TIME && echo "正在检查版本更新..."
[ ! -f /tmp/Github_Tags ] && touch /tmp/Github_Tags
2020-11-17 06:14:17 +00:00
wget -q ${Github_Tags} -O - > /tmp/Github_Tags
if [[ ${Stable_Mode} == 1 ]];then
GET_FullVersion=$(cat /tmp/Github_Tags | egrep -o "AutoBuild-${CURRENT_DEVICE}-R[0-9]+.[0-9]+.[0-9]+.[0-9]+-Stable" | awk 'END {print}')
else
GET_FullVersion=$(cat /tmp/Github_Tags | egrep -o "AutoBuild-${CURRENT_DEVICE}-R[0-9]+.[0-9]+.[0-9]+.[0-9]+" | awk 'END {print}')
fi
GET_Version="${GET_FullVersion#*${CURRENT_DEVICE}-}"
2020-11-17 06:14:17 +00:00
if [[ -z "${GET_FullVersion}" ]] || [[ -z "${GET_Version}" ]];then
2020-10-05 11:01:23 +00:00
TIME && echo "检查更新失败,请稍后重试!"
2020-09-19 07:37:55 +00:00
exit
fi
2020-10-05 11:01:23 +00:00
echo -e "\n固件作者: ${Author%/*}"
2020-11-17 06:14:17 +00:00
echo "设备名称: ${DEFAULT_DEVICE}"
2020-12-03 11:25:21 +00:00
echo -e "\n当前固件版本: ${CURRENT_VERSION}"
2020-12-23 14:28:57 +00:00
echo "云端固件版本: ${GET_Version}"
2020-11-17 06:14:17 +00:00
if [[ ! ${Force_Update} == 1 ]];then
2020-12-05 17:45:06 +00:00
if [[ "${CURRENT_VERSION}" == "${GET_Version}" ]];then
2020-12-23 14:28:57 +00:00
[[ "${AutoUpdate_Mode}" == "1" ]] && exit
TIME && read -p "已是最新版本,是否强制更新固件?[Y/n]:" Choose
2020-12-05 17:45:06 +00:00
if [[ "${Choose}" == Y ]] || [[ "${Choose}" == y ]];then
2020-12-23 14:28:57 +00:00
TIME && echo "开始强制更新固件..."
2020-10-02 18:04:28 +00:00
else
TIME && echo "已取消强制更新,即将退出更新程序..."
2020-10-02 18:04:28 +00:00
sleep 2
exit
fi
2020-09-28 14:28:40 +00:00
fi
2020-10-02 18:07:56 +00:00
fi
2020-11-17 06:14:17 +00:00
Firmware_Info="${GET_FullVersion}"
2020-09-26 16:57:50 +00:00
Firmware="${Firmware_Info}.bin"
2020-09-25 01:51:26 +00:00
Firmware_Detail="${Firmware_Info}.detail"
2020-12-23 14:28:57 +00:00
echo -e "\n云端固件名称: ${Firmware}"
echo "固件下载地址: ${Github_Download}"
Disk_List="/tmp/disk_list"
[ -f $Disk_List ] && rm -f $Disk_List
Check_Disk="$(mount | egrep -o "mnt/+sd[a-zA-Z][0-9]+")"
if [ ! -z "${Check_Disk}" ];then
echo "${Check_Disk}" > ${Disk_List}
Disk_Number=$(sed -n '$=' ${Disk_List})
if [ ${Disk_Number} -gt 1 ];then
for Disk_Name in $(cat ${Disk_List})
do
Disk_Available="$(df -m | grep "${Disk_Name}" | awk '{print $4}')"
if [ "${Disk_Available}" -gt 20 ];then
Download_Path="/${Disk_Name}"
break
else
Download_Path="/tmp"
fi
done
else
Disk_Name="${Check_Disk}"
Disk_Available="$(df -m | grep "${Disk_Name}" | awk '{print $4}')"
if [ "${Disk_Available}" -gt 20 ];then
Download_Path="/${Disk_Name}"
else
Download_Path="/tmp"
fi
fi
else
Download_Path="/tmp"
fi
[ ! -d "${Download_Path}/Downloads" ] && mkdir -p ${Download_Path}/Downloads
cd ${Download_Path}/Downloads
echo "固件保存位置: ${Download_Path}/Downloads"
2020-09-25 01:51:26 +00:00
TIME && echo "正在下载固件,请耐心等待..."
2020-11-17 06:14:17 +00:00
wget -q ${Github_Download}/${Firmware} -O ${Firmware}
if [[ ! $? == 0 ]];then
TIME && echo "固件下载失败,请检查网络后重试!"
2020-09-19 07:37:55 +00:00
exit
fi
TIME && echo "固件下载成功!"
TIME && echo "正在获取云端固件MD5,请耐心等待..."
2020-11-17 06:14:17 +00:00
wget -q ${Github_Download}/${Firmware_Detail} -O ${Firmware_Detail}
if [[ ! $? == 0 ]];then
TIME && echo "MD5 获取失败,请检查网络后重试!"
2020-09-19 07:37:55 +00:00
exit
fi
GET_MD5=$(awk -F '[ :]' '/MD5/ {print $2;exit}' ${Firmware_Detail})
2020-11-17 06:14:17 +00:00
CURRENT_MD5=$(md5sum ${Firmware} | cut -d ' ' -f1)
echo -e "\n本地固件MD5:${CURRENT_MD5}"
2020-11-17 06:14:17 +00:00
echo "云端固件MD5:${GET_MD5}"
if [[ -z "${GET_MD5}" ]] || [[ -z "${CURRENT_MD5}" ]];then
echo -e "\nMD5 获取失败!"
2020-09-19 07:37:55 +00:00
exit
fi
2020-11-17 06:14:17 +00:00
if [[ ! "${GET_MD5}" == "${CURRENT_MD5}" ]];then
echo -e "\nMD5 对比失败,请检查网络后重试!"
2020-09-19 07:37:55 +00:00
exit
else
TIME && echo -e "MD5 对比通过!"
2020-09-19 07:37:55 +00:00
fi
TIME && echo -e "开始更新固件,请耐心等待路由器重启...\n"
2020-10-05 11:01:23 +00:00
sleep 3
sysupgrade ${Upgrade_Options} ${Firmware}