Un an après mon article sur l’erreur APT : the following packages have been kept back, voici que cela recommence : impossible d’installer les mises à jour de mysql-client
et mysql-server
parce que mysql-apt-config
n’est plus à jour.
J’ai tendance à oublier la solution parce que je n’y suis confronté que depuis peu donc j’ai écris un petit script bash que je pourrai lancer la prochaine fois, histoire de gagner du temps.
Pré-requis: installation de pup
pup est un outil en ligne de commande pour traiter le HTML. Il lit à partir de stdin, imprime vers stdout, et permet à l’utilisateur de filtrer des parties de la page en utilisant des sélecteurs CSS.
Nous téléchargeons pup
depuis son repo sur Github, on extraie le binaire et on le déplace dans /usr/local/bin/
:
wget https://github.com/ericchiang/pup/releases/download/v0.4.0/pup_v0.4.0_linux_amd64.zip
unzip pup_v0.4.0_linux_amd64.zip
sudo mv pup /usr/local/bin/
Code language: JavaScript (javascript)
Script bash pour mettre à jour MySQL avec mysql-apt-config
Nous créons notre nouveau script, et nous le rendons exécutable:
cd /home/scripts
nano install-latest-mysql-apt.sh
chmod +x install-latest-mysql-apt.sh
Et voici le contenu du script:
#!/bin/bash
# ----------------------------------------- #
# Script Name: install-latest-mysql-apt.sh #
# Author: Matt Biscay #
# URL: https://www.skyminds.net/?p=613967 #
# ----------------------------------------- #
# Color codes
RED=$(tput bold; tput setaf 1)
GREEN=$(tput bold; tput setaf 2)
YELLOW=$(tput bold; tput setaf 3)
MAGENTA=$(tput bold; tput setaf 5)
NC=$(tput sgr0) # No Color
# Function to print the header
function print_header() {
echo -e "${YELLOW}# ----------------------------------------- #${NC}"
echo -e "${YELLOW}# Script Name: install-latest-mysql-apt.sh #${NC}"
echo -e "${YELLOW}# Author: Matt Biscay #${NC}"
echo -e "${YELLOW}# URL: https://www.skyminds.net/?p=613967 #${NC}"
echo -e "${YELLOW}# ----------------------------------------- #${NC}"
}
# Print the header
print_header
# Function to check if the command exists
function check_command() {
command -v "$1" &> /dev/null || {
echo -e "${RED}$1 is not installed. Please install it and try again.${NC}"
exit 1
}
}
# Check if wget, unzip are installed
check_command wget
check_command unzip
# Check if pup is installed
if ! command -v pup &> /dev/null; then
read -p $"${MAGENTA}The \"pup\" tool is not installed, would you like to install it now? (Y/N) ${NC}" INSTALL_PUP
case ${INSTALL_PUP:0:1} in
[Yy]* )
TMP_DIR=$(mktemp -d)
wget https://github.com/ericchiang/pup/releases/download/v0.4.0/pup_v0.4.0_linux_amd64.zip -O "$TMP_DIR/pup.zip"
unzip "$TMP_DIR/pup.zip" -d "$TMP_DIR"
mv "$TMP_DIR/pup" /usr/local/bin/
rm -r "$TMP_DIR"
echo -e "${GREEN}Package pup has been installed.${NC}"
;;
[Nn]* )
echo "Exiting since pup is not installed."
exit 1
;;
* )
echo -e "${RED}Invalid input. Please enter Y or N.${NC}"
exit 1
;;
esac
fi
BASE_URL="https://dev.mysql.com"
INITIAL_URL="${BASE_URL}/downloads/repo/apt/"
# Create temporary files
TMP_INITIAL=$(mktemp)
TMP_SECONDARY=$(mktemp)
echo "Fetching initial page... $INITIAL_URL"
wget -qO- "$INITIAL_URL" > "$TMP_INITIAL"
# Extract the secondary download page URL from the main page
echo "Extracting secondary page URL..."
SECONDARY_URL=$(cat "$TMP_INITIAL" | pup 'div.button03 a attr{href}')
echo "Secondary URL: ${BASE_URL}${SECONDARY_URL}"
# Fetch the secondary page and store its content for debugging
echo "Fetching secondary page..."
wget -qO- "${BASE_URL}${SECONDARY_URL}" > "$TMP_SECONDARY"
# Extract the download link from the secondary page
echo "Extracting download link..."
DOWNLOAD_URL=$(cat "$TMP_SECONDARY" | pup 'a:contains("No thanks, just start my download.") attr{href}')
echo "Download URL: ${BASE_URL}${DOWNLOAD_URL}"
# Ask user if they want to install mysql-apt-config
read -p $"${MAGENTA}Do you want to download and install the package? (Y/N) ${NC}" DOWNLOAD_MYSQL_APT_CONFIG
case ${DOWNLOAD_MYSQL_APT_CONFIG:0:1} in
[Yy]* )
wget "${BASE_URL}${DOWNLOAD_URL}" -O mysql-apt-config.deb
dpkg -i mysql-apt-config.deb
echo -e "${GREEN}Package mysql-apt-config has been updated.${NC}"
read -p $"${MAGENTA}Are you ready to install updates with \"apt update && apt upgrade\"? (Y/N) ${NC}" UPDATE_UPGRADE
case ${UPDATE_UPGRADE:0:1} in
[Yy]* )
apt update && apt upgrade
;;
[Nn]* )
echo "Exiting without installing updates."
;;
* )
echo -e "${RED}Invalid input. Please enter Y or N.${NC}"
;;
esac
;;
[Nn]* )
echo "Exiting without installing."
;;
* )
echo -e "${RED}Invalid input. Please enter Y or N.${NC}"
;;
esac
# Trap to clean up temporary files
trap 'rm -f "$TMP_INITIAL" "$TMP_SECONDARY" mysql-apt-config.deb' EXIT
Code language: PHP (php)
Fonctionnement du script
Le script commence par vérifier que pup
est installé. S’il ne l’est pas, on prompte l’utilisateur pour l’installer:
The "pup" tool is not installed, would you like to install it now? (Y/N) y
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.109.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1718186 (1.6M) [application/octet-stream]
Saving to: ‘/tmp/tmp.4buPXeejfo/pup.zip’
Archive: /tmp/tmp.4buPXeejfo/pup.zip
inflating: /tmp/tmp.4buPXeejfo/pup
Package pup has been installed.
Code language: JavaScript (javascript)
Le script effectue deux requêtes sur le site de MySQL: lors de la première requête, il identifie le lien du bouton “Download”.