commit f203124a2cd68a91f19805884b45ed2c0b932edb Author: kuzyn_pro Date: Tue May 27 17:06:22 2025 +0200 Add files via upload diff --git a/freepbx_uninstall.sh b/freepbx_uninstall.sh new file mode 100644 index 0000000..8e194a7 --- /dev/null +++ b/freepbx_uninstall.sh @@ -0,0 +1,833 @@ +#!/bin/bash +##################################################################################### +# FreePBX 17 Uninstall Script +# This script removes FreePBX 17 and all its components installed by the official +# installation script. +# +# WARNING: This will remove ALL FreePBX data, configurations, and databases! +# Make sure to backup any important data before running this script. +##################################################################################### + +set -e + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Logging setup +LOG_FILE="/var/log/freepbx-uninstall.log" +BACKUP_DIR="/var/backups/freepbx-uninstall-$(date +%Y%m%d-%H%M%S)" + +# Check for root privileges +if [[ $EUID -ne 0 ]]; then + echo -e "${RED}This script must be run as root${NC}" + exit 1 +fi + +# Initialize logging +init_logging() { + mkdir -p "$(dirname "$LOG_FILE")" + touch "$LOG_FILE" + echo "===================================================================================" >> "$LOG_FILE" + echo "FreePBX 17 Uninstallation started at $(date)" >> "$LOG_FILE" + echo "Script executed by: $(whoami)" >> "$LOG_FILE" + echo "System: $(uname -a)" >> "$LOG_FILE" + echo "===================================================================================" >> "$LOG_FILE" +} + +# Function to safely remove packages with dependency checking +safe_package_removal() { + local packages=("$@") + local packages_to_remove=() + local packages_with_deps=() + + log_message "Analyzing packages for safe removal..." + echo "PACKAGE_ANALYSIS_START" >> "$LOG_FILE" + + for package in "${packages[@]}"; do + # Check if package is installed first + if ! dpkg -l | grep -q "^ii.*$package" 2>/dev/null; then + log_message "Package '$package' is not installed - skipping" + continue + fi + + echo "Analyzing package: $package" + if check_package_dependencies "$package"; then + packages_to_remove+=("$package") + echo "SAFE_TO_REMOVE: $package" >> "$LOG_FILE" + else + packages_with_deps+=("$package") + echo "HAS_DEPENDENCIES: $package" >> "$LOG_FILE" + fi + done + + echo "PACKAGE_ANALYSIS_END" >> "$LOG_FILE" + + # Show summary + if [[ ${#packages_to_remove[@]} -gt 0 ]]; then + echo -e "${GREEN}Safe to remove (no dependencies):${NC}" + printf '%s\n' "${packages_to_remove[@]}" + echo "" + fi + + if [[ ${#packages_with_deps[@]} -gt 0 ]]; then + echo -e "${YELLOW}Packages with dependencies (need confirmation):${NC}" + printf '%s\n' "${packages_with_deps[@]}" + echo "" + + read -p "Do you want to remove packages with dependencies? (y/N): " remove_deps + if [[ "$remove_deps" =~ ^[Yy]$ ]]; then + packages_to_remove+=("${packages_with_deps[@]}") + echo "USER_CONFIRMED_DEPENDENCY_REMOVAL" >> "$LOG_FILE" + else + log_warning "Skipping packages with dependencies" + echo "USER_DECLINED_DEPENDENCY_REMOVAL" >> "$LOG_FILE" + fi + fi + + # Remove approved packages + if [[ ${#packages_to_remove[@]} -gt 0 ]]; then + log_message "Removing approved packages..." + echo "REMOVING_PACKAGES: ${packages_to_remove[*]}" >> "$LOG_FILE" + + for package in "${packages_to_remove[@]}"; do + log_message "Removing package: $package" + echo "REMOVING: $package" >> "$LOG_FILE" + apt-get purge -y "$package" >> "$LOG_FILE" 2>&1 || { + log_error "Failed to remove package: $package" + echo "FAILED_REMOVAL: $package" >> "$LOG_FILE" + } + done + else + log_message "No packages approved for removal" + echo "NO_PACKAGES_REMOVED" >> "$LOG_FILE" + fi +} +check_proxmox() { + if dpkg -l | grep -q "proxmox-ve" || [[ -f /etc/pve/local/pve-ssl.pem ]] || systemctl list-units | grep -q "pveproxy"; then + echo -e "${RED}⚠️ PROXMOX DETECTED ⚠️${NC}" + echo -e "${YELLOW}This system appears to be running Proxmox VE!${NC}" + echo "" + echo "FreePBX and Proxmox share some common packages (Apache2, Fail2ban, etc.)" + echo "Removing these packages could break your Proxmox installation!" + echo "" + echo "This script has been modified to be more careful with shared packages," + echo "but there's still some risk involved." + echo "" + read -p "Do you understand the risks and want to continue? (y/N): " continue_proxmox + + if [[ "$continue_proxmox" != "y" && "$continue_proxmox" != "Y" ]]; then + log_message "Uninstallation cancelled due to Proxmox detection." + exit 0 + fi + + echo -e "${YELLOW}Proceeding with Proxmox-safe mode...${NC}" + echo "" + return 0 + fi + + return 1 +} + +# Function to log messages +log_message() { + local message="$1" + echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $message${NC}" + echo "[$(date '+%Y-%m-%d %H:%M:%S')] INFO: $message" >> "$LOG_FILE" +} + +log_warning() { + local message="$1" + echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] WARNING: $message${NC}" + echo "[$(date '+%Y-%m-%d %H:%M:%S')] WARNING: $message" >> "$LOG_FILE" +} + +log_error() { + local message="$1" + echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $message${NC}" + echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $message" >> "$LOG_FILE" +} + +# Function to check package dependencies before removal +check_package_dependencies() { + local package="$1" + local dependencies + + log_message "Checking dependencies for package: $package" + echo "Checking dependencies for package: $package" >> "$LOG_FILE" + + # Check what depends on this package + dependencies=$(apt-cache rdepends --installed "$package" 2>/dev/null | grep -v "^$package$" | grep -v "Reverse Depends:" | sed 's/^ //' | sort -u) + + if [[ -n "$dependencies" ]]; then + log_warning "Package '$package' has the following dependencies:" + echo "$dependencies" | while read -r dep; do + if [[ -n "$dep" ]]; then + echo " - $dep" + echo " DEPENDENCY: $dep" >> "$LOG_FILE" + fi + done + echo "" + return 1 # Has dependencies + else + log_message "Package '$package' has no dependencies - safe to remove" + echo "Package '$package' - no dependencies found" >> "$LOG_FILE" + return 0 # No dependencies + fi +} + +# Function to create automatic backup +create_backup() { + log_message "Creating automatic backup before uninstallation..." + echo "Creating backup in: $BACKUP_DIR" >> "$LOG_FILE" + + mkdir -p "$BACKUP_DIR" + + # Backup important directories and files + backup_items=( + "/etc/asterisk" + "/etc/freepbx" + "/var/lib/asterisk" + "/var/www/html" + "/etc/apache2/sites-available" + "/etc/apache2/sites-enabled" + "/etc/ssl/openssl.cnf" + "/etc/gai.conf" + "/root/.screenrc" + "/etc/vim/vimrc.local" + "/etc/apt/sources.list" + "/etc/apt/sources.list.d" + "/etc/apt/preferences.d" + "/etc/apt/apt.conf.d" + "/etc/postfix/main.cf" + "/etc/default/tftpd-hpa" + "/etc/default/chrony" + ) + + for item in "${backup_items[@]}"; do + if [[ -e "$item" ]]; then + log_message "Backing up: $item" + echo "BACKUP: $item" >> "$LOG_FILE" + + # Create directory structure in backup + backup_path="$BACKUP_DIR$item" + mkdir -p "$(dirname "$backup_path")" + + if [[ -d "$item" ]]; then + cp -r "$item" "$backup_path" 2>/dev/null || true + else + cp "$item" "$backup_path" 2>/dev/null || true + fi + fi + done + + # Backup package list + log_message "Backing up package lists..." + dpkg --get-selections > "$BACKUP_DIR/packages-before-uninstall.txt" + apt list --installed > "$BACKUP_DIR/apt-list-before-uninstall.txt" 2>/dev/null + + # Backup service status + systemctl list-units --type=service --state=active > "$BACKUP_DIR/services-before-uninstall.txt" + + # Create restore instructions + cat > "$BACKUP_DIR/RESTORE_INSTRUCTIONS.txt" << EOF +FreePBX Uninstall Backup Created: $(date) +===================================== + +This backup contains: +- Configuration files from /etc/asterisk, /etc/freepbx, etc. +- Web files from /var/www/html +- Apache configurations +- System configuration files +- Package lists before uninstallation +- Service status before uninstallation + +To restore files: +1. Stop relevant services +2. Copy files from this backup to their original locations +3. Restart services +4. Reinstall packages if needed using packages-before-uninstall.txt + +WARNING: Always check file permissions and ownership after restore! + +Backup location: $BACKUP_DIR +Log file: $LOG_FILE +EOF + + # Set proper permissions + chmod -R 600 "$BACKUP_DIR" + chmod 644 "$BACKUP_DIR/RESTORE_INSTRUCTIONS.txt" + + log_message "Backup completed successfully!" + log_message "Backup location: $BACKUP_DIR" + echo "BACKUP_COMPLETED: $BACKUP_DIR" >> "$LOG_FILE" +} + +# Function to confirm action +confirm_action() { + echo -e "${RED}" + echo "===============================================================================" + echo " ⚠️ DANGER ZONE ⚠️" + echo "===============================================================================" + echo "This script will COMPLETELY REMOVE FreePBX 17 and all related components!" + echo "" + echo "This includes:" + echo "• FreePBX application and all modules" + echo "• Asterisk and all its components" + echo "• All databases (including call records, configurations, etc.)" + echo "• All user accounts and settings" + echo "• All audio files and recordings" + echo "• All custom configurations" + echo "" + echo "⚠️ ALL DATA WILL BE PERMANENTLY LOST! ⚠️" + echo "===============================================================================" + echo -e "${NC}" + echo "" + echo "A complete backup will be created automatically before any changes." + echo "Backup location: $BACKUP_DIR" + echo "Log file: $LOG_FILE" + echo "" + + echo "USER_CONFIRMATION_REQUESTED" >> "$LOG_FILE" + + read -p "Are you absolutely sure you want to continue? Type 'YES' to proceed: " confirm + + if [[ "$confirm" != "YES" ]]; then + log_message "Uninstallation cancelled by user." + echo "USER_CANCELLED_UNINSTALL" >> "$LOG_FILE" + exit 0 + fi + + echo "" + log_warning "Starting uninstallation in 5 seconds... Press Ctrl+C to cancel!" + echo "USER_CONFIRMED_UNINSTALL" >> "$LOG_FILE" + echo "COUNTDOWN_START" >> "$LOG_FILE" + + for i in {5..1}; do + echo "Starting in $i seconds..." + sleep 1 + done + + echo "COUNTDOWN_END - STARTING_UNINSTALL" >> "$LOG_FILE" +} + +# Function to stop and disable services +stop_services() { + log_message "Stopping FreePBX and related services..." + echo "SERVICE_STOP_START" >> "$LOG_FILE" + + services=("freepbx" "asterisk" "apache2" "mariadb" "fail2ban" "postfix" "tftpd-hpa" "chrony" "redis-server") + + for service in "${services[@]}"; do + if systemctl is-active --quiet "$service" 2>/dev/null; then + log_message "Stopping $service..." + echo "STOPPING_SERVICE: $service" >> "$LOG_FILE" + systemctl stop "$service" >> "$LOG_FILE" 2>&1 || { + log_warning "Failed to stop $service" + echo "FAILED_TO_STOP: $service" >> "$LOG_FILE" + } + else + echo "SERVICE_NOT_ACTIVE: $service" >> "$LOG_FILE" + fi + + if systemctl is-enabled --quiet "$service" 2>/dev/null; then + log_message "Disabling $service..." + echo "DISABLING_SERVICE: $service" >> "$LOG_FILE" + systemctl disable "$service" >> "$LOG_FILE" 2>&1 || { + log_warning "Failed to disable $service" + echo "FAILED_TO_DISABLE: $service" >> "$LOG_FILE" + } + else + echo "SERVICE_NOT_ENABLED: $service" >> "$LOG_FILE" + fi + done + + echo "SERVICE_STOP_END" >> "$LOG_FILE" +} + +# Function to remove FreePBX packages +remove_freepbx_packages() { + log_message "Removing FreePBX packages..." + echo "FREEPBX_PACKAGE_REMOVAL_START" >> "$LOG_FILE" + + # Core FreePBX packages + freepbx_packages=( + "freepbx17" + "sangoma-pbx17" + "sysadmin17" + "ioncube-loader-82" + ) + + log_message "Removing core FreePBX packages with dependency checking..." + safe_package_removal "${freepbx_packages[@]}" + + # Remove Asterisk packages + log_message "Removing Asterisk packages..." + asterisk_packages=($(dpkg -l | grep "asterisk" | awk '{print $2}')) + if [[ ${#asterisk_packages[@]} -gt 0 ]]; then + echo "Found Asterisk packages: ${asterisk_packages[*]}" >> "$LOG_FILE" + safe_package_removal "${asterisk_packages[@]}" + else + log_message "No Asterisk packages found" + fi + + # Remove DAHDI packages if installed + log_message "Checking for DAHDI packages..." + dahdi_packages=($(dpkg -l | grep -E "(dahdi|libpri|wanpipe)" | awk '{print $2}')) + if [[ ${#dahdi_packages[@]} -gt 0 ]]; then + log_message "Found DAHDI packages: ${dahdi_packages[*]}" + echo "Found DAHDI packages: ${dahdi_packages[*]}" >> "$LOG_FILE" + safe_package_removal "${dahdi_packages[@]}" + else + log_message "No DAHDI packages found" + fi + + echo "FREEPBX_PACKAGE_REMOVAL_END" >> "$LOG_FILE" +} + +# Function to remove other installed packages +remove_other_packages() { + log_message "Removing other packages installed by FreePBX installer..." + echo "OTHER_PACKAGES_REMOVAL_START" >> "$LOG_FILE" + + # SAFE packages that are unlikely to be used by Proxmox + safe_packages_to_remove=( + "sox" + "mpg123" + "lame" + "sngrep" + "tftpd-hpa" + "xinetd" + "php8.2-cli" + "php8.2-common" + "php8.2-curl" + "php8.2-gd" + "php8.2-mysql" + "php8.2-xml" + "php8.2-zip" + "php8.2-mbstring" + "php8.2-intl" + "php8.2-bz2" + "php8.2-ldap" + "php8.2-sqlite3" + "php8.2-bcmath" + "php8.2-soap" + "php8.2-ssh2" + "php8.2-redis" + "php-pear" + "odbc-mariadb" + "unixodbc" + "python3-mysqldb" + "python-is-python3" + "mailutils" + "easy-rsa" + "openvpn" + "incron" + ) + + # POTENTIALLY DANGEROUS packages that might be used by Proxmox + dangerous_packages=( + "apache2" + "fail2ban" + "redis-server" + "mariadb-server" + "mariadb-client" + "nodejs" + "npm" + "postfix" + "iptables-persistent" + "avahi-daemon" + "avahi-utils" + "libnss-mdns" + "haproxy" + "ffmpeg" + ) + + # Remove safe packages without asking + log_message "Removing FreePBX-specific packages (safe to remove)..." + echo "REMOVING_SAFE_PACKAGES" >> "$LOG_FILE" + safe_package_removal "${safe_packages_to_remove[@]}" + + echo -e "${RED}⚠️ PROXMOX WARNING ⚠️${NC}" + echo -e "${YELLOW}The following packages might be used by Proxmox:${NC}" + printf '%s\n' "${dangerous_packages[@]}" + echo "" + echo -e "${RED}Removing these packages could break your Proxmox installation!${NC}" + echo -e "${YELLOW}Since FreePBX was never started, these packages are probably safe to keep.${NC}" + echo "" + read -p "Do you want to analyze potentially dangerous packages for removal? (y/N): " analyze_dangerous + + if [[ "$analyze_dangerous" =~ ^[Yy]$ ]]; then + echo -e "${RED}Analyzing dangerous packages with dependency checking...${NC}" + echo "ANALYZING_DANGEROUS_PACKAGES" >> "$LOG_FILE" + sleep 2 + + safe_package_removal "${dangerous_packages[@]}" + + log_message "Dangerous packages analysis completed." + else + log_message "Keeping potentially dangerous packages for Proxmox compatibility." + echo "KEEPING_DANGEROUS_PACKAGES_FOR_PROXMOX" >> "$LOG_FILE" + fi + + echo "OTHER_PACKAGES_REMOVAL_END" >> "$LOG_FILE" +} + +# Function to remove users and groups +remove_users_groups() { + log_message "Removing asterisk user and group..." + + if id "asterisk" &>/dev/null; then + userdel asterisk 2>/dev/null || true + fi + + if getent group asterisk &>/dev/null; then + groupdel asterisk 2>/dev/null || true + fi +} + +# Function to remove directories and files +remove_directories() { + log_message "Removing FreePBX directories and files..." + echo "DIRECTORY_REMOVAL_START" >> "$LOG_FILE" + + directories_to_remove=( + "/var/log/pbx" + "/var/lib/asterisk" + "/etc/asterisk" + "/tftpboot" + "/var/www/html" + "/etc/freepbx*" + "/var/lib/freepbx" + "/var/spool/asterisk" + "/usr/src/freepbx" + "/etc/openvpn/easyrsa3" + "/var/lib/php/session" + ) + + for dir in "${directories_to_remove[@]}"; do + if [[ -d "$dir" ]] || [[ -f "$dir" ]] || [[ -L "$dir" ]]; then + log_message "Removing: $dir" + echo "REMOVING_DIRECTORY: $dir" >> "$LOG_FILE" + rm -rf "$dir" 2>&1 | tee -a "$LOG_FILE" || { + log_error "Failed to remove: $dir" + echo "FAILED_TO_REMOVE: $dir" >> "$LOG_FILE" + } + else + echo "NOT_FOUND: $dir" >> "$LOG_FILE" + fi + done + + # Remove scripts created by FreePBX installer + scripts_to_remove=( + "/usr/bin/post-apt-run" + "/usr/bin/kernel-check" + ) + + for script in "${scripts_to_remove[@]}"; do + if [[ -f "$script" ]]; then + log_message "Removing script: $script" + echo "REMOVING_SCRIPT: $script" >> "$LOG_FILE" + rm -f "$script" 2>&1 | tee -a "$LOG_FILE" || { + log_error "Failed to remove script: $script" + echo "FAILED_TO_REMOVE_SCRIPT: $script" >> "$LOG_FILE" + } + else + echo "SCRIPT_NOT_FOUND: $script" >> "$LOG_FILE" + fi + done + + echo "DIRECTORY_REMOVAL_END" >> "$LOG_FILE" +} + +# Function to clean up configuration files +cleanup_configs() { + log_message "Cleaning up configuration files..." + echo "CONFIG_CLEANUP_START" >> "$LOG_FILE" + + # Be very careful with openssl.cnf as Proxmox uses SSL certificates + if grep -q "FreePBX 17 changes" /etc/ssl/openssl.cnf 2>/dev/null; then + echo -e "${YELLOW}⚠️ SSL Configuration Warning ⚠️${NC}" + echo "FreePBX modified /etc/ssl/openssl.cnf which is used by Proxmox for SSL certificates." + echo "Restoring original settings might affect Proxmox web interface." + echo "" + echo "OPENSSL_CONFIG_MODIFIED_BY_FREEPBX" >> "$LOG_FILE" + + read -p "Do you want to restore original SSL configuration? (y/N): " restore_ssl + + if [[ "$restore_ssl" =~ ^[Yy]$ ]]; then + log_message "Restoring original openssl.cnf..." + echo "RESTORING_OPENSSL_CONFIG" >> "$LOG_FILE" + sed -i '/# FreePBX 17 changes - begin/,/# FreePBX 17 changes - end/d' /etc/ssl/openssl.cnf 2>&1 | tee -a "$LOG_FILE" + sed -i 's/^openssl_conf = default_conf$/openssl_conf = openssl_init/' /etc/ssl/openssl.cnf 2>&1 | tee -a "$LOG_FILE" + log_warning "SSL configuration restored - you may need to restart Proxmox services." + echo "OPENSSL_CONFIG_RESTORED" >> "$LOG_FILE" + else + log_message "Keeping FreePBX SSL configuration for Proxmox compatibility." + echo "KEEPING_FREEPBX_OPENSSL_CONFIG" >> "$LOG_FILE" + fi + else + echo "OPENSSL_CONFIG_NOT_MODIFIED" >> "$LOG_FILE" + fi + + # Restore original gai.conf (safer) + if grep -q "^precedence ::ffff:0:0/96 100" /etc/gai.conf 2>/dev/null; then + log_message "Restoring original gai.conf..." + echo "RESTORING_GAI_CONFIG" >> "$LOG_FILE" + sed -i 's/^precedence ::ffff:0:0\/96 100/#precedence ::ffff:0:0\/96 100/' /etc/gai.conf 2>&1 | tee -a "$LOG_FILE" + echo "GAI_CONFIG_RESTORED" >> "$LOG_FILE" + else + echo "GAI_CONFIG_NOT_MODIFIED" >> "$LOG_FILE" + fi + + # Remove FreePBX changes from .screenrc (safe) + if [[ -f /root/.screenrc ]] && grep -q "FreePBX 17 changes" /root/.screenrc; then + log_message "Cleaning up .screenrc..." + echo "CLEANING_SCREENRC" >> "$LOG_FILE" + sed -i '/# FreePBX 17 changes - begin/,/# FreePBX 17 changes - end/d' /root/.screenrc 2>&1 | tee -a "$LOG_FILE" + echo "SCREENRC_CLEANED" >> "$LOG_FILE" + else + echo "SCREENRC_NOT_MODIFIED" >> "$LOG_FILE" + fi + + # Remove FreePBX changes from vimrc.local (safe) + if [[ -f /etc/vim/vimrc.local ]] && grep -q "FreePBX 17 changes" /etc/vim/vimrc.local; then + log_message "Cleaning up vimrc.local..." + echo "CLEANING_VIMRC" >> "$LOG_FILE" + sed -i '/\" FreePBX 17 changes - begin/,/\" FreePBX 17 changes - end/d' /etc/vim/vimrc.local 2>&1 | tee -a "$LOG_FILE" + echo "VIMRC_CLEANED" >> "$LOG_FILE" + else + echo "VIMRC_NOT_MODIFIED" >> "$LOG_FILE" + fi + + # Remove apt configuration files (safe) + config_files_to_remove=( + "/etc/apt/apt.conf.d/00freepbx" + "/etc/apt/apt.conf.d/80postaptcmd" + "/etc/apt/apt.conf.d/05checkkernel" + "/etc/apt/preferences.d/99sangoma-fpbx-repository" + ) + + for config in "${config_files_to_remove[@]}"; do + if [[ -f "$config" ]]; then + log_message "Removing config file: $config" + echo "REMOVING_CONFIG: $config" >> "$LOG_FILE" + rm -f "$config" 2>&1 | tee -a "$LOG_FILE" || { + log_error "Failed to remove config: $config" + echo "FAILED_TO_REMOVE_CONFIG: $config" >> "$LOG_FILE" + } + else + echo "CONFIG_NOT_FOUND: $config" >> "$LOG_FILE" + fi + done + + echo "CONFIG_CLEANUP_END" >> "$LOG_FILE" +} + +# Function to remove repositories +remove_repositories() { + log_message "Removing FreePBX repositories..." + + # Remove GPG key + apt-key del "9641 7C6E 0423 6E0A 986B 69EF DE82 7447 3C8D 0E52" 2>/dev/null || true + rm -f /etc/apt/trusted.gpg.d/freepbx.gpg + + # Remove repository entries + if [[ -f /etc/apt/sources.list.d/freepbx17-prod-bookworm.list ]]; then + rm -f /etc/apt/sources.list.d/freepbx17-prod-bookworm.list + fi + + if [[ -f /etc/apt/sources.list.d/freepbx17-dev-bookworm.list ]]; then + rm -f /etc/apt/sources.list.d/freepbx17-dev-bookworm.list + fi + + # Remove from sources.list if added there + sed -i '/deb.*freepbx\.org/d' /etc/apt/sources.list 2>/dev/null || true +} + +# Function to remove databases +remove_databases() { + echo -e "${RED}⚠️ DATABASE WARNING ⚠️${NC}" + echo -e "${YELLOW}Do you want to remove MariaDB/MySQL databases?${NC}" + echo "" + echo "Since FreePBX was never started, it probably didn't create any databases." + echo "However, if you have other applications using MariaDB (including Proxmox)," + echo "removing databases could break them!" + echo "" + echo "Options:" + echo "1) Keep all databases (RECOMMENDED for Proxmox systems)" + echo "2) Remove only FreePBX-related databases (if any exist)" + echo "3) Remove ALL databases (DANGEROUS - could break Proxmox!)" + echo "" + read -p "Choose option (1/2/3): " db_option + + case $db_option in + 2) + log_message "Looking for FreePBX databases to remove..." + if systemctl is-active --quiet mariadb 2>/dev/null; then + # Try to remove only FreePBX databases + mysql -e "DROP DATABASE IF EXISTS asterisk;" 2>/dev/null || true + mysql -e "DROP DATABASE IF EXISTS freepbx;" 2>/dev/null || true + log_message "FreePBX-specific databases removed (if they existed)." + else + log_message "MariaDB is not running - no databases to remove." + fi + ;; + 3) + echo -e "${RED}This will delete ALL databases on this system!${NC}" + read -p "Are you absolutely sure? Type 'DELETE ALL' to confirm: " confirm_delete + + if [[ "$confirm_delete" == "DELETE ALL" ]]; then + log_message "Removing ALL databases..." + + if systemctl is-active --quiet mariadb 2>/dev/null; then + systemctl stop mariadb + fi + + rm -rf /var/lib/mysql/* 2>/dev/null || true + log_message "All databases removed." + else + log_message "Database removal cancelled." + fi + ;; + *) + log_message "Keeping all databases (recommended for Proxmox)." + ;; + esac +} + +# Function to clean up package cache and autoremove +final_cleanup() { + log_message "Performing final cleanup..." + echo "FINAL_CLEANUP_START" >> "$LOG_FILE" + + # Unhold packages that might have been held + log_message "Removing package holds..." + echo "REMOVING_PACKAGE_HOLDS" >> "$LOG_FILE" + apt-mark unhold sangoma-pbx17 nodejs node-* freepbx17 2>&1 | tee -a "$LOG_FILE" || true + + # Remove orphaned packages + log_message "Removing orphaned packages..." + echo "REMOVING_ORPHANED_PACKAGES" >> "$LOG_FILE" + apt-get autoremove -y --purge 2>&1 | tee -a "$LOG_FILE" || { + log_error "Failed to remove orphaned packages" + echo "FAILED_ORPHAN_REMOVAL" >> "$LOG_FILE" + } + + # Clean package cache + log_message "Cleaning package cache..." + echo "CLEANING_PACKAGE_CACHE" >> "$LOG_FILE" + apt-get autoclean 2>&1 | tee -a "$LOG_FILE" || { + log_error "Failed to clean package cache" + echo "FAILED_CACHE_CLEAN" >> "$LOG_FILE" + } + + # Update package list + log_message "Updating package list..." + echo "UPDATING_PACKAGE_LIST" >> "$LOG_FILE" + apt-get update 2>&1 | tee -a "$LOG_FILE" || { + log_error "Failed to update package list" + echo "FAILED_PACKAGE_UPDATE" >> "$LOG_FILE" + } + + # Create post-cleanup package list for comparison + log_message "Creating post-cleanup package list..." + dpkg --get-selections > "${BACKUP_DIR}/packages-after-uninstall.txt" 2>&1 | tee -a "$LOG_FILE" || true + apt list --installed > "${BACKUP_DIR}/apt-list-after-uninstall.txt" 2>&1 | tee -a "$LOG_FILE" || true + + log_message "Final cleanup completed." + echo "FINAL_CLEANUP_END" >> "$LOG_FILE" +} + +# Function to restore system state +restore_system_state() { + log_message "Restoring system to original state..." + + # Uncomment CD-ROM repository if it was commented out + if grep -q "^#deb cdrom" /etc/apt/sources.list; then + log_message "Restoring CD-ROM repository..." + sed -i '/^#deb cdrom/s/^#//' /etc/apt/sources.list + fi + + # Reset debconf selections that were set during installation + echo "iptables-persistent iptables-persistent/autosave_v4 boolean false" | debconf-set-selections + echo "iptables-persistent iptables-persistent/autosave_v6 boolean false" | debconf-set-selections + + log_message "System restoration completed." +} + +# Main execution +main() { + # Initialize logging first + init_logging + + log_message "FreePBX 17 Uninstallation Script Started" + log_message "Log file: $LOG_FILE" + log_message "Backup will be created in: $BACKUP_DIR" + + # Check if Proxmox is installed first + check_proxmox + + confirm_action + + # Create backup before making any changes + create_backup + + stop_services + remove_freepbx_packages + remove_other_packages + remove_users_groups + remove_directories + cleanup_configs + remove_repositories + remove_databases + restore_system_state + final_cleanup + + log_message "FreePBX 17 has been removed from the system!" + + if dpkg -l | grep -q "proxmox-ve"; then + echo "" + echo -e "${YELLOW}⚠️ PROXMOX SYSTEM DETECTED ⚠️${NC}" + echo "Please check Proxmox web interface (https://your-ip:8006) to ensure" + echo "it's still working properly after this uninstallation." + echo "" + echo "If you experience issues with Proxmox, you may need to:" + echo "• Restart Proxmox services: systemctl restart pveproxy pvedaemon" + echo "• Reinstall any accidentally removed packages" + echo "• Restore from backup: $BACKUP_DIR" + echo "" + log_message "Proxmox compatibility warnings displayed to user" + fi + + log_message "Consider rebooting the system to ensure all changes take effect." + + echo "" + echo -e "${GREEN}===============================================================================${NC}" + echo -e "${GREEN} FreePBX 17 Uninstallation Complete!${NC}" + echo -e "${GREEN}===============================================================================${NC}" + echo "" + echo "The system has been restored with Proxmox-safe settings." + echo "Some packages may have been preserved to maintain system stability." + echo "" + echo -e "${GREEN}Important files:${NC}" + echo "• Complete log: $LOG_FILE" + echo "• Backup location: $BACKUP_DIR" + echo "• Restore instructions: $BACKUP_DIR/RESTORE_INSTRUCTIONS.txt" + echo "" + echo "Consider rebooting the system: sudo reboot" + echo "" + + # Final log entry + echo "===================================================================================" >> "$LOG_FILE" + echo "FreePBX 17 Uninstallation completed at $(date)" >> "$LOG_FILE" + echo "Backup location: $BACKUP_DIR" >> "$LOG_FILE" + echo "===================================================================================" >> "$LOG_FILE" + + log_message "Uninstallation process completed successfully!" +} + +# Run the main function +main "$@"