Antes: si HEAD no resolvía (objeto corrupto) o 'git commit' fallaba, el script seguía adelante y 'git push' acababa diciendo 'push OK' sin haber respaldado nada de verdad -- así estuvo una semana en silencio. Ahora aborta con aviso claro en el log en vez de fingir éxito.
42 lines
1.3 KiB
Bash
42 lines
1.3 KiB
Bash
#!/usr/bin/env bash
|
|
# Respalda al cerrar sesión/apagar todo lo que chezmoi ya tiene rastreado.
|
|
# Solo re-sincroniza rutas YA gestionadas (chezmoi re-add), nunca añade
|
|
# rutas nuevas por su cuenta -- eso evita que algo sensible se cuele solo.
|
|
set -uo pipefail
|
|
|
|
LOG="$HOME/.local/share/chezmoi-sync.log"
|
|
mkdir -p "$(dirname "$LOG")"
|
|
exec >>"$LOG" 2>&1
|
|
|
|
echo "=== shutdown-sync $(date -Iseconds) ==="
|
|
|
|
SRC="$HOME/.local/share/chezmoi"
|
|
cd "$SRC" || exit 0
|
|
|
|
# Comprobación de salud antes de tocar nada: un HEAD que no resuelve (objeto
|
|
# corrupto en disco, etc.) hace que git diff/commit fallen en silencio más
|
|
# abajo y el script acababa diciendo "push OK" sin haber respaldado nada.
|
|
if ! git rev-parse --verify --quiet HEAD >/dev/null; then
|
|
echo "!! repo git roto (HEAD no resuelve) -- reparación manual necesaria, no se toca nada"
|
|
exit 1
|
|
fi
|
|
|
|
chezmoi re-add
|
|
git add -A
|
|
|
|
if git diff --cached --quiet; then
|
|
echo "sin cambios que respaldar"
|
|
exit 0
|
|
fi
|
|
|
|
if ! git commit -m "Auto-backup $(date '+%Y-%m-%d %H:%M')"; then
|
|
echo "!! git commit falló -- revisa el repo a mano, los cambios quedan sin respaldar"
|
|
exit 1
|
|
fi
|
|
|
|
if timeout 20 git push origin main; then
|
|
echo "push OK"
|
|
else
|
|
echo "push falló (¿sin red?), el commit queda en local para el próximo intento"
|
|
fi
|