Files
shell_scripts/wp-cli/update_wp.sh
Bruno 21 50039e30ca update_wp.sh
Correction de texte
2018-08-25 19:08:07 +02:00

240 lines
6.4 KiB
Bash
Executable File

#!/usr/bin/env bash
#WordPress updater with WP-CLI.
if [ "$1" == "--help" ]; then
echo -e "\033[93mupdate_wp.sh\033[0m"
echo
echo "Mise à jour et maintenance de WordPress"
echo
echo "Requiert:"
echo " - wp-cli (https://wp-cli.org/)"
echo
echo "Configuration:"
echo -e " - \033[4m\$path\033[0m est le nom du répertoire d'installation (wordpress par défaut)."
echo -e " - \033[4m\$wordpress_path\033[0m est le chemin où est installé WordPress."
echo -e " - \033[4m\$maint\033[0m : mettre sur \033[4mfalse\033[0m pour ne pas exécuter les opérations de maintenance (idem pour toutes les autres options)."
echo
echo "USAGE: update_wp"
echo
echo " --help display this help"
echo
exit 0
fi
# nom du répertoire d'installation
path=wordpress
# chemin où est installé WordPress
wordpress_path="/home/bruno/Sites/"
# Maintenance
maint=true
# Nombre de révisions à conserver:
# Package trepmal/wp-revisions-cli requis !
revisions=3
# Effacer Pingback et Trackback
back=true
# Effacer les spams
removespam=true
# Effacer les révisions
rev=true
# Vider la corbeille
emptytrash=true
# Sauvegarde de la base
backup=true
# Maintenance de la base (optimize & repair)
database=false
# Effacer les transients expirés
transient=true
# Ouvrir la page d'aministration
admin=true
cd $wordpress_path
siteurl=$(wp option get siteurl --quiet --path=$path)
blogname=$(wp option get blogname --path=$path)
admin_email=$(wp option get admin_email --path=$path)
echo -e "\n\033[1mMise-à-jour et maintenance du site $blogname\033[0m"
echo "$siteurl"
echo "Pour toutes questions et problèmes, contacter l'administrateur $admin_email"
echo -e "\n\033[1;31mUpdates...\033[0m"
echo -e "\n\033[1mCore update...\033[0m"
# Vérifier si des MAJ sont disponibles ?
wp core check-update --path=$path
echo -e "\n"
# Mettre à jour WordPress:
wp core update --path=$path
# Mettre à jour la bdd:
wp core update-db --path=$path
echo -e "\n\033[1mPlugins update...\033[0m"
# Vérifier si des MAJ de plugins sont disponibles ?
wp plugin update --all --dry-run --path=$path
echo -e "\n"
# Mettre à jour tous les plugins:
wp plugin update --all --path=$path
echo -e "\n\033[1mThemes update...\033[0m"
# Vérifier si des MAJ de themes sont disponibles ?
wp theme update --all --dry-run --path=$path
echo -e "\n"
# Mettre à jour tous les themes:
wp theme update --all --path=$path
echo -e "\n\033[1mLanguage update...\033[0m"
# Mettre à jour les traductions de WordPress:
wp language core update --path=$path
# Mettre à jour les traductions de tous les plugins:
wp language plugin update --all --path=$path
# Mettre à jour les traductions de tous les themes:
wp language theme update --all --path=$path
# status
echo -e "\n\033[1mPlugins and themes status...\033[0m"
wp plugin status --path=$path
echo
wp theme status --path=$path
# Maintenance
if [ "$maint" = true ]; then
echo -e "\n"
echo -e "\n\033[1;31mMaintenance...\033[0m"
#On récupère la liste des packages installés
packages_list=$(wp package list | sed -n '1!p' | awk '{print $1'})
if [ "$transient" = true ]; then
#echo -e "\n\033[1mSupprimer les transients expirés...\033[0m"
echo -e "\n\033[1mRemove expired transients...\033[0m"
wp transient delete --expired --path=$path
fi
if [ "$database" = true ]; then
#echo -e "\n\033[1mMaintenance bdd...\033[0m"
echo -e "\n\033[1mDatabase maintenance...\033[0m"
wp db optimize --quiet --path=$path
#wp db repair --quiet --path=$path
fi
if [ "$backup" = true ]; then
#echo -e "\n\033[1mSauvegarde bdd...\033[0m"
echo -e "\n\033[1mDatabase backup...\033[0m"
DATE=`date +%Y-%m-%d_%H:%M:%S`
file="wp-$blogname-$DATE.sql"
wp db export "$file" --add-drop-table --path=$path
fi
if [ "$removespam" = true ]; then
#echo -e "\n\033[1mEffacer le SPAM...\033[0m"
echo -e "\n\033[1mRemove spam comments...\033[0m"
spam=$(wp comment list --status=spam --format=ids --path=$path)
if [ -n "$spam" ]; then
wp comment delete "$spam" --path=$path
else
echo "No SPAM"
fi
fi
if [ "$emptytrash" = true ]; then
#echo -e "\n\033[1mEffacer les commentaires supprimés et post supprimés...\033[0m"
echo -e "\n\033[1mRemove comments in trash and comment from deleted posts...\033[0m"
trashed=$(wp comment list --status=trash,post-trashed --format=ids --path=$path)
if [ -n "$trashed" ]; then
wp comment delete "$trashed" --path=$path
else
echo "No trashed post"
fi
fi
#if [ "$rev" = true ]; then
if [[ -n "$(echo "$packages_list" | awk '$1 ~ /trepmal\/wp-revisions-cli/')" ]] && [[ "$rev" = true ]]; then
#echo -e "\n\033[1mGarder seulement $revisions révisions...\033[0m"
echo -e "\n\033[1mKeep only $revisions revisions...\033[0m"
wp revisions clean $revisions --path=$path
elif [ "$rev" = true ]; then
wp package install trepmal/wp-revisions-cli
echo -e "\n\033[1mKeep only $revisions revisions...\033[0m"
wp revisions clean $revisions --path=$path
fi
if [ "$back" = true ]; then
#echo -e "\n\033[1mListe des trackback...\033[0m"
echo -e "\n\033[1mTrackback list...\033[0m"
trackback=$(wp comment list --type=trackback --format=ids --path=$path)
if [ -n "$trackback" ]; then
wp comment delete "$trackback" --path=$path
else
echo "No trackback post"
fi
#echo -e "\n\033[1mListe des pingback...\033[0m"
echo -e "\n\033[1mPingback list...\033[0m"
pingback=$(wp comment list --type=pingback --format=ids --path=$path)
if [ -n "$pingback" ]; then
wp comment delete "$pingback" --path=$path
else
echo "No pingback post"
fi
fi
# Ouvrir le tableau de bord dans un navigateur:
# Nécéssite le paquet admin-command (sera installé si nécessaire)
if [[ -n "$(echo "$packages_list" | awk '$1 ~ /wp-cli\/admin-command/')" ]] && [[ "$admin" = true ]]; then
echo -e "\n\033[1mOpen wp-admin/ page in browser...\033[0m"
wp admin --path=$path
elif [ "$admin" = true ]; then
wp package install wp-cli/admin-command
echo ""
echo -e "\n\033[1mOpen wp-admin/ page in browser...\033[0m"
wp admin --path=$path
fi
fi
echo -e "\n"