Skip to content

Debian 到 AlmaLinux 10 跨发行版迁移指南

本指南详细介绍如何从 Debian 10/11/12 迁移到 AlmaLinux 10,包括系统架构差异处理、包管理系统转换和服务配置迁移等跨发行版迁移挑战。

迁移概述

系统架构差异分析

yaml
基础差异:
  包管理: APT (.deb) → DNF (.rpm)
  初始化: systemd (相同) → systemd (兼容)
  文件系统: ext4/btrfs → xfs/ext4 (推荐 XFS)
  防火墙: ufw/iptables → firewalld
  网络: NetworkManager/ifupdown → NetworkManager

主要挑战:
  - 包名称和依赖关系不同
  - 配置文件路径差异
  - 服务管理方式变化
  - 权限和安全模型差异

迁移复杂度: 高 (跨发行版)
预计时间: 4-12小时/系统 (含数据迁移和验证)

迁移策略对比

迁移方式适用场景复杂度停机时间回滚难度
并行迁移生产环境中等短 (切换时间)容易
逐步替换大型环境分阶段容易
数据导出迁移小型环境困难
容器化迁移现代应用中等容易

第一阶段:Debian 系统分析

1.1 系统环境评估

Debian 版本和包信息收集

bash
# 检查 Debian 版本
cat /etc/debian_version
cat /etc/os-release
lsb_release -a

# 收集已安装包信息
dpkg --get-selections > /root/debian_packages.txt
apt list --installed > /root/apt_packages.txt

# 检查第三方仓库
cat /etc/apt/sources.list
ls /etc/apt/sources.list.d/

# 收集系统配置信息
systemctl list-unit-files --type=service --state=enabled > /root/enabled_services.txt
crontab -l > /root/root_crontab.txt 2>/dev/null || echo "No crontab"

# 检查用户和组
cat /etc/passwd > /root/debian_users.txt
cat /etc/group > /root/debian_groups.txt

应用程序和服务分析

bash
# 创建系统分析脚本
cat > /root/debian_analysis.sh << 'EOF'
#!/bin/bash

echo "=== Debian 系统迁移分析 ==="

ANALYSIS_DIR="/root/debian_migration_analysis"
mkdir -p $ANALYSIS_DIR

# 1. Web 服务器检查
if systemctl is-active --quiet apache2; then
    echo "Apache2 已安装并运行"
    apache2ctl -M > $ANALYSIS_DIR/apache_modules.txt
    cp -r /etc/apache2 $ANALYSIS_DIR/
fi

if systemctl is-active --quiet nginx; then
    echo "Nginx 已安装并运行"
    nginx -T > $ANALYSIS_DIR/nginx_config.txt 2>&1
    cp -r /etc/nginx $ANALYSIS_DIR/
fi

# 2. 数据库检查
if systemctl is-active --quiet mysql mariadb; then
    echo "MySQL/MariaDB 检测"
    mysql -e "SHOW DATABASES;" > $ANALYSIS_DIR/mysql_databases.txt 2>/dev/null || true
    mysqldump --all-databases --single-transaction > $ANALYSIS_DIR/mysql_backup.sql 2>/dev/null || true
fi

if systemctl is-active --quiet postgresql; then
    echo "PostgreSQL 检测"
    sudo -u postgres psql -l > $ANALYSIS_DIR/postgresql_databases.txt
    sudo -u postgres pg_dumpall > $ANALYSIS_DIR/postgresql_backup.sql
fi

# 3. PHP 应用检查
if command -v php &> /dev/null; then
    php -v > $ANALYSIS_DIR/php_version.txt
    php -m > $ANALYSIS_DIR/php_modules.txt
    find /etc/php* -name php.ini -exec cp {} $ANALYSIS_DIR/ \; 2>/dev/null
fi

# 4. Python 应用检查
if command -v python3 &> /dev/null; then
    python3 --version > $ANALYSIS_DIR/python_version.txt
    pip3 list > $ANALYSIS_DIR/python_packages.txt 2>/dev/null || true
fi

# 5. 网络配置检查
ip addr show > $ANALYSIS_DIR/network_interfaces.txt
cat /etc/network/interfaces > $ANALYSIS_DIR/interfaces_config.txt 2>/dev/null || true
systemctl status networking > $ANALYSIS_DIR/networking_status.txt

