You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PowerShell/scripts/03-Restore-PwshSshSetup.ps1

83 lines
2.5 KiB

<#
Restaura el entorno desde un backup:
- Perfil
- Historial (opcional)
- Llaves (opcional) + permisos id_rsa
- Re-crea el link ~/.ssh/config -> ssh_config (ruta actual)
#>
param(
[Parameter(Mandatory=$true)]
[string]$BackupPath,
[string]$RepoConfig = "C:\Users\ofgar\Workspace\revelation\ssh_config",
[switch]$RestoreHistory,
[switch]$RestoreKeys
)
$ErrorActionPreference = "Stop"
function Repair-PrivateKeyAcl([string]$KeyPath) {
if (-not (Test-Path -LiteralPath $KeyPath)) { return }
icacls $KeyPath /inheritance:r | Out-Null
icacls $KeyPath /grant:r "$($env:USERNAME):F" | Out-Null
}
# 1) Asegurar módulos
Install-Module PSReadLine -Scope CurrentUser -Force
Install-Module PSFzf -Scope CurrentUser -Force
# 2) Perfil
$profileSrc = Join-Path $BackupPath "Microsoft.PowerShell_profile.ps1"
if (Test-Path -LiteralPath $profileSrc) {
New-Item -ItemType Directory -Force -Path (Split-Path -Parent $PROFILE) | Out-Null
Copy-Item -LiteralPath $profileSrc -Destination $PROFILE -Force
Write-Host "OK: Perfil restaurado"
} else {
Write-Warning "No se encontró perfil en el backup"
}
# 3) Historial (opcional)
if ($RestoreHistory) {
$histDst = (Get-PSReadLineOption).HistorySavePath
$histSrc = Join-Path $BackupPath "ConsoleHost_history.txt"
if (Test-Path -LiteralPath $histSrc) {
New-Item -ItemType Directory -Force -Path (Split-Path -Parent $histDst) | Out-Null
Copy-Item -LiteralPath $histSrc -Destination $histDst -Force
Write-Host "OK: Historial restaurado"
} else {
Write-Warning "No se encontró historial en el backup"
}
}
# 4) Llaves (opcional)
if ($RestoreKeys) {
$sshDir = Join-Path $env:USERPROFILE ".ssh"
New-Item -ItemType Directory -Force -Path $sshDir | Out-Null
foreach ($f in @("id_rsa","id_rsa.pub","known_hosts")) {
$src = Join-Path $BackupPath $f
$dst = Join-Path $sshDir $f
if (Test-Path -LiteralPath $src) {
Copy-Item -LiteralPath $src -Destination $dst -Force
Write-Host "OK: restaurado $f"
} else {
Write-Warning "No existe en backup: $f"
}
}
Repair-PrivateKeyAcl (Join-Path $sshDir "id_rsa")
Write-Host "OK: permisos ajustados id_rsa"
}
# 5) Re-crear link de config (usa script del mismo folder)
$scriptLink = Join-Path $PSScriptRoot "New-SshConfigLink.ps1"
if (-not (Test-Path -LiteralPath $scriptLink)) {
throw "Falta New-SshConfigLink.ps1 en: $PSScriptRoot"
}
& $scriptLink -RepoConfig $RepoConfig
Write-Host ""
Write-Host "Restauración lista. Abre PowerShell nuevo y prueba: ssh / Ctrl+R"