-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevolutions-backup.ps1
More file actions
83 lines (68 loc) · 3.07 KB
/
Copy pathdevolutions-backup.ps1
File metadata and controls
83 lines (68 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Secrets
$envFile = Join-Path $PSScriptRoot '.env'
if (!(Test-Path $envFile)) {
Write-Error "Missing .env file at '$envFile'. Copy .env.example and fill in values." -ErrorAction Stop
}
$secrets = Get-Content $envFile -Raw | ConvertFrom-StringData
$appKey = $secrets.APP_KEY
$appSecret = $secrets.APP_SECRET
$zipPassword = $secrets.ZIP_PASSWORD
# Config
$config = Import-PowerShellDataFile (Join-Path $PSScriptRoot 'config.psd1')
$url = $config.Url
$pathToExportedFolder = $config.PathToExportedFolder
$sevenZipPath = $config.SevenZipPath
$timestamp = Get-Date -Format "yyyy-MM-dd_HHmmss"
$zipOutputPath = Join-Path $config.ZipOutputDir "devolutions_$timestamp.7z"
$sfxOutputPath = Join-Path $config.ZipOutputDir "devolutions_$timestamp.exe"
# Create the folder
New-Item -ItemType Directory -Path $pathToExportedFolder -Force
# Test if folder exists
if (!(Test-Path -Path $pathToExportedFolder)) {
Write-Error -Message "Folder doesn't exist" -ErrorAction Stop
}
# Register PSGallery if not already registered
Register-PSRepository -Default -ErrorAction SilentlyContinue
# Install Devolutions PowerShell Module if not already available
if (-not (Get-Module -ListAvailable -Name Devolutions.PowerShell)) {
Install-Module -Name Devolutions.PowerShell -Force
}
Import-Module Devolutions.PowerShell
# Connect
Connect-HubAccount -Url $url -ApplicationKey $appKey -ApplicationSecret $appSecret
# Export
$vaults = Get-HubVaultsForExport
foreach ($vault in $vaults) {
Write-Host "Exporting vault $($vault.name) ($($vault.id))."
if ($vault.type -eq 'PAM' -or "$($vault.type)" -eq 'PAM') {
Write-Host "PAM Vault can't be exported, skipping."
continue
}
$entries = Get-HubEntriesForExport -VaultId $vault.id
$safeName = $vault.name.Split([IO.Path]::GetInvalidFileNameChars()) -join '_' -replace ' ', '_'
$filepath = Join-Path -Path $pathToExportedFolder -ChildPath "exported-entries-from-$safeName.json"
$entries | Out-File -FilePath $filepath
}
Write-Host "All vaults exported. Creating archives..."
# Verify 7-Zip is available
if (!(Test-Path $sevenZipPath)) {
Write-Error "7-Zip not found at '$sevenZipPath'. Install it or update the path." -ErrorAction Stop
}
# Create password-protected .7z archive with header encryption
& $sevenZipPath a -t7z "-p$zipPassword" -mhe=on "$zipOutputPath" "$pathToExportedFolder\*"
if ($LASTEXITCODE -eq 0) {
Write-Host "Archive created successfully: $zipOutputPath"
} else {
Write-Warning "7-Zip exited with code $LASTEXITCODE - .7z archive may be incomplete."
}
# Create self-extracting archive
& $sevenZipPath a -t7z -sfx "-p$zipPassword" -mhe=on "$sfxOutputPath" "$pathToExportedFolder\*"
if ($LASTEXITCODE -eq 0) {
Write-Host "Archive created successfully: $sfxOutputPath"
} else {
Write-Warning "7-Zip exited with code $LASTEXITCODE - SFX archive may be incomplete."
}
# Always clean up the export folder regardless of result
Write-Host "Cleaning up exported files..."
Remove-Item -Path $pathToExportedFolder -Recurse -Force
Write-Host "Done."