#!/bin/bash
# Une seule session SSH par site : base + code dans ~/yv-tmp/sauv-maj, puis mises à jour par WP-CLI.
d="$1"; D=$(date -u +%Y%m%dT%H%M%SZ); S=~/yv-tmp/sauv-maj; mkdir -p $S; WP="/usr/local/php8.3/bin/php $HOME/yv-outils/wp-cli.phar --path=$HOME/$d"
bash ~/yv-outils/yv-dump.sh $d | gzip > $S/base-$d-$D.sql.gz
zcat $S/base-$d-$D.sql.gz | tail -1 | grep -q "Dump completed" || { echo "ARRÊT : sauvegarde de la base ratée"; exit 1; }
echo "base sauvegardée : $S/base-$d-$D.sql.gz"
nice tar czf $S/code-$d-$D.tar.gz -C ~/$d/wp-content plugins themes mu-plugins || { echo "ARRÊT : sauvegarde du code ratée"; exit 1; }
echo "code sauvegardé : $S/code-$d-$D.tar.gz ($(du -h $S/code-$d-$D.tar.gz | cut -f1))"
EXC=$($WP plugin list --field=name 2>/dev/null | grep -E "broken|-old|old-|bak|backup|copy|-20[0-9]{6}" | paste -sd, -); [ -n "$EXC" ] && echo "écartées : $EXC"
$WP plugin update --all ${EXC:+--exclude=$EXC} 2>&1 | grep -E "^\||Success|Error|Warning" | tail -20
$WP theme update --all 2>&1 | grep -E "Success|Error|Warning" | tail -3
$WP core update --minor 2>&1 | grep -E "Success|Error|Warning" | tail -2
$WP elementor flush-css 2>&1 | tail -1
$WP cache flush 2>&1 | tail -1