# 6. 防火墙配置检查
if command -v ufw &> /dev/null; then
    ufw status verbose > $ANALYSIS_DIR/ufw_status.txt
fi

iptables -L -n > $ANALYSIS_DIR/iptables_rules.txt

# 7. 存储和挂载点
df -h > $ANALYSIS_DIR/filesystem_usage.txt
mount > $ANALYSIS_DIR/mount_points.txt
cat /etc/fstab > $ANALYSIS_DIR/fstab.txt

echo "分析完成,结果保存在: $ANALYSIS_DIR"
EOF

chmod +x /root/debian_analysis.sh
/root/debian_analysis.sh

1.2 包映射和依赖分析

创建 Debian 到 AlmaLinux 包映射表

bash
# 创建包映射脚本
cat > /root/package_mapping.sh << 'EOF'
#!/bin/bash

# 常用 Debian 到 AlmaLinux 包映射
declare -A PACKAGE_MAP=(
    # Web 服务器
    ["apache2"]="httpd"
    ["apache2-utils"]="httpd-tools"
    ["libapache2-mod-php"]="php"
    ["nginx"]="nginx"
    
    # 数据库
    ["mysql-server"]="mysql-server"
    ["mariadb-server"]="mariadb-server"
    ["postgresql"]="postgresql-server"
    ["sqlite3"]="sqlite"
    
    # 开发工具
    ["build-essential"]="gcc gcc-c++ make"
    ["git"]="git"
    ["curl"]="curl"
    ["wget"]="wget"
    ["vim"]="vim"
    
    # 系统工具
    ["openssh-server"]="openssh-server"
    ["cron"]="cronie"
    ["rsyslog"]="rsyslog"
    ["logrotate"]="logrotate"
    
    # PHP 相关
    ["php"]="php"
    ["php-fpm"]="php-fpm"
    ["php-mysql"]="php-mysqlnd"
    ["php-curl"]="php-curl"
    ["php-json"]="php-json"
    
    # Python
    ["python3"]="python3"
    ["python3-pip"]="python3-pip"
    ["python3-dev"]="python3-devel"
    
    # 网络工具
    ["net-tools"]="net-tools"
    ["iproute2"]="iproute"
    ["iptables"]="iptables"
)

echo "=== Debian 到 AlmaLinux 包映射转换 ==="

# 读取 Debian 已安装包列表
while read -r package state; do
    if [[ "$state" == "install" ]]; then
        pkg_name=$(echo $package | cut -d: -f1)
        
        if [[ -n "${PACKAGE_MAP[$pkg_name]}" ]]; then
            echo "$pkg_name -> ${PACKAGE_MAP[$pkg_name]}"
        else
            echo "$pkg_name -> [需要手动查找]"
        fi
    fi
done < /root/debian_packages.txt > /root/package_mapping_result.txt

echo "包映射完成,结果保存在: /root/package_mapping_result.txt"
EOF

chmod +x /root/package_mapping.sh
/root/package_mapping.sh

第二阶段:数据备份和迁移准备

2.1 全面数据备份

创建跨发行版备份脚本

bash
cat > /root/debian_migration_backup.sh << 'EOF'
#!/bin/bash

BACKUP_DIR="/backup/debian_to_alma_$(date +%Y%m%d_%H%M)"
mkdir -p $BACKUP_DIR

echo "开始 Debian 迁移备份..."

# 1. 系统配置备份
echo "备份系统配置..."
tar --exclude='/proc' --exclude='/tmp' --exclude='/mnt' \
    --exclude='/dev' --exclude='/sys' --exclude='/run' \
    --exclude='/backup' \
    -czf $BACKUP_DIR/etc_config.tar.gz /etc/

# 2. 用户数据备份
echo "备份用户数据..."
tar -czf $BACKUP_DIR/home_backup.tar.gz /home/

# 3. Web 内容备份
if [ -d "/var/www" ]; then
    tar -czf $BACKUP_DIR/www_content.tar.gz /var/www/
fi

# 4. 数据库备份
echo "备份数据库..."
if systemctl is-active --quiet mysql mariadb; then
    mysqldump --all-databases --single-transaction > $BACKUP_DIR/mysql_all.sql
    echo "MySQL 备份完成"
fi

if systemctl is-active --quiet postgresql; then
    sudo -u postgres pg_dumpall > $BACKUP_DIR/postgresql_all.sql
    echo "PostgreSQL 备份完成"
