Add Ubuntu configuration backup and restore scripts

This commit is contained in:
Carlos Tello
2026-09-21 10:17:31 -03:00
parent 9bd3d1052d
commit c9a2e73c61
3 changed files with 427 additions and 0 deletions
+164
View File
@@ -0,0 +1,164 @@
#!/usr/bin/env bash
set -Eeuo pipefail
readonly SCRIPT_NAME="$(basename "$0")"
archive_path=""
confirmed=false
install_packages=false
start_services=false
show_help() {
cat <<EOF
Uso: sudo $SCRIPT_NAME ARCHIVO.tar.gz [opciones]
Restaura configuraciones de un respaldo creado por backup_ubuntu_config.sh.
Por seguridad, sin --yes solo muestra lo que se restauraría.
Opciones:
--yes Ejecuta la restauración.
--install-packages Instala los paquetes marcados como manuales en el respaldo.
--start-services Habilita y arranca los servicios que estaban habilitados.
-h, --help Muestra esta ayuda.
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--yes)
confirmed=true
shift
;;
--install-packages)
install_packages=true
shift
;;
--start-services)
start_services=true
shift
;;
-h|--help)
show_help
exit 0
;;
-*)
echo "Opción desconocida: $1" >&2
show_help >&2
exit 2
;;
*)
if [[ -n "$archive_path" ]]; then
echo "Solo se admite un archivo de respaldo." >&2
exit 2
fi
archive_path="$1"
shift
;;
esac
done
if [[ -z "$archive_path" ]]; then
echo "Debes indicar un archivo .tar.gz." >&2
show_help >&2
exit 2
fi
if [[ "${EUID}" -ne 0 ]]; then
echo "Ejecuta este script con sudo o como root." >&2
exit 1
fi
[[ -f "$archive_path" ]] || { echo "No existe: $archive_path" >&2; exit 1; }
command -v tar >/dev/null || { echo "tar es obligatorio." >&2; exit 1; }
work_dir="$(mktemp -d)"
cleanup() {
rm -rf "$work_dir"
}
trap cleanup EXIT
tar -xzf "$archive_path" -C "$work_dir" --no-same-owner
if [[ ! -d "$work_dir/files" || ! -f "$work_dir/metadata/backup-info.txt" ]]; then
echo "El archivo no parece un respaldo válido de backup_ubuntu_config.sh." >&2
exit 1
fi
printf 'Respaldo: %s\n' "$archive_path"
cat "$work_dir/metadata/backup-info.txt"
printf '\nArchivos de configuración que se restaurarían:\n'
find "$work_dir/files" -mindepth 1 -maxdepth 8 -printf '%P\n' | sort | sed -n '1,200p'
if [[ "$confirmed" != true ]]; then
cat <<EOF
Vista previa solamente. Para aplicar estos cambios:
sudo $SCRIPT_NAME "$archive_path" --yes
Opciones adicionales:
--install-packages Reinstala paquetes APT manuales.
--start-services Habilita y arranca los servicios respaldados.
EOF
exit 0
fi
if [[ "$install_packages" == true && -s "$work_dir/packages/apt-manual.txt" ]]; then
export DEBIAN_FRONTEND=noninteractive
apt-get update
xargs -r apt-get install -y --no-install-recommends < "$work_dir/packages/apt-manual.txt"
fi
if [[ -d "$work_dir/files/etc" ]]; then
cp -a "$work_dir/files/etc/." /etc/
fi
if [[ -d "$work_dir/files/home" ]]; then
cp -a "$work_dir/files/home/." /home/
fi
if [[ -d "$work_dir/files/root" ]]; then
cp -a "$work_dir/files/root/." /root/
fi
if [[ -s "$work_dir/metadata/users.tsv" ]]; then
while IFS=$'\t' read -r username _ _ home; do
[[ -n "$username" && -d "$home" ]] || continue
if id "$username" >/dev/null 2>&1; then
chown -R "$username:$(id -gn "$username")" "$home" 2>/dev/null || true
else
echo "Aviso: no existe el usuario $username; revisa $home manualmente." >&2
fi
done < "$work_dir/metadata/users.tsv"
fi
systemctl daemon-reload
if [[ "$start_services" == true && -s "$work_dir/services/enabled-service-names.txt" ]]; then
while IFS= read -r service; do
[[ -n "$service" ]] || continue
systemctl enable "$service" 2>/dev/null || echo "Aviso: no se pudo habilitar $service" >&2
done < "$work_dir/services/enabled-service-names.txt"
systemctl restart ollama 2>/dev/null || true
systemctl restart litellm 2>/dev/null || true
systemctl restart openclaw-gateway 2>/dev/null || true
fi
if command -v ufw >/dev/null 2>&1; then
ufw --force reload 2>/dev/null || true
fi
cat <<EOF
Restauración completada.
Revisa antes de exponer el servidor:
systemctl --failed
systemctl status ollama litellm
ufw status verbose
nvidia-smi
Los modelos Ollama y el entorno virtual de LiteLLM no forman parte del respaldo.
Debes reinstalarlos o descargarlos por separado.
EOF