Update phpmyadmin.sh

This commit is contained in:
CanbiZ 2025-06-23 15:41:03 +02:00
parent ab95f6fb50
commit eb5173f03b

View File

@ -5,8 +5,8 @@
# License: MIT # License: MIT
function header_info { function header_info {
clear clear
cat <<"EOF" cat <<"EOF"
____ __ __ ___ ___ __ _ ____ __ __ ___ ___ __ _
/ __ \/ /_ ____ / |/ /_ __/ | ____/ /___ ___ (_)___ / __ \/ /_ ____ / |/ /_ __/ | ____/ /___ ___ (_)___
/ /_/ / __ \/ __ \/ /|_/ / / / / /| |/ __ / __ `__ \/ / __ \ / /_/ / __ \/ __ \/ /|_/ / / / / /| |/ __ / __ `__ \/ / __ \
@ -36,18 +36,18 @@ IP=$(ip -4 addr show "$IFACE" | awk '/inet / {print $2}' | cut -d/ -f1 | head -n
# Detect OS # Detect OS
if [[ -f "/etc/alpine-release" ]]; then if [[ -f "/etc/alpine-release" ]]; then
OS="Alpine" OS="Alpine"
PKG_MANAGER_INSTALL="apk add --no-cache" PKG_MANAGER_INSTALL="apk add --no-cache"
PKG_QUERY="apk info -e" PKG_QUERY="apk info -e"
INSTALL_DIR="$INSTALL_DIR_ALPINE" INSTALL_DIR="$INSTALL_DIR_ALPINE"
elif [[ -f "/etc/debian_version" ]]; then elif [[ -f "/etc/debian_version" ]]; then
OS="Debian" OS="Debian"
PKG_MANAGER_INSTALL="apt-get install -y" PKG_MANAGER_INSTALL="apt-get install -y"
PKG_QUERY="dpkg -l" PKG_QUERY="dpkg -l"
INSTALL_DIR="$INSTALL_DIR_DEBIAN" INSTALL_DIR="$INSTALL_DIR_DEBIAN"
else else
echo -e "${CROSS} Unsupported OS detected. Exiting." echo -e "${CROSS} Unsupported OS detected. Exiting."
exit 1 exit 1
fi fi
header_info header_info
@ -57,93 +57,93 @@ function msg_ok() { echo -e "${CM} ${GN}${1}${CL}"; }
function msg_error() { echo -e "${CROSS} ${RD}${1}${CL}"; } function msg_error() { echo -e "${CROSS} ${RD}${1}${CL}"; }
function check_internet() { function check_internet() {
msg_info "Checking Internet connectivity to GitHub" msg_info "Checking Internet connectivity to GitHub"
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://github.com) HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://github.com)
if [[ "$HTTP_CODE" -ge 200 && "$HTTP_CODE" -lt 400 ]]; then if [[ "$HTTP_CODE" -ge 200 && "$HTTP_CODE" -lt 400 ]]; then
msg_ok "Internet connectivity OK" msg_ok "Internet connectivity OK"
else else
msg_error "Internet connectivity or GitHub unreachable (Status $HTTP_CODE). Exiting." msg_error "Internet connectivity or GitHub unreachable (Status $HTTP_CODE). Exiting."
exit 1 exit 1
fi fi
} }
function is_phpmyadmin_installed() { function is_phpmyadmin_installed() {
if [[ "$OS" == "Debian" ]]; then if [[ "$OS" == "Debian" ]]; then
[[ -f "$INSTALL_DIR/config.inc.php" ]] [[ -f "$INSTALL_DIR/config.inc.php" ]]
else else
[[ -d "$INSTALL_DIR_ALPINE" ]] && rc-service lighttpd status &>/dev/null [[ -d "$INSTALL_DIR_ALPINE" ]] && rc-service lighttpd status &>/dev/null
fi fi
} }
function install_php_and_modules() { function install_php_and_modules() {
msg_info "Checking existing PHP installation" msg_info "Checking existing PHP installation"
if command -v php >/dev/null 2>&1; then if command -v php >/dev/null 2>&1; then
PHP_VERSION=$(php -r 'echo PHP_VERSION;') PHP_VERSION=$(php -r 'echo PHP_VERSION;')
msg_ok "Found PHP version $PHP_VERSION" msg_ok "Found PHP version $PHP_VERSION"
else else
msg_info "PHP not found, will install PHP core" msg_info "PHP not found, will install PHP core"
fi fi
if [[ "$OS" == "Debian" ]]; then if [[ "$OS" == "Debian" ]]; then
PHP_MODULES=("php" "php-mysqli" "php-mbstring" "php-zip" "php-gd" "php-json" "php-curl") PHP_MODULES=("php" "php-mysqli" "php-mbstring" "php-zip" "php-gd" "php-json" "php-curl")
MISSING_PACKAGES=() MISSING_PACKAGES=()
for pkg in "${PHP_MODULES[@]}"; do for pkg in "${PHP_MODULES[@]}"; do
if ! dpkg -l | grep -qw "$pkg"; then if ! dpkg -l | grep -qw "$pkg"; then
MISSING_PACKAGES+=("$pkg") MISSING_PACKAGES+=("$pkg")
fi fi
done done
if [[ ${#MISSING_PACKAGES[@]} -gt 0 ]]; then if [[ ${#MISSING_PACKAGES[@]} -gt 0 ]]; then
msg_info "Installing missing PHP packages: ${MISSING_PACKAGES[*]}" msg_info "Installing missing PHP packages: ${MISSING_PACKAGES[*]}"
if ! apt-get update &>/dev/null || ! apt-get install -y "${MISSING_PACKAGES[@]}" &>/dev/null; then if ! apt-get update &>/dev/null || ! apt-get install -y "${MISSING_PACKAGES[@]}" &>/dev/null; then
msg_error "Failed to install required PHP modules. Exiting." msg_error "Failed to install required PHP modules. Exiting."
exit 1 exit 1
fi fi
msg_ok "Installed missing PHP packages" msg_ok "Installed missing PHP packages"
else
msg_ok "All required PHP modules are already installed"
fi
else else
msg_info "Installing Lighttpd and PHP for Alpine" msg_ok "All required PHP modules are already installed"
$PKG_MANAGER_INSTALL lighttpd php php-fpm php-session php-json php-mysqli curl tar openssl &>/dev/null
msg_ok "Installed Lighttpd and PHP"
fi fi
else
msg_info "Installing Lighttpd and PHP for Alpine"
$PKG_MANAGER_INSTALL lighttpd php php-fpm php-session php-json php-mysqli curl tar openssl &>/dev/null
msg_ok "Installed Lighttpd and PHP"
fi
} }
function install_phpmyadmin() { function install_phpmyadmin() {
msg_info "Fetching latest phpMyAdmin release from GitHub" msg_info "Fetching latest phpMyAdmin release from GitHub"
LATEST_VERSION_RAW=$(curl -s https://api.github.com/repos/phpmyadmin/phpmyadmin/releases/latest | grep tag_name | cut -d '"' -f4) LATEST_VERSION_RAW=$(curl -s https://api.github.com/repos/phpmyadmin/phpmyadmin/releases/latest | grep tag_name | cut -d '"' -f4)
LATEST_VERSION=$(echo "$LATEST_VERSION_RAW" | sed -e 's/^RELEASE_//' -e 's/_/./g') LATEST_VERSION=$(echo "$LATEST_VERSION_RAW" | sed -e 's/^RELEASE_//' -e 's/_/./g')
if [[ -z "$LATEST_VERSION" ]]; then if [[ -z "$LATEST_VERSION" ]]; then
msg_error "Could not determine latest phpMyAdmin version from GitHub falling back to 5.2.2" msg_error "Could not determine latest phpMyAdmin version from GitHub falling back to 5.2.2"
LATEST_VERSION="RELEASE_5_2_2" LATEST_VERSION="RELEASE_5_2_2"
fi fi
msg_ok "Latest version: $LATEST_VERSION" msg_ok "Latest version: $LATEST_VERSION"
TARBALL_URL="https://files.phpmyadmin.net/phpMyAdmin/${LATEST_VERSION}/phpMyAdmin-${LATEST_VERSION}-all-languages.tar.gz" TARBALL_URL="https://files.phpmyadmin.net/phpMyAdmin/${LATEST_VERSION}/phpMyAdmin-${LATEST_VERSION}-all-languages.tar.gz"
msg_info "Downloading ${TARBALL_URL}" msg_info "Downloading ${TARBALL_URL}"
if ! curl -fsSL "$TARBALL_URL" -o /tmp/phpmyadmin.tar.gz; then if ! curl -fsSL "$TARBALL_URL" -o /tmp/phpmyadmin.tar.gz; then
msg_error "Download failed: $TARBALL_URL" msg_error "Download failed: $TARBALL_URL"
exit 1 exit 1
fi fi
mkdir -p "$INSTALL_DIR" mkdir -p "$INSTALL_DIR"
tar xf /tmp/phpmyadmin.tar.gz --strip-components=1 -C "$INSTALL_DIR" tar xf /tmp/phpmyadmin.tar.gz --strip-components=1 -C "$INSTALL_DIR"
} }
function configure_phpmyadmin() { function configure_phpmyadmin() {
if [[ "$OS" == "Debian" ]]; then if [[ "$OS" == "Debian" ]]; then
cp "$INSTALL_DIR/config.sample.inc.php" "$INSTALL_DIR/config.inc.php" cp "$INSTALL_DIR/config.sample.inc.php" "$INSTALL_DIR/config.inc.php"
SECRET=$(openssl rand -base64 24) SECRET=$(openssl rand -base64 24)
sed -i "s#\$cfg\['blowfish_secret'\] = '';#\$cfg['blowfish_secret'] = '${SECRET}';#" "$INSTALL_DIR/config.inc.php" sed -i "s#\$cfg\['blowfish_secret'\] = '';#\$cfg['blowfish_secret'] = '${SECRET}';#" "$INSTALL_DIR/config.inc.php"
chmod 660 "$INSTALL_DIR/config.inc.php" chmod 660 "$INSTALL_DIR/config.inc.php"
chown -R www-data:www-data "$INSTALL_DIR" chown -R www-data:www-data "$INSTALL_DIR"
systemctl restart apache2 systemctl restart apache2
msg_ok "Configured phpMyAdmin with Apache" msg_ok "Configured phpMyAdmin with Apache"
else else
msg_info "Configuring Lighttpd for phpMyAdmin (Alpine detected)" msg_info "Configuring Lighttpd for phpMyAdmin (Alpine detected)"
mkdir -p /etc/lighttpd mkdir -p /etc/lighttpd
cat <<EOF >/etc/lighttpd/lighttpd.conf cat <<EOF >/etc/lighttpd/lighttpd.conf
server.modules = ( server.modules = (
"mod_access", "mod_access",
"mod_alias", "mod_alias",
@ -170,125 +170,125 @@ accesslog.filename = "/var/log/lighttpd/access.log"
server.errorlog = "/var/log/lighttpd/error.log" server.errorlog = "/var/log/lighttpd/error.log"
EOF EOF
msg_info "Starting PHP-FPM and Lighttpd" msg_info "Starting PHP-FPM and Lighttpd"
PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION . PHP_MINOR_VERSION;') PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION . PHP_MINOR_VERSION;')
PHP_FPM_SERVICE="php-fpm${PHP_VERSION}" PHP_FPM_SERVICE="php-fpm${PHP_VERSION}"
if $STD rc-service "$PHP_FPM_SERVICE" start && $STD rc-update add "$PHP_FPM_SERVICE" default; then
msg_ok "Started PHP-FPM service: $PHP_FPM_SERVICE"
else
msg_error "Failed to start PHP-FPM service: $PHP_FPM_SERVICE"
exit 1
fi
$STD rc-service lighttpd start
$STD rc-update add lighttpd default
msg_ok "Configured and started Lighttpd successfully"
if $STD rc-service "$PHP_FPM_SERVICE" start && $STD rc-update add "$PHP_FPM_SERVICE" default; then
msg_ok "Started PHP-FPM service: $PHP_FPM_SERVICE"
else
msg_error "Failed to start PHP-FPM service: $PHP_FPM_SERVICE"
exit 1
fi fi
$STD rc-service lighttpd start
$STD rc-update add lighttpd default
msg_ok "Configured and started Lighttpd successfully"
fi
} }
function uninstall_phpmyadmin() { function uninstall_phpmyadmin() {
msg_info "Stopping Webserver" msg_info "Stopping Webserver"
if [[ "$OS" == "Debian" ]]; then if [[ "$OS" == "Debian" ]]; then
systemctl stop apache2 systemctl stop apache2
else else
$STD rc-service lighttpd stop $STD rc-service lighttpd stop
$STD rc-service php-fpm stop $STD rc-service php-fpm stop
fi fi
msg_info "Removing phpMyAdmin directory" msg_info "Removing phpMyAdmin directory"
rm -rf "$INSTALL_DIR" rm -rf "$INSTALL_DIR"
if [[ "$OS" == "Alpine" ]]; then if [[ "$OS" == "Alpine" ]]; then
msg_info "Removing Lighttpd config" msg_info "Removing Lighttpd config"
rm -f /etc/lighttpd/lighttpd.conf rm -f /etc/lighttpd/lighttpd.conf
$STD rc-service php-fpm restart $STD rc-service php-fpm restart
$STD rc-service lighttpd restart $STD rc-service lighttpd restart
else else
$STD systemctl restart apache2 $STD systemctl restart apache2
fi fi
msg_ok "Uninstalled phpMyAdmin" msg_ok "Uninstalled phpMyAdmin"
} }
function update_phpmyadmin() { function update_phpmyadmin() {
msg_info "Fetching latest phpMyAdmin release from GitHub" msg_info "Fetching latest phpMyAdmin release from GitHub"
LATEST_VERSION_RAW=$(curl -s https://api.github.com/repos/phpmyadmin/phpmyadmin/releases/latest | grep tag_name | cut -d '"' -f4) LATEST_VERSION_RAW=$(curl -s https://api.github.com/repos/phpmyadmin/phpmyadmin/releases/latest | grep tag_name | cut -d '"' -f4)
LATEST_VERSION=$(echo "$LATEST_VERSION_RAW" | sed -e 's/^RELEASE_//' -e 's/_/./g') LATEST_VERSION=$(echo "$LATEST_VERSION_RAW" | sed -e 's/^RELEASE_//' -e 's/_/./g')
if [[ -z "$LATEST_VERSION" ]]; then if [[ -z "$LATEST_VERSION" ]]; then
msg_error "Could not determine latest phpMyAdmin version from GitHub falling back to 5.2.2" msg_error "Could not determine latest phpMyAdmin version from GitHub falling back to 5.2.2"
LATEST_VERSION="5.2.2" LATEST_VERSION="5.2.2"
fi fi
msg_ok "Latest version: $LATEST_VERSION" msg_ok "Latest version: $LATEST_VERSION"
TARBALL_URL="https://files.phpmyadmin.net/phpMyAdmin/${LATEST_VERSION}/phpMyAdmin-${LATEST_VERSION}-all-languages.tar.gz" TARBALL_URL="https://files.phpmyadmin.net/phpMyAdmin/${LATEST_VERSION}/phpMyAdmin-${LATEST_VERSION}-all-languages.tar.gz"
msg_info "Downloading ${TARBALL_URL}" msg_info "Downloading ${TARBALL_URL}"
if ! curl -fsSL "$TARBALL_URL" -o /tmp/phpmyadmin.tar.gz; then if ! curl -fsSL "$TARBALL_URL" -o /tmp/phpmyadmin.tar.gz; then
msg_error "Download failed: $TARBALL_URL" msg_error "Download failed: $TARBALL_URL"
exit 1 exit 1
fi fi
BACKUP_DIR="/tmp/phpmyadmin-backup-$(date +%Y%m%d-%H%M%S)" BACKUP_DIR="/tmp/phpmyadmin-backup-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$BACKUP_DIR" mkdir -p "$BACKUP_DIR"
BACKUP_ITEMS=("config.inc.php" "upload" "save" "tmp" "themes") BACKUP_ITEMS=("config.inc.php" "upload" "save" "tmp" "themes")
msg_info "Backing up existing phpMyAdmin data" msg_info "Backing up existing phpMyAdmin data"
for item in "${BACKUP_ITEMS[@]}"; do for item in "${BACKUP_ITEMS[@]}"; do
[[ -e "$INSTALL_DIR/$item" ]] && cp -a "$INSTALL_DIR/$item" "$BACKUP_DIR/" && echo " ↪︎ $item" [[ -e "$INSTALL_DIR/$item" ]] && cp -a "$INSTALL_DIR/$item" "$BACKUP_DIR/" && echo " ↪︎ $item"
done done
msg_ok "Backup completed: $BACKUP_DIR" msg_ok "Backup completed: $BACKUP_DIR"
tar xf /tmp/phpmyadmin.tar.gz --strip-components=1 -C "$INSTALL_DIR" tar xf /tmp/phpmyadmin.tar.gz --strip-components=1 -C "$INSTALL_DIR"
msg_ok "Extracted phpMyAdmin $LATEST_VERSION" msg_ok "Extracted phpMyAdmin $LATEST_VERSION"
msg_info "Restoring preserved files" msg_info "Restoring preserved files"
for item in "${BACKUP_ITEMS[@]}"; do for item in "${BACKUP_ITEMS[@]}"; do
[[ -e "$BACKUP_DIR/$item" ]] && cp -a "$BACKUP_DIR/$item" "$INSTALL_DIR/" && echo " ↪︎ $item restored" [[ -e "$BACKUP_DIR/$item" ]] && cp -a "$BACKUP_DIR/$item" "$INSTALL_DIR/" && echo " ↪︎ $item restored"
done done
msg_ok "Restoration completed" msg_ok "Restoration completed"
configure_phpmyadmin configure_phpmyadmin
} }
if is_phpmyadmin_installed; then if is_phpmyadmin_installed; then
echo -e "${YW}⚠️ ${APP} is already installed at ${INSTALL_DIR}.${CL}" echo -e "${YW}⚠️ ${APP} is already installed at ${INSTALL_DIR}.${CL}"
read -r -p "Would you like to Update (1), Uninstall (2) or Cancel (3)? [1/2/3]: " action read -r -p "Would you like to Update (1), Uninstall (2) or Cancel (3)? [1/2/3]: " action
action="${action//[[:space:]]/}" # Eingabe bereinigen action="${action//[[:space:]]/}"
case "$action" in case "$action" in
1) 1)
check_internet check_internet
update_phpmyadmin update_phpmyadmin
;; ;;
2) 2)
uninstall_phpmyadmin uninstall_phpmyadmin
;; ;;
3) 3)
echo -e "${YW}⚠️ Action cancelled. Exiting.${CL}" echo -e "${YW}⚠️ Action cancelled. Exiting.${CL}"
exit 0 exit 0
;; ;;
*) *)
echo -e "${YW}⚠️ Invalid input. Exiting.${CL}" echo -e "${YW}⚠️ Invalid input. Exiting.${CL}"
exit 1 exit 1
;; ;;
esac esac
else else
read -r -p "Would you like to install ${APP}? (y/n): " install_prompt read -r -p "Would you like to install ${APP}? (y/n): " install_prompt
install_prompt="${install_prompt//[[:space:]]/}" # Eingabe bereinigen install_prompt="${install_prompt//[[:space:]]/}"
if [[ "${install_prompt,,}" =~ ^(y|yes)$ ]]; then if [[ "${install_prompt,,}" =~ ^(y|yes)$ ]]; then
check_internet check_internet
install_php_and_modules install_php_and_modules
install_phpmyadmin install_phpmyadmin
configure_phpmyadmin configure_phpmyadmin
if [[ "$OS" == "Debian" ]]; then if [[ "$OS" == "Debian" ]]; then
echo -e "${CM} ${GN}${APP} is reachable at: ${BL}http://${IP}/phpMyAdmin${CL}" echo -e "${CM} ${GN}${APP} is reachable at: ${BL}http://${IP}/phpMyAdmin${CL}"
else
echo -e "${CM} ${GN}${APP} is reachable at: ${BL}http://${IP}/${CL}"
fi
else else
echo -e "${YW}⚠️ Installation skipped. Exiting.${CL}" echo -e "${CM} ${GN}${APP} is reachable at: ${BL}http://${IP}/${CL}"
exit 0
fi fi
else
echo -e "${YW}⚠️ Installation skipped. Exiting.${CL}"
exit 0
fi
fi fi