#!/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 <&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 </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 <