fi

# 5. 应用程序数据备份
echo "备份应用程序数据..."
if [ -d "/opt" ]; then
    tar -czf $BACKUP_DIR/opt_apps.tar.gz /opt/
fi

# 6. 日志文件备份
tar -czf $BACKUP_DIR/log_files.tar.gz /var/log/

# 7. cron 任务备份
echo "备份计划任务..."
for user in $(cut -f1 -d: /etc/passwd); do
    if crontab -u $user -l >/dev/null 2>&1; then
        echo "=== $user ===" >> $BACKUP_DIR/all_crontabs.txt
        crontab -u $user -l >> $BACKUP_DIR/all_crontabs.txt 2>/dev/null
    fi
done

# 8. 网络配置备份
echo "备份网络配置..."
cp -r /etc/network $BACKUP_DIR/ 2>/dev/null || true
cp /etc/resolv.conf $BACKUP_DIR/
cp /etc/hosts $BACKUP_DIR/

# 9. SSL 证书备份
echo "备份 SSL 证书..."
if [ -d "/etc/ssl" ]; then
    cp -r /etc/ssl $BACKUP_DIR/
fi

if [ -d "/etc/letsencrypt" ]; then
    cp -r /etc/letsencrypt $BACKUP_DIR/
fi

# 10. 创建迁移清单
cat > $BACKUP_DIR/migration_checklist.txt << 'CHECKLIST'
Debian 到 AlmaLinux 迁移清单:

[ ] 1. AlmaLinux 10 系统安装完成
[ ] 2. 基础网络配置恢复
[ ] 3. 用户账户创建
[ ] 4. 软件包安装和配置
[ ] 5. Web 服务器配置恢复
[ ] 6. 数据库服务恢复
[ ] 7. 应用程序数据恢复
[ ] 8. SSL 证书配置
[ ] 9. 防火墙规则配置
[ ] 10. 计划任务恢复
[ ] 11. 监控和日志配置
[ ] 12. 功能测试验证
CHECKLIST

echo "备份完成: $BACKUP_DIR"
ls -la $BACKUP_DIR
EOF

chmod +x /root/debian_migration_backup.sh
/root/debian_migration_backup.sh

2.2 配置文件转换工具

创建配置转换脚本

bash
cat > /root/config_converter.sh << 'EOF'
#!/bin/bash

BACKUP_DIR="/backup/debian_to_alma_$(date +%Y%m%d)*"
CONVERT_DIR="/tmp/converted_configs"
mkdir -p $CONVERT_DIR

echo "=== 配置文件转换工具 ==="

# 1. Apache 配置转换
convert_apache_config() {
    if [ -d "$BACKUP_DIR/etc/apache2" ]; then
        echo "转换 Apache 配置..."
        mkdir -p $CONVERT_DIR/httpd
        
        # 主配置文件转换
        sed -e 's|/var/log/apache2|/var/log/httpd|g' \
            -e 's|/etc/apache2|/etc/httpd|g' \
            -e 's|/var/www/html|/var/www/html|g' \
            $BACKUP_DIR/etc/apache2/apache2.conf > $CONVERT_DIR/httpd/httpd.conf
        
        # 虚拟主机转换
        if [ -d "$BACKUP_DIR/etc/apache2/sites-available" ]; then
            cp -r $BACKUP_DIR/etc/apache2/sites-available $CONVERT_DIR/httpd/conf.d/
        fi
    fi
}

