File: //bigscoots/cpanel/php_migrate.sh
#!/bin/bash
# php_migrate.sh — PHP version migration helper
# Source server: bash php_migrate.sh --generate-ea4
# Destination server: bash php_migrate.sh --convert-ea4-to-cl
usage() {
echo ""
echo "Usage:"
echo " --generate-ea4 Extract PHP versions from EasyApache 4 (run on source)"
echo " --convert-ea4-to-cl Fetch from source and apply via CloudLinux selector (run on destination)"
echo ""
exit 1
}
generate_ea4() {
OUTPUT="/root/php_versions.csv"
echo "Extracting PHP versions from EasyApache 4..."
echo "username,php_version" > "$OUTPUT"
whmapi1 php_get_vhost_versions 2>/dev/null | awk '
/account:/ { user = $2 }
/main_domain: 1/ { is_main = 1 }
/version:/ {
ver = $2
gsub(/^ea-php/, "", ver)
major = substr(ver, 1, 1)
minor = substr(ver, 2)
phpver = major "." minor
}
/vhost:/ {
if (is_main && user != "" && phpver != "") {
print user "," phpver
is_main = 0
phpver = ""
}
}
' >> "$OUTPUT"
COUNT=$(tail -n +2 "$OUTPUT" | wc -l)
echo "Done — $COUNT accounts written to $OUTPUT"
echo ""
cat "$OUTPUT"
echo ""
echo "Next steps:"
echo " 1. Copy the CSV to your destination server:"
echo " scp $OUTPUT root@DESTINATION_IP:/root/"
echo ""
echo " 2. On the destination server, run:"
echo " bash php_migrate.sh --convert-ea4-to-cl"
}
convert_ea4_to_cl() {
LOG="/root/php_migration.log"
# Prompt for SSH details
read -rp "Source server (e.g. [email protected]): " SSH_TARGET
read -rp "SSH port [22]: " SSH_PORT
SSH_PORT=${SSH_PORT:-22}
echo ""
echo "Connecting to $SSH_TARGET on port $SSH_PORT to fetch PHP versions..."
# Pull CSV data directly from source via SSH
CSV_DATA=$(ssh -p "$SSH_PORT" -o StrictHostKeyChecking=no "$SSH_TARGET" \
"whmapi1 php_get_vhost_versions 2>/dev/null | awk '
/account:/ { user = \$2 }
/main_domain: 1/ { is_main = 1 }
/version:/ {
ver = \$2
gsub(/^ea-php/, \"\", ver)
major = substr(ver, 1, 1)
minor = substr(ver, 2)
phpver = major \".\" minor
}
/vhost:/ {
if (is_main && user != \"\" && phpver != \"\") {
print user \",\" phpver
is_main = 0
phpver = \"\"
}
}'")
if [ -z "$CSV_DATA" ]; then
echo "Error: no data returned from source. Check SSH access and that whmapi1 is available."
exit 1
fi
echo ""
echo "Fetched PHP versions from source:"
echo "username,php_version"
echo "$CSV_DATA"
echo ""
# Build list of available versions from first column of selectorctl -l
AVAILABLE_VERSIONS=$(selectorctl -l 2>/dev/null | awk '{print $1}')
if [ -z "$AVAILABLE_VERSIONS" ]; then
echo "Error: could not retrieve available PHP versions from selectorctl."
exit 1
fi
echo "=== PHP Selector migration $(date) ===" | tee "$LOG"
echo ""
echo "Available PHP versions on this server:" | tee -a "$LOG"
echo "$AVAILABLE_VERSIONS" | tee -a "$LOG"
echo ""
PASS=0; FAIL=0; SKIP=0
while IFS=',' read -r USERNAME PHP_VER; do
[ -z "$USERNAME" ] && continue
if ! id "$USERNAME" &>/dev/null; then
echo "SKIP | $USERNAME | user not found on destination" | tee -a "$LOG"
((SKIP++)); continue
fi
if ! echo "$AVAILABLE_VERSIONS" | grep -qx "$PHP_VER"; then
echo "WARN | $USERNAME | PHP $PHP_VER not available in selector" | tee -a "$LOG"
((FAIL++)); continue
fi
selectorctl --set-user-current="$PHP_VER" --user="$USERNAME" &>/dev/null
RESULT=$(selectorctl --user-current --user="$USERNAME" 2>/dev/null | awk '{print $1}')
if [ "$RESULT" = "$PHP_VER" ]; then
echo "OK | $USERNAME | PHP $RESULT" | tee -a "$LOG"
((PASS++))
else
echo "FAIL | $USERNAME | expected $PHP_VER got $RESULT" | tee -a "$LOG"
((FAIL++))
fi
done <<< "$CSV_DATA"
echo ""
echo "=== Done: $PASS OK, $FAIL failed, $SKIP skipped ===" | tee -a "$LOG"
echo "Full log saved to $LOG"
}
# --- Main ---
case "$1" in
--generate-ea4)
generate_ea4
;;
--convert-ea4-to-cl)
convert_ea4_to_cl
;;
*)
usage
;;
esac