#!/bin/bash # 如果遇到windows格式污染请执行: # sed -i 's/\r$//' updatesource.sh set -euo pipefail REPO_DIR="/etc/yum.repos.d" BACKUP_DIR="${REPO_DIR}/backup_$(date +%F_%H-%M-%S)" REPO_FILE="${REPO_DIR}/CentOS-Base.repo" # ============================== # 选择源 # ============================== select_mirror() { echo "===============================" echo " 选择 CentOS 7 Vault 源" echo "===============================" echo "1. 阿里云(推荐)" echo "2. 清华 TUNA" echo "3. 华为云" echo "4. 网易" echo "5. 官方 vault" echo "===============================" read -rp "请输入编号: " choice case "$choice" in 1) BASEURL="http://mirrors.aliyun.com/centos-vault/7.9.2009" ;; 2) BASEURL="https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.9.2009" ;; 3) BASEURL="https://repo.huaweicloud.com/centos-vault/7.9.2009" ;; 4) BASEURL="http://mirrors.163.com/centos-vault/7.9.2009" ;; 5) BASEURL="https://vault.centos.org/7.9.2009" ;; *) echo "无效选择"; exit 1 ;; esac } # ============================== # 备份 # ============================== backup_repo() { echo "[+] 备份 repo → $BACKUP_DIR" mkdir -p "$BACKUP_DIR" cp -a ${REPO_DIR}/*.repo "$BACKUP_DIR"/ 2>/dev/null || true } # ============================== # 写入 repo(无 EOF 版本) # ============================== write_repo() { echo "[+] 写入新 repo" # 清理旧 repo mkdir -p "${REPO_DIR}/bak_old" mv ${REPO_DIR}/*.repo "${REPO_DIR}/bak_old/" 2>/dev/null || true # 写入新 repo { echo "[base]" echo "name=CentOS-7 - Base" echo "baseurl=${BASEURL}/os/x86_64/" echo "enabled=1" echo "gpgcheck=0" echo "" echo "[updates]" echo "name=CentOS-7 - Updates" echo "baseurl=${BASEURL}/updates/x86_64/" echo "enabled=1" echo "gpgcheck=0" echo "" echo "[extras]" echo "name=CentOS-7 - Extras" echo "baseurl=${BASEURL}/extras/x86_64/" echo "enabled=1" echo "gpgcheck=0" } > "$REPO_FILE" } # ============================== # fastestmirror 控制 # ============================== config_fastestmirror() { CONF_FILE="/etc/yum/pluginconf.d/fastestmirror.conf" if [[ ! -f "$CONF_FILE" ]]; then echo "[!] 未找到 fastestmirror 插件,跳过" return fi read -rp "是否关闭 fastestmirror?(y/n): " fm if [[ "$fm" == "y" ]]; then sed -i 's/^enabled=.*/enabled=0/' "$CONF_FILE" echo "[+] 已关闭 fastestmirror" else sed -i 's/^enabled=.*/enabled=1/' "$CONF_FILE" echo "[+] 已开启 fastestmirror" fi } # ============================== # 刷新缓存 # ============================== refresh_cache() { echo "[+] 清理缓存" yum clean all echo "[+] 构建缓存" yum makecache } # ============================== # 可选组件 # ============================== install_optional() { read -rp "是否安装 EPEL?(y/n): " epel if [[ "$epel" == "y" ]]; then yum install -y epel-release || true fi read -rp "是否启用 SCLo?(y/n): " sclo if [[ "$sclo" == "y" ]]; then yum install -y centos-release-scl || true fi } # ============================== # 回滚 # ============================== rollback() { echo "[!] 可用备份:" ls -d ${REPO_DIR}/backup_* 2>/dev/null || { echo "没有备份" exit 1 } read -rp "输入备份路径: " dir if [[ -d "$dir" ]]; then rm -f ${REPO_DIR}/*.repo cp -a "$dir"/*.repo "$REPO_DIR"/ echo "[✓] 回滚完成" else echo "[✗] 目录不存在" exit 1 fi } # ============================== # 主流程 # ============================== main() { echo "1. 更换源" echo "2. 回滚" read -rp "请选择: " action case "$action" in 1) select_mirror backup_repo write_repo config_fastestmirror refresh_cache install_optional echo "[✔] 完成" ;; 2) rollback ;; *) echo "无效选择" exit 1 ;; esac } main