Phase 4: Implement backup/restore system with PHP-FPM restart capability
NEW LIBRARY: lib/php-config-manager.sh (14 functions, 442 lines)
BACKUP FUNCTIONS:
- initialize_backup_system() - Creates /root/server-toolkit/backups/php/
- backup_php_config() - Backs up single config file with metadata
- backup_fpm_pool() - Backs up PHP-FPM pool configuration
- backup_user_php_configs() - Backs up ALL PHP configs for a user
- list_backups() - Lists all backups with metadata (date, user, domain, file count)
RESTORE FUNCTIONS:
- restore_php_config() - Restores single config file
- restore_from_backup() - Restores entire backup set
- delete_backup() - Removes old backups
CONFIGURATION MODIFICATION:
- modify_fpm_pool_setting() - Changes single FPM pool setting
- modify_php_ini_setting() - Changes single php.ini setting
- apply_fpm_pool_settings() - Applies multiple settings at once
PHP-FPM MANAGEMENT:
- restart_php_fpm() - Restarts PHP-FPM service (systemd/sysvinit)
- reload_php_fpm() - Graceful reload (no downtime)
- verify_php_fpm_running() - Checks if service is active
MENU OPTIONS B & R IMPLEMENTED:
Option B: Backup Current Configurations
- Select domain to backup
- Backs up all php.ini files (priority 1-4)
- Backs up PHP-FPM pool config
- Creates metadata.txt with timestamp, user, domain
- Preserves directory structure
- Shows list of backed up files
- Backup location: /root/server-toolkit/backups/php/YYYYMMDD_HHMMSS/
Option R: Restore from Backup
- Lists all available backups with details
- Shows: backup name, date, username, domain, file count
- Numbered selection menu
- Confirmation prompt: "This will overwrite current configurations!"
- Requires typing "yes" to proceed
- Restores all files with metadata preservation
- Shows success/failure for each file
- Reminder to restart PHP-FPM
BACKUP STRUCTURE:
/root/server-toolkit/backups/php/
├── 20250102_143045/
│ ├── metadata.txt (backup info)
│ ├── opt/cpanel/ea-php82/root/etc/php-fpm.d/username.conf
│ ├── home/username/.php/8.2/php.ini
│ └── home/username/public_html/.user.ini
└── 20250102_150830/
└── ...
SAFETY FEATURES:
- Metadata tracking (who, what, when)
- Confirmation required for restore
- Non-destructive backups (never overwrites backups)
- Timestamp-based naming (no conflicts)
- Preserves file permissions and ownership
FUTURE USE:
These functions will be used by Phase 5 (apply/action menu) to:
1. Auto-backup before applying changes
2. Rollback if changes cause issues
3. Compare current vs backed up configs
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd ../.. && pwd)"
|
||||
source "$SCRIPT_DIR/lib/php-detector.sh" 2>/dev/null || { echo "ERROR: php-detector.sh not found"; exit 1; }
|
||||
source "$SCRIPT_DIR/lib/php-analyzer.sh" 2>/dev/null || { echo "ERROR: php-analyzer.sh not found"; exit 1; }
|
||||
source "$SCRIPT_DIR/lib/php-config-manager.sh" 2>/dev/null || { echo "ERROR: php-config-manager.sh not found"; exit 1; }
|
||||
source "$SCRIPT_DIR/lib/system-detect.sh" 2>/dev/null || { echo "ERROR: system-detect.sh not found"; exit 1; }
|
||||
source "$SCRIPT_DIR/lib/user-manager.sh" 2>/dev/null || { echo "ERROR: user-manager.sh not found"; exit 1; }
|
||||
|
||||
@@ -862,6 +863,148 @@ check_server_memory_capacity() {
|
||||
read -p "Press Enter to continue..."
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# OPTION B: BACKUP CONFIGURATIONS
|
||||
# ============================================================================
|
||||
|
||||
backup_configurations() {
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}BACKUP PHP CONFIGURATIONS${NC}"
|
||||
echo ""
|
||||
|
||||
# Select domain
|
||||
local selection
|
||||
selection=$(select_domain "backup")
|
||||
|
||||
if [ $? -ne 0 ] || [ -z "$selection" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
local domain username
|
||||
domain=$(echo "$selection" | cut -d'|' -f1)
|
||||
username=$(echo "$selection" | cut -d'|' -f2)
|
||||
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}BACKUP: ${GREEN}$domain${NC}"
|
||||
echo ""
|
||||
|
||||
# Initialize backup system
|
||||
initialize_backup_system
|
||||
|
||||
# Create backup
|
||||
cecho "${YELLOW}Creating backup of PHP configurations...${NC}"
|
||||
echo ""
|
||||
|
||||
local backup_dir
|
||||
backup_dir=$(backup_user_php_configs "$username" "$domain" 2>&1)
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
cecho "${GREEN}${BOLD}✓ Backup created successfully!${NC}"
|
||||
echo ""
|
||||
cecho " Backup location: ${WHITE}$backup_dir${NC}"
|
||||
echo ""
|
||||
|
||||
# Show what was backed up
|
||||
if [ -f "$backup_dir/metadata.txt" ]; then
|
||||
cecho "${CYAN}Files backed up:${NC}"
|
||||
grep "^ /" "$backup_dir/metadata.txt" | while IFS= read -r line; do
|
||||
local file=$(echo "$line" | awk '{print $1}')
|
||||
cecho " ${GREEN}✓${NC} $file"
|
||||
done
|
||||
fi
|
||||
else
|
||||
cecho "${RED}${BOLD}✗ Backup failed${NC}"
|
||||
echo ""
|
||||
echo "$backup_dir"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
read -p "Press Enter to continue..."
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# OPTION R: RESTORE FROM BACKUP
|
||||
# ============================================================================
|
||||
|
||||
restore_configurations() {
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}RESTORE FROM BACKUP${NC}"
|
||||
echo ""
|
||||
|
||||
# List available backups
|
||||
cecho "${CYAN}Available backups:${NC}"
|
||||
echo ""
|
||||
|
||||
local backups
|
||||
backups=$(list_backups)
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
cecho "${YELLOW}No backups found${NC}"
|
||||
echo ""
|
||||
read -p "Press Enter to continue..."
|
||||
return
|
||||
fi
|
||||
|
||||
# Display backups
|
||||
local backup_array=()
|
||||
local index=1
|
||||
|
||||
echo "$backups" | tail -n +2 | while IFS='|' read -r backup_name created username domain file_count; do
|
||||
printf "${GREEN}%3d${NC}) %-20s %s ${CYAN}[%s]${NC} ${YELLOW}(%s)${NC} %s files\n" \
|
||||
"$index" "$backup_name" "$created" "$username" "$domain" "$file_count"
|
||||
backup_array+=("$backup_name")
|
||||
index=$((index + 1))
|
||||
done
|
||||
|
||||
# Store backup names in array for selection
|
||||
mapfile -t backup_array < <(echo "$backups" | tail -n +2 | cut -d'|' -f1)
|
||||
|
||||
echo ""
|
||||
read -p "Select backup number to restore (or 'q' to cancel): " selection
|
||||
|
||||
if [[ "$selection" == "q" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
if ! [[ "$selection" =~ ^[0-9]+$ ]] || [ "$selection" -lt 1 ] || [ "$selection" -gt ${#backup_array[@]} ]; then
|
||||
cecho "${RED}Invalid selection${NC}"
|
||||
sleep 2
|
||||
return
|
||||
fi
|
||||
|
||||
local selected_backup="${backup_array[$((selection - 1))]}"
|
||||
|
||||
# Confirm restoration
|
||||
echo ""
|
||||
cecho "${YELLOW}${BOLD}WARNING: This will overwrite current configurations!${NC}"
|
||||
echo ""
|
||||
read -p "Are you sure you want to restore from $selected_backup? (yes/no): " confirm
|
||||
|
||||
if [[ "$confirm" != "yes" ]]; then
|
||||
cecho "${YELLOW}Restore cancelled${NC}"
|
||||
sleep 2
|
||||
return
|
||||
fi
|
||||
|
||||
# Perform restore
|
||||
show_banner
|
||||
cecho "${WHITE}${BOLD}RESTORING FROM BACKUP${NC}"
|
||||
echo ""
|
||||
|
||||
if restore_from_backup "$selected_backup"; then
|
||||
echo ""
|
||||
cecho "${GREEN}${BOLD}✓ Restore completed successfully!${NC}"
|
||||
echo ""
|
||||
cecho "${YELLOW}Don't forget to restart PHP-FPM for changes to take effect!${NC}"
|
||||
else
|
||||
echo ""
|
||||
cecho "${RED}${BOLD}✗ Restore failed${NC}"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
read -p "Press Enter to continue..."
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# MAIN LOOP
|
||||
# ============================================================================
|
||||
@@ -919,12 +1062,10 @@ main() {
|
||||
check_server_memory_capacity
|
||||
;;
|
||||
b)
|
||||
cecho "${YELLOW}Backup feature not yet implemented${NC}"
|
||||
sleep 2
|
||||
backup_configurations
|
||||
;;
|
||||
r)
|
||||
cecho "${YELLOW}Restore feature not yet implemented${NC}"
|
||||
sleep 2
|
||||
restore_configurations
|
||||
;;
|
||||
q|Q)
|
||||
cecho "${GREEN}Exiting PHP Optimizer...${NC}"
|
||||
|
||||
Reference in New Issue
Block a user