This commit is contained in:
SuperManito
2024-01-18 18:03:04 +08:00
parent d93d7ee3b8
commit d959129839

View File

@@ -708,108 +708,108 @@ function CloseFirewall() {
fi fi
} }
## 备份原有软件源 ## 备份原有软件源(文件/目录)
function BackupOriginMirrors() { function BackupOriginalMirrors() {
function BackupFile() { function BackupFile() {
local target_file=$1 local target_file=$1
local backup_file=$2 local backup_file=$2
local type="$3" local type="$3"
## 判断是否存在源文件
if [ -s "$target_file" ]; then [ -f "${target_file}" ] || touch "${target_file}"
if [ -s "$backup_file" ]; then if [ ! -s "${target_file}" ]; then
if [[ "${IGNORE_BACKUP_TIPS}" == "false" ]]; then
local CHOICE_BACKUP1
CHOICE_BACKUP1=$(echo -e "\n${BOLD}└─ 检测到系统中存在已备份的${type}源文件,是否跳过覆盖备份? [Y/n] ${PLAIN}")
read -rp "${CHOICE_BACKUP1}" INPUT
[[ -z "${INPUT}" ]] && INPUT=Y
case "${INPUT}" in
[Yy] | [Yy][Ee][Ss]) ;;
[Nn] | [Nn][Oo])
echo ''
cp -rvf "$target_file" "$backup_file" 2>&1
BACKUPED="true"
;;
*)
echo -e "\n$WARN 输入错误,默认不覆盖!"
;;
esac
fi
else
echo ''
cp -rvf "$target_file" "$backup_file" 2>&1
BACKUPED="true"
echo -e "\n$COMPLETE 已备份原有${type}源文件"
sleep 1s
fi
else
[ -f "$target_file" ] || touch "$target_file"
echo -e '' echo -e ''
return
fi
## 判断是否存在已备份的源文件
if [ -s "${backup_file}" ]; then
if [[ "${IGNORE_BACKUP_TIPS}" != "false" ]]; then
return
fi
local CHOICE_BACKUP
CHOICE_BACKUP=$(echo -e "\n${BOLD}└─ 检测到系统中存在已备份的 ${type} 源文件,是否跳过覆盖备份? [Y/n] ${PLAIN}")
read -rp "${CHOICE_BACKUP}" INPUT
[[ -z "${INPUT}" ]] && INPUT=Y
case "${INPUT}" in
[Yy] | [Yy][Ee][Ss]) ;;
[Nn] | [Nn][Oo])
echo ''
cp -rvf "${target_file}" "${backup_file}" 2>&1
BACKED_UP="true"
;;
*)
echo -e "\n$WARN 输入错误,默认不覆盖!"
;;
esac
else
echo ''
cp -rvf "${target_file}" "${backup_file}" 2>&1
BACKED_UP="true"
echo -e "\n$COMPLETE 已备份原有 ${type} 源文件"
sleep 1s
fi fi
} }
function BackupRepo() { function BackupDir() {
local target_dir=$1 local target_dir=$1
local backup_dir=$2 local backup_dir=$2
local VERIFICATION_FILES=1 [ -d "${target_dir}" ] || mkdir -p "${target_dir}"
local VERIFICATION_BACKUPFILES=1 [ -d "${backup_dir}" ] || mkdir -p "${backup_dir}"
## 判断是否存在 repo 源文件
[ -d "$target_dir" ] && ls "$target_dir" | grep '\.repo$' -q ls "${target_dir}" | grep '\.repo$' -q
VERIFICATION_FILES=$? if [ $? -ne 0 ]; then
[ -d "$backup_dir" ] && ls "$backup_dir" | grep '\.repo$' -q return
VERIFICATION_BACKUPFILES=$? fi
if [ ${VERIFICATION_FILES} -eq 0 ]; then ## 判断是否存在已备份的 repo 源文件
if [ -d "$backup_dir" ] && [ ${VERIFICATION_BACKUPFILES} -eq 0 ]; then ls "${backup_dir}" | grep '\.repo$' -q
if [[ "${IGNORE_BACKUP_TIPS}" == "false" ]]; then if [ $? -eq 0 ]; then
local CHOICE_BACKUP3 if [[ "${IGNORE_BACKUP_TIPS}" != "false" ]]; then
CHOICE_BACKUP3=$(echo -e "\n${BOLD}└─ 检测到系统中存在已备份的 repo 源文件,是否跳过覆盖备份? [Y/n] ${PLAIN}") return
read -rp "${CHOICE_BACKUP3}" INPUT
[[ -z "${INPUT}" ]] && INPUT=Y
case "${INPUT}" in
[Yy] | [Yy][Ee][Ss]) ;;
[Nn] | [Nn][Oo])
echo ''
cp -rvf $target_dir/* "$backup_dir" 2>&1
BACKUPED="true"
;;
*)
echo -e "\n$WARN 输入错误,默认不覆盖!"
;;
esac
fi
else
[ ! -d "$backup_dir" ] && mkdir -p "$backup_dir"
echo ''
cp -rvf $target_dir}/* "$backup_dir" 2>&1
BACKUPED="true"
echo -e "\n$COMPLETE 已备份原有 repo 源文件"
sleep 1s
fi fi
local CHOICE_BACKUP
CHOICE_BACKUP=$(echo -e "\n${BOLD}└─ 检测到系统中存在已备份的 repo 源文件,是否跳过覆盖备份? [Y/n] ${PLAIN}")
read -rp "${CHOICE_BACKUP}" INPUT
[[ -z "${INPUT}" ]] && INPUT=Y
case "${INPUT}" in
[Yy] | [Yy][Ee][Ss]) ;;
[Nn] | [Nn][Oo])
echo ''
cp -rvf $target_dir/* "${backup_dir}" 2>&1
BACKED_UP="true"
;;
*)
echo -e "\n$WARN 输入错误,默认不覆盖!"
;;
esac
else else
[ -d "$target_dir" ] || mkdir -p "$target_dir" echo ''
cp -rvf $target_dir/* "${backup_dir}" 2>&1
BACKED_UP="true"
echo -e "\n$COMPLETE 已备份原有 repo 源文件"
sleep 1s
fi fi
} }
BACKUPED="false" BACKED_UP="false" # 是否已备份
if [[ "${BACKUP}" == "true" ]]; then if [[ "${BACKUP}" == "true" ]]; then
case "${SYSTEM_FACTIONS}" in case "${SYSTEM_FACTIONS}" in
"${SYSTEM_DEBIAN}") "${SYSTEM_DEBIAN}")
# /etc/apt/sources.list # /etc/apt/sources.list
BackupFile $File_DebianSourceList $File_DebianSourceListBackup " list " BackupFile $File_DebianSourceList $File_DebianSourceListBackup "list"
;; ;;
"${SYSTEM_REDHAT}" | "${SYSTEM_OPENCLOUDOS}" | "${SYSTEM_OPENEULER}") "${SYSTEM_REDHAT}" | "${SYSTEM_OPENCLOUDOS}" | "${SYSTEM_OPENEULER}")
# /etc/yum.repos.d # /etc/yum.repos.d
BackupRepo $Dir_YumRepos $Dir_YumReposBackup BackupDir $Dir_YumRepos $Dir_YumReposBackup
;; ;;
"${SYSTEM_OPENSUSE}") "${SYSTEM_OPENSUSE}")
# /etc/zypp/repos.d # /etc/zypp/repos.d
BackupRepo $Dir_openSUSERepos $Dir_openSUSEReposBackup BackupDir $Dir_openSUSERepos $Dir_openSUSEReposBackup
;; ;;
"${SYSTEM_ARCH}") "${SYSTEM_ARCH}")
# /etc/pacman.d/mirrorlist # /etc/pacman.d/mirrorlist
BackupFile $File_ArchMirrorList $File_ArchMirrorListBackup " mirrorlist " BackupFile $File_ArchMirrorList $File_ArchMirrorListBackup "mirrorlist"
;; ;;
"${SYSTEM_ALPINE}") "${SYSTEM_ALPINE}")
# /etc/apk/repositories # /etc/apk/repositories
BackupFile $File_AlpineRepositories $File_AlpineRepositoriesBackup " repositories " BackupFile $File_AlpineRepositories $File_AlpineRepositoriesBackup "repositories"
;; ;;
esac esac
fi fi
@@ -837,7 +837,7 @@ function RemoveOriginMirrors() {
;; ;;
*) *)
if [ -f $Dir_YumRepos/epel.repo ]; then if [ -f $Dir_YumRepos/epel.repo ]; then
ls $Dir_YumRepos/ | grep -Ev epel | xargs -0 rm -rf ls $Dir_YumRepos/ | grep -Ev epel | xargs rm -rf
else else
rm -rf $Dir_YumRepos/* rm -rf $Dir_YumRepos/*
fi fi
@@ -846,7 +846,7 @@ function RemoveOriginMirrors() {
;; ;;
"${SYSTEM_CENTOS}") "${SYSTEM_CENTOS}")
if [ -f $Dir_YumRepos/epel.repo ]; then if [ -f $Dir_YumRepos/epel.repo ]; then
ls $Dir_YumRepos/ | grep -Ev epel | xargs -0 rm -rf ls $Dir_YumRepos/ | grep -Ev epel | xargs rm -rf
else else
rm -rf $Dir_YumRepos/* rm -rf $Dir_YumRepos/*
fi fi
@@ -887,7 +887,7 @@ function RemoveOriginMirrors() {
[ -d $Dir_YumRepos ] && rm -rf $Dir_YumRepos/openEuler.repo [ -d $Dir_YumRepos ] && rm -rf $Dir_YumRepos/openEuler.repo
;; ;;
"${SYSTEM_OPENSUSE}") "${SYSTEM_OPENSUSE}")
[ -d $Dir_openSUSERepos ] && rm -rf $Dir_openSUSERepos/repo-* [ -d $Dir_openSUSERepos ] && ls $Dir_openSUSERepos/ | grep -E "^repo-" | grep -Ev "openh264" | xargs rm -rf
;; ;;
"${SYSTEM_ARCH}") "${SYSTEM_ARCH}")
[ -f $File_ArchMirrorList ] && sed -i '1,$d' $File_ArchMirrorList [ -f $File_ArchMirrorList ] && sed -i '1,$d' $File_ArchMirrorList
@@ -902,48 +902,42 @@ function RemoveOriginMirrors() {
function ChangeMirrors() { function ChangeMirrors() {
## 打印修改前后差异 ## 打印修改前后差异
function PrintDiff() { function PrintDiff() {
## Debian/Arch 比较模式 ## 单一文件比较模式
function DiffMode1() { function DiffFile() {
local backup_file=$1 local diff_file=$1
local origin_file=$2 local origin_file=$2
if [[ -s $backup_file && -s $origin_file ]]; then if [[ -s $diff_file ]] && [[ -s $origin_file ]]; then
if [[ "$(cat "$backup_file")" != "$(cat "$origin_file")" ]]; then if [[ "$(cat "${diff_file}")" != "$(cat "${origin_file}")" ]]; then
echo -e "\n${BLUE}${backup_file}${PLAIN} -> ${BLUE}${origin_file}${PLAIN}" echo -e "\n${BLUE}${diff_file}${PLAIN} -> ${BLUE}${origin_file}${PLAIN}"
diff "$backup_file" "$origin_file" -d --color=always -I -B -E diff "${diff_file}" "${origin_file}" -d --color=always -I -B -E
fi fi
fi fi
} }
## RedHat/openEuler/openSUSE 比较模式 ## 目录文件比较模式
function DiffMode2() { function DiffDir() {
local backup_dir=$1 local diff_dir=$1
local origin_dir=$2 local origin_dir=$2
local backup_file origin_file for item in $(ls $diff_dir | xargs); do
for item in $(ls $backup_dir | xargs); do DiffFile "${diff_dir}/${item}" "${origin_dir}/${item}"
backup_file="$backup_dir/$item"
origin_file="$origin_dir/$item"
if [[ "$(cat $backup_file)" != "$(cat $origin_file)" ]]; then
echo -e "\n${BLUE}${backup_file}${PLAIN} -> ${BLUE}${origin_file}${PLAIN}"
diff "$backup_file" "$origin_file" -d --color=always -I -B -E
fi
done done
} }
if [[ -x /usr/bin/diff && "${BACKUPED}" == "true" ]]; then if [[ -x /usr/bin/diff && "${BACKED_UP}" == "true" ]]; then
case "${SYSTEM_FACTIONS}" in case "${SYSTEM_FACTIONS}" in
"${SYSTEM_DEBIAN}") "${SYSTEM_DEBIAN}")
DiffMode1 $File_DebianSourceListBackup $File_DebianSourceList DiffFile $File_DebianSourceListBackup $File_DebianSourceList
;; ;;
"${SYSTEM_REDHAT}" | "${SYSTEM_OPENCLOUDOS}" | "${SYSTEM_OPENEULER}") "${SYSTEM_REDHAT}" | "${SYSTEM_OPENCLOUDOS}" | "${SYSTEM_OPENEULER}")
DiffMode2 $Dir_YumReposBackup $Dir_YumRepos DiffDir $Dir_YumReposBackup $Dir_YumRepos
;; ;;
"${SYSTEM_OPENSUSE}") "${SYSTEM_OPENSUSE}")
DiffMode2 $Dir_openSUSEReposBackup $Dir_openSUSERepos DiffDir $Dir_openSUSEReposBackup $Dir_openSUSERepos
;; ;;
"${SYSTEM_ARCH}") "${SYSTEM_ARCH}")
DiffMode1 $File_ArchMirrorListBackup $File_ArchMirrorList DiffFile $File_ArchMirrorListBackup $File_ArchMirrorList
;; ;;
"${SYSTEM_ALPINE}") "${SYSTEM_ALPINE}")
DiffMode1 $File_AlpineRepositoriesBackup $File_AlpineRepositories DiffFile $File_AlpineRepositoriesBackup $File_AlpineRepositories
;; ;;
esac esac
fi fi
@@ -1013,7 +1007,7 @@ function UpdateSoftware() {
function CleanCache() { function CleanCache() {
## 跳过特殊系统 ## 跳过特殊系统
case "${SYSTEM_JUDGMENT}" in case "${SYSTEM_JUDGMENT}" in
"${SYSTEM_ARCH}" | "${SYSTEM_RHEL}" | "${SYSTEM_ALPINE}") "${SYSTEM_RHEL}" | "${SYSTEM_OPENSUSE}" | "${SYSTEM_ARCH}" | "${SYSTEM_ALPINE}")
return return
;; ;;
esac esac
@@ -1047,9 +1041,6 @@ function UpdateSoftware() {
yum autoremove -y >/dev/null 2>&1 yum autoremove -y >/dev/null 2>&1
yum clean packages -y >/dev/null 2>&1 yum clean packages -y >/dev/null 2>&1
;; ;;
"${SYSTEM_OPENSUSE}")
zypper clean >/dev/null 2>&1
;;
esac esac
echo -e "\n$COMPLETE 清理完毕" echo -e "\n$COMPLETE 清理完毕"
} }
@@ -5222,7 +5213,7 @@ function Combin_Function() {
ChooseWebProtocol ChooseWebProtocol
ChooseInstallEPEL ChooseInstallEPEL
CloseFirewall CloseFirewall
BackupOriginMirrors BackupOriginalMirrors
RemoveOriginMirrors RemoveOriginMirrors
ChangeMirrors ChangeMirrors
UpdateSoftware UpdateSoftware