centos7上面使用python3执行DrissionPage的shell脚本备份

#!/bin/bash

# 设置重试参数
MAX_RETRIES=3
RETRY_DELAY=5  # 秒

# 确保以root用户运行
if [ "$(id -u)" -ne 0 ]; then
    echo "请使用root权限运行此脚本" >&2
    exit 1
fi
# 添加到系统环境配置(所有用户生效)
cat >> /etc/profile << EOF
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
EOF

# 使配置立即生效
source /etc/profile
# 重试函数
retry() {
    local cmd="$1"
    local description="$2"
    local retries=0

    echo "执行: $description"

    while [ $retries -lt $MAX_RETRIES ]; do
        echo "尝试 $((retries+1))/$MAX_RETRIES: $description"

        # 执行命令并捕获输出和状态
        output=$($cmd 2>&1)
        status=$?

        if [ $status -eq 0 ]; then
            echo "✓ $description 成功"
            return 0
        fi

        echo "✗ $description 失败: $output"
        retries=$((retries+1))

        if [ $retries -lt $MAX_RETRIES ]; then
            echo "等待 $RETRY_DELAY 秒后重试..."
            sleep $RETRY_DELAY
        fi
    done

    echo "⚠️ $description 已达到最大重试次数,操作失败"
    return 1
}

echo  "启动PPPoE连接"
pppoe-start
sleep 5
pppoe-status
echo "当前的IP是 $(curl -s ip.sb)"
# 备份YUM源配置
echo "正在备份YUM源配置..."
mkdir -p /etc/yum.repos.d/backup || { echo "创建备份目录失败"; exit 1; }
cp /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/ 2>/dev/null

# 配置阿里云YUM源
retry "curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo" "下载CentOS基础源" || { echo "下载CentOS基础源失败,脚本退出"; exit 1; }
retry "curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo" "下载EPEL源" || { echo "下载EPEL源失败,脚本退出"; exit 1; }

# 清理并重建YUM缓存
retry "yum clean all" "清理YUM缓存" || { echo "清理YUM缓存失败,脚本退出"; exit 1; }
retry "yum makecache" "重建YUM缓存" || { echo "重建YUM缓存失败,脚本退出"; exit 1; }

# 配置pip使用阿里云镜像
echo "正在配置pip使用阿里云镜像..."
mkdir -p ~/.pip || { echo "创建pip配置目录失败"; exit 1; }
cat > ~/.pip/pip.conf << 'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
timeout = 60

[install]
trusted-host = mirrors.aliyun.com
EOF

# 安装系统依赖
retry "yum install -y wget gcc jnettop python3-devel" "安装系统依赖" || { echo "安装系统依赖失败,脚本退出"; exit 1; }

# 安装Python包
retry "pip3 install DrissionPage==4.1.0.9 loguru" "安装Python包" || { echo "安装Python包失败,脚本退出"; exit 1; }

# 安装Chromium浏览器
retry "yum install -y chromium" "安装Chromium" || { echo "安装Chromium失败,脚本退出"; exit 1; }

# 验证Chromium安装
echo "验证Chromium安装..."
chromium_path=$(which chromium-browser)
if [ -z "$chromium_path" ]; then
    echo "Chromium安装失败,未找到chromium-browser命令" >&2
    exit 1
else
    echo "Chromium安装成功,路径:$chromium_path"
fi

echo "所有操作已成功完成!"
posted @ 2025-06-17 17:25  公众号python学习开发  阅读(21)  评论(0)    收藏  举报