# 2. PHP 配置转换
convert_php_config() {
    if [ -f "$BACKUP_DIR/etc/php/*/apache2/php.ini" ]; then
        echo "转换 PHP 配置..."
        mkdir -p $CONVERT_DIR/php
        
        # 复制 PHP 配置,路径适配 RHEL 系统
        cp $BACKUP_DIR/etc/php/*/apache2/php.ini $CONVERT_DIR/php/
        
        # 调整路径
        sed -i 's|/var/log/apache2|/var/log/httpd|g' $CONVERT_DIR/php/php.ini
    fi
}

# 3. 网络配置转换
convert_network_config() {
    echo "转换网络配置..."
    mkdir -p $CONVERT_DIR/network-scripts
    
    # 从 Debian interfaces 转换为 RHEL network-scripts
    if [ -f "$BACKUP_DIR/etc/network/interfaces" ]; then
        while read -r line; do
            if [[ $line =~ ^iface[[:space:]]+([^[:space:]]+)[[:space:]]+inet[[:space:]]+static ]]; then
                interface="${BASH_REMATCH[1]}"
                echo "DEVICE=$interface" > $CONVERT_DIR/network-scripts/ifcfg-$interface
                echo "BOOTPROTO=static" >> $CONVERT_DIR/network-scripts/ifcfg-$interface
                echo "ONBOOT=yes" >> $CONVERT_DIR/network-scripts/ifcfg-$interface
            fi
        done < $BACKUP_DIR/etc/network/interfaces
    fi
}

# 4. 防火墙规则转换
convert_firewall_config() {
    echo "转换防火墙配置..."
    mkdir -p $CONVERT_DIR/firewall
    
    if [ -f "$BACKUP_DIR/iptables_rules.txt" ]; then
        # 生成 firewalld 配置脚本
        cat > $CONVERT_DIR/firewall/configure_firewalld.sh << 'FW_SCRIPT'
#!/bin/bash

# 从 iptables 规则转换的 firewalld 配置
echo "配置 firewalld 规则..."

# 启用 firewalld
systemctl enable --now firewalld

# 基础服务配置 (根据实际需求调整)
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https

# 重新加载配置
firewall-cmd --reload

echo "防火墙配置完成"
FW_SCRIPT
        
        chmod +x $CONVERT_DIR/firewall/configure_firewalld.sh
    fi
}

# 执行转换
convert_apache_config
convert_php_config
convert_network_config
convert_firewall_config

echo "配置转换完成,结果保存在: $CONVERT_DIR"
EOF

chmod +x /root/config_converter.sh
/root/config_converter.sh

第三阶段:AlmaLinux 10 环境搭建

3.1 基础系统配置

AlmaLinux 安装后配置脚本

bash
cat > /root/almalinux_post_install.sh << 'EOF'
#!/bin/bash

echo "=== AlmaLinux 10 迁移后配置 ==="

# 1. 系统更新
echo "更新系统..."
dnf update -y

# 2. 安装 EPEL 仓库
echo "安装 EPEL 仓库..."
dnf install -y epel-release

# 3. 配置国内镜像源
echo "配置镜像源..."
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://repo.almalinux.org|baseurl=https://mirrors.aliyun.com/almalinux|g' \
    -i.bak /etc/yum.repos.d/almalinux*.repo

# 4. 安装基础工具
echo "安装基础工具..."
dnf groupinstall -y "Development Tools"
dnf install -y wget curl vim net-tools bind-utils

# 5. 配置 SELinux (根据需求)
echo "配置 SELinux..."
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config

# 6. 配置防火墙
echo "配置防火墙..."
systemctl enable --now firewalld
firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload

# 7. 时区设置
echo "设置时区..."
timedatectl set-timezone Asia/Shanghai

echo "基础配置完成"
EOF

chmod +x /root/almalinux_post_install.sh
/root/almalinux_post_install.sh

3.2 应用环境重建

Web 服务器环境重建

bash
cat > /root/rebuild_web_environment.sh << 'EOF'
#!/bin/bash

echo "=== 重建 Web 服务器环境 ==="

BACKUP_DIR="/backup/debian_to_alma_*"

# 1. 安装 Apache HTTP Server
echo "安装 Apache..."
dnf install -y httpd httpd-tools mod_ssl

# 2. 安装 PHP
echo "安装 PHP..."
dnf install -y php php-fpm php-mysqlnd php-curl php-json php-xml php-mbstring php-zip

# 3. 安装 MariaDB
echo "安装 MariaDB..."
dnf install -y mariadb-server mariadb

# 4. 启动服务
echo "启动服务..."
systemctl enable --now httpd
systemctl enable --now php-fpm
systemctl enable --now mariadb

# 5. 配置防火墙
echo "配置防火墙..."
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

# 6. 恢复配置文件 (如果存在转换后的配置)
if [ -d "/tmp/converted_configs/httpd" ]; then
    echo "恢复 Apache 配置..."
    cp /tmp/converted_configs/httpd/* /etc/httpd/conf.d/ 2>/dev/null || true
fi

# 7. 恢复 Web 内容
if [ -f "$BACKUP_DIR/www_content.tar.gz" ]; then
    echo "恢复 Web 内容..."
    cd /
    tar -xzf $BACKUP_DIR/www_content.tar.gz
fi

# 8. 重启服务
systemctl restart httpd
systemctl restart php-fpm

echo "Web 环境重建完成"
EOF

chmod +x /root/rebuild_web_environment.sh
/root/rebuild_web_environment.sh

3.3 数据库迁移和恢复

数据库恢复脚本

bash
cat > /root/restore_databases.sh << 'EOF'
#!/bin/bash

echo "=== 数据库恢复 ==="

BACKUP_DIR="/backup/debian_to_alma_*"

# 1. 配置 MariaDB 安全设置
echo "配置 MariaDB..."
mysql_secure_installation <<MYSQL_SECURE
y
password
password
y
y
y
y
MYSQL_SECURE

# 2. 恢复 MySQL/MariaDB 数据
if [ -f "$BACKUP_DIR/mysql_all.sql" ]; then
    echo "恢复 MySQL 数据..."
    mysql -u root -p < $BACKUP_DIR/mysql_all.sql
    echo "MySQL 数据恢复完成"
fi

# 3. 安装和配置 PostgreSQL (如果需要)
if [ -f "$BACKUP_DIR/postgresql_all.sql" ]; then
    echo "安装 PostgreSQL..."
    dnf install -y postgresql-server postgresql-contrib
    
    # 初始化数据库
    postgresql-setup --initdb
    systemctl enable --now postgresql
    
    # 恢复数据
    sudo -u postgres psql < $BACKUP_DIR/postgresql_all.sql
    echo "PostgreSQL 数据恢复完成"
fi

# 4. 验证数据库服务
systemctl status mariadb
systemctl status postgresql 2>/dev/null || echo "PostgreSQL 未安装"

echo "数据库恢复完成"
EOF

chmod +x /root/restore_databases.sh
/root/restore_databases.sh

第四阶段:服务和应用迁移

4.1 用户账户和权限迁移

用户迁移脚本

bash
cat > /root/migrate_users.sh << 'EOF'
#!/bin/bash

echo "=== 用户账户迁移 ==="

BACKUP_DIR="/backup/debian_to_alma_*"

# 1. 恢复用户账户 (跳过系统用户)
if [ -f "$BACKUP_DIR/debian_users.txt" ]; then
    echo "恢复用户账户..."
    
    # 提取用户 ID >= 1000 的普通用户
    awk -F: '$3 >= 1000 && $3 < 65534 {print $0}' $BACKUP_DIR/debian_users.txt > /tmp/users_to_migrate.txt
    
    while IFS=: read -r username password uid gid gecos home shell; do
        if ! id "$username" &>/dev/null; then
            echo "创建用户: $username"
            useradd -u $uid -g $gid -d $home -s $shell -c "$gecos" $username
            
            # 设置临时密码 (需要用户首次登录修改)
            echo "$username:temp123456" | chpasswd
            echo "用户 $username 已创建,临时密码: temp123456"
        fi
    done < /tmp/users_to_migrate.txt
fi

# 2. 恢复用户数据
if [ -f "$BACKUP_DIR/home_backup.tar.gz" ]; then
    echo "恢复用户数据..."
    cd /
    tar -xzf $BACKUP_DIR/home_backup.tar.gz
    
    # 修复权限
    for homedir in /home/*; do
        if [ -d "$homedir" ]; then
            username=$(basename $homedir)
            if id "$username" &>/dev/null; then
                chown -R $username:$username $homedir
                echo "已修复 $username 的权限"
            fi
        fi
    done
fi

echo "用户迁移完成"
EOF

chmod +x /root/migrate_users.sh
/root/migrate_users.sh

4.2 计划任务和服务迁移

计划任务恢复

bash
cat > /root/restore_cron_jobs.sh << 'EOF'
#!/bin/bash

echo "=== 恢复计划任务 ==="

BACKUP_DIR="/backup/debian_to_alma_*"

# 1. 安装 cron 服务
dnf install -y cronie
systemctl enable --now crond

# 2. 恢复 cron 任务
if [ -f "$BACKUP_DIR/all_crontabs.txt" ]; then
    echo "恢复计划任务..."
    
    current_user=""
    temp_crontab=""
    
    while IFS= read -r line; do
        if [[ $line =~ ^===\ (.+)\ ===$ ]]; then
            # 保存上一个用户的 crontab
            if [ -n "$current_user" ] && [ -n "$temp_crontab" ]; then
                echo "$temp_crontab" | crontab -u $current_user -
                echo "已恢复 $current_user 的计划任务"
            fi
            
            # 开始新用户
            current_user="${BASH_REMATCH[1]}"
            temp_crontab=""
        else
            temp_crontab="$temp_crontab$line\n"
        fi
    done < $BACKUP_DIR/all_crontabs.txt
    
    # 保存最后一个用户的 crontab
    if [ -n "$current_user" ] && [ -n "$temp_crontab" ]; then
        echo -e "$temp_crontab" | crontab -u $current_user -
        echo "已恢复 $current_user 的计划任务"
    fi
fi

# 3. 验证计划任务
echo "当前计划任务:"
for user in $(cut -f1 -d: /etc/passwd); do
    if crontab -u $user -l >/dev/null 2>&1; then
        echo "用户 $user 的计划任务:"
        crontab -u $user -l
        echo "---"
    fi
done

echo "计划任务恢复完成"
EOF

chmod +x /root/restore_cron_jobs.sh
/root/restore_cron_jobs.sh

第五阶段:验证和优化

5.1 迁移验证

全面系统验证脚本

bash
cat > /root/migration_verification.sh << 'EOF'
#!/bin/bash

echo "=== Debian 到 AlmaLinux 迁移验证 ==="

# 1. 系统基础验证
echo "1. 系统信息验证:"
echo "操作系统: $(cat /etc/almalinux-release)"
echo "内核版本: $(uname -r)"
echo "系统架构: $(uname -m)"

# 2. 网络连通性验证
echo -e "\n2. 网络验证:"
if ping -c 3 8.8.8.8 >/dev/null 2>&1; then
    echo "✓ 网络连接正常"
else
    echo "✗ 网络连接异常"
fi

# 3. Web 服务验证
echo -e "\n3. Web 服务验证:"
if systemctl is-active --quiet httpd; then
    echo "✓ Apache HTTP Server 运行正常"
    if curl -I http://localhost >/dev/null 2>&1; then
        echo "✓ Web 服务响应正常"
    else
        echo "✗ Web 服务无响应"
    fi
else
    echo "✗ Apache HTTP Server 未运行"
fi

# 4. PHP 验证
echo -e "\n4. PHP 验证:"
if command -v php >/dev/null 2>&1; then
    echo "✓ PHP 已安装: $(php -v | head -1)"
    
    # 创建 PHP 测试文件
    echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
    echo "PHP 信息页面: http://localhost/phpinfo.php"
else
    echo "✗ PHP 未安装"
fi

# 5. 数据库验证
echo -e "\n5. 数据库验证:"
if systemctl is-active --quiet mariadb; then
    echo "✓ MariaDB 运行正常"
    if mysql -e "SELECT VERSION();" >/dev/null 2>&1; then
        echo "✓ 数据库连接正常"
        mysql -e "SHOW DATABASES;"
    else
        echo "✗ 数据库连接失败"
    fi
else
    echo "✗ MariaDB 未运行"
fi

# 6. 用户账户验证
echo -e "\n6. 用户账户验证:"
echo "普通用户列表:"
awk -F: '$3 >= 1000 && $3 < 65534 {print $1}' /etc/passwd

# 7. 计划任务验证
echo -e "\n7. 计划任务验证:"
if systemctl is-active --quiet crond; then
    echo "✓ Cron 服务运行正常"
    echo "活动的计划任务数量: $(crontab -l 2>/dev/null | grep -v '^#' | wc -l)"
else
    echo "✗ Cron 服务未运行"
fi

# 8. 防火墙验证
echo -e "\n8. 防火墙验证:"
if systemctl is-active --quiet firewalld; then
    echo "✓ Firewalld 运行正常"
    echo "开放的服务:"
    firewall-cmd --list-services
else
    echo "✗ Firewalld 未运行"
fi

# 9. 存储验证
echo -e "\n9. 存储验证:"
echo "磁盘使用情况:"
df -h

# 10. 性能基准测试
echo -e "\n10. 性能验证:"
echo "系统负载: $(uptime)"
echo "内存使用: $(free -h | grep Mem)"

echo -e "\n=== 验证完成 ==="
EOF

chmod +x /root/migration_verification.sh
/root/migration_verification.sh

5.2 性能优化

AlmaLinux 性能优化脚本

bash
cat > /root/almalinux_optimization.sh << 'EOF'
#!/bin/bash

echo "=== AlmaLinux 性能优化 ==="

# 1. 内核参数优化
echo "优化内核参数..."
cat >> /etc/sysctl.conf << 'SYSCTL'

# 网络优化
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_congestion_control = bbr

# 文件系统优化
fs.file-max = 2097152
vm.swappiness = 10
vm.dirty_ratio = 15

# 进程优化
kernel.pid_max = 65536
SYSCTL

sysctl -p

# 2. 文件系统优化
echo "优化文件系统..."
if mount | grep -q ext4; then
    echo "检测到 ext4 文件系统,添加优化挂载选项"
    # 添加 noatime 优化 (需要手动编辑 /etc/fstab)
    sed -i 's/defaults/defaults,noatime/' /etc/fstab
fi

# 3. Apache 性能优化
echo "优化 Apache 配置..."
cat >> /etc/httpd/conf.d/performance.conf << 'APACHE'
# Apache 性能优化
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15

# 工作进程优化
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxRequestWorkers 256
ThreadsPerChild 25

# 缓存优化
LoadModule expires_module modules/mod_expires.so
ExpiresActive On
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
APACHE

# 4. PHP 性能优化
echo "优化 PHP 配置..."
sed -i 's/memory_limit = .*/memory_limit = 256M/' /etc/php.ini
sed -i 's/max_execution_time = .*/max_execution_time = 300/' /etc/php.ini
sed -i 's/upload_max_filesize = .*/upload_max_filesize = 100M/' /etc/php.ini
sed -i 's/post_max_size = .*/post_max_size = 100M/' /etc/php.ini

# 启用 OPcache
sed -i 's/;opcache.enable=.*/opcache.enable=1/' /etc/php.d/10-opcache.ini

# 5. MariaDB 性能优化
echo "优化 MariaDB 配置..."
cat >> /etc/my.cnf.d/performance.cnf << 'MYSQL'
[mysqld]
# 内存优化
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
key_buffer_size = 128M
query_cache_size = 64M

# 连接优化
max_connections = 200
thread_cache_size = 8

# 日志优化
slow_query_log = 1
slow_query_log_file = /var/log/mariadb/slow.log
long_query_time = 2
MYSQL

# 6. 重启优化的服务
echo "重启服务应用优化..."
systemctl restart httpd
systemctl restart php-fpm
systemctl restart mariadb

echo "性能优化完成"
EOF

chmod +x /root/almalinux_optimization.sh
/root/almalinux_optimization.sh

故障排查和技术支持

常见问题解决

迁移问题诊断工具

bash
cat > /root/migration_troubleshoot.sh << 'EOF'
#!/bin/bash

echo "=== Debian 到 AlmaLinux 迁移故障排查 ==="

# 1. 包依赖问题检查
echo "1. 检查包依赖问题:"
dnf check

# 2. 服务状态检查
echo -e "\n2. 关键服务状态:"
for service in httpd php-fpm mariadb crond firewalld; do
    if systemctl is-enabled $service >/dev/null 2>&1; then
        status=$(systemctl is-active $service)
        echo "$service: $status"
        if [ "$status" != "active" ]; then
            echo "  错误日志:"
            journalctl -u $service --no-pager -n 5
        fi
    fi
done

# 3. 网络问题检查
echo -e "\n3. 网络配置检查:"
ip addr show
echo "DNS 配置:"
cat /etc/resolv.conf

# 4. 权限问题检查
echo -e "\n4. 关键目录权限检查:"
ls -la /var/www/html/
ls -la /etc/httpd/conf.d/

# 5. 日志文件检查
echo -e "\n5. 近期错误日志:"
tail -20 /var/log/httpd/error_log 2>/dev/null || echo "Apache 错误日志不存在"
tail -20 /var/log/mariadb/mariadb.log 2>/dev/null || echo "MariaDB 日志不存在"

echo -e "\n=== 故障排查完成 ==="
EOF

chmod +x /root/migration_troubleshoot.sh

总结: Debian 到 AlmaLinux 10 的跨发行版迁移是一个复杂的过程,需要仔细规划和详细的步骤执行。本指南提供了完整的迁移流程,包括系统分析、数据备份、环境重建、服务迁移和验证优化。虽然迁移复杂度较高,但通过系统性的方法可以实现平滑的系统转换。

相关文档:

基于 MIT 许可发布