-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackup-DeveloperEnvironment.ps1
More file actions
252 lines (217 loc) · 8.32 KB
/
Copy pathBackup-DeveloperEnvironment.ps1
File metadata and controls
252 lines (217 loc) · 8.32 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Backs up developer environment configurations.
.DESCRIPTION
Creates timestamped backup of developer environment configurations including:
- VSCode settings and keybindings
- VSCode extensions list
- Windows Terminal settings
- PowerShell profile
- Git configuration
- SSH configuration
Features:
- Timestamped backup folders
- Manifest file for tracking backed up items
- WhatIf support for preview
- Automatic directory creation
.PARAMETER BackupPath
Destination folder for backup. Default: $env:USERPROFILE\Backups\DevEnv
.PARAMETER IncludeExtensions
Include VSCode extensions list in backup. Default: $true
.PARAMETER WhatIf
Shows what would be backed up without making changes.
.EXAMPLE
.\Backup-DeveloperEnvironment.ps1
Creates backup in default location with timestamp.
.EXAMPLE
.\Backup-DeveloperEnvironment.ps1 -BackupPath D:\Backups\DevEnv
Creates backup in specified location.
.EXAMPLE
.\Backup-DeveloperEnvironment.ps1 -WhatIf
Shows what would be backed up without making changes.
.NOTES
Author: Windows & Linux Sysadmin Toolkit
Version: 1.0.0
Requires: PowerShell 5.1+
.LINK
Restore-DeveloperEnvironment.ps1
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter()]
[string]$BackupPath = "$env:USERPROFILE\Backups\DevEnv",
[Parameter()]
[switch]$IncludeExtensions = $true
)
#Requires -Version 5.1
# Import CommonFunctions
$modulePath = Join-Path $PSScriptRoot "..\lib\CommonFunctions.psm1"
if (Test-Path $modulePath) {
Import-Module $modulePath -Force
}
else {
# Fallback logging functions if module not available
function Write-Success { param($Message) Write-Host "[+] $Message" -ForegroundColor Green }
function Write-InfoMessage { param($Message) Write-Host "[i] $Message" -ForegroundColor Blue }
function Write-WarningMessage { param($Message) Write-Host "[!] $Message" -ForegroundColor Yellow }
function Write-ErrorMessage { param($Message) Write-Host "[-] $Message" -ForegroundColor Red }
}
# ============================================================================
# MAIN LOGIC
# ============================================================================
function Invoke-DeveloperEnvironmentBackup {
[CmdletBinding(SupportsShouldProcess = $true)]
[OutputType([string])]
param(
[Parameter()]
[string]$BackupPath = "$env:USERPROFILE\Backups\DevEnv",
[Parameter()]
[switch]$IncludeExtensions = $true
)
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$backupDir = Join-Path $BackupPath $timestamp
$targets = @(
@{
Name = "VSCode-Settings"
Path = "$env:APPDATA\Code\User\settings.json"
Description = "VSCode user settings"
}
@{
Name = "VSCode-Keybindings"
Path = "$env:APPDATA\Code\User\keybindings.json"
Description = "VSCode keyboard shortcuts"
}
@{
Name = "WindowsTerminal"
Path = "$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
Description = "Windows Terminal settings"
}
@{
Name = "PowerShellProfile"
Path = $PROFILE
Description = "PowerShell profile script"
}
@{
Name = "GitConfig"
Path = "$env:USERPROFILE\.gitconfig"
Description = "Git global configuration"
}
@{
Name = "SSHConfig"
Path = "$env:USERPROFILE\.ssh\config"
Description = "SSH client configuration"
}
)
Write-InfoMessage "Developer Environment Backup"
Write-InfoMessage "Backup location: $backupDir"
Write-Host ""
if ($PSCmdlet.ShouldProcess($backupDir, "Create backup directory")) {
try {
New-Item -ItemType Directory -Path $backupDir -Force | Out-Null
Write-Success "Created backup directory"
}
catch {
Write-ErrorMessage "Failed to create backup directory: $($_.Exception.Message)"
return $null
}
}
$manifest = @{
Timestamp = $timestamp
BackupDate = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
ComputerName = $env:COMPUTERNAME
UserName = $env:USERNAME
Items = @()
}
$successCount = 0
$skipCount = 0
foreach ($target in $targets) {
Write-InfoMessage "Processing: $($target.Description)"
if ($target.Path -and (Test-Path $target.Path)) {
$destFile = Join-Path $backupDir "$($target.Name)$([System.IO.Path]::GetExtension($target.Path))"
if ($PSCmdlet.ShouldProcess($target.Path, "Backup to $destFile")) {
try {
Copy-Item -Path $target.Path -Destination $destFile -Force
$manifest.Items += @{
Name = $target.Name
OriginalPath = $target.Path
BackupFile = $destFile
Description = $target.Description
FileSize = (Get-Item $target.Path).Length
LastModified = (Get-Item $target.Path).LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
}
Write-Success "Backed up: $($target.Name)"
$successCount++
}
catch {
Write-ErrorMessage "Failed to backup $($target.Name): $($_.Exception.Message)"
}
}
}
else {
Write-WarningMessage "Not found: $($target.Name) ($($target.Path))"
$skipCount++
}
}
if ($IncludeExtensions) {
Write-InfoMessage "Processing: VSCode extensions list"
$codeCmd = Get-Command code -ErrorAction SilentlyContinue
if ($codeCmd) {
$destFile = Join-Path $backupDir "VSCode-Extensions.txt"
if ($PSCmdlet.ShouldProcess("VSCode extensions", "Export list to $destFile")) {
try {
$extensions = & code --list-extensions 2>$null
if ($extensions) {
$extensions | Out-File -FilePath $destFile -Encoding UTF8
$manifest.Items += @{
Name = "VSCode-Extensions"
Command = "code --list-extensions"
BackupFile = $destFile
Description = "List of installed VSCode extensions"
ExtensionCount = ($extensions | Measure-Object).Count
}
Write-Success "Saved: VSCode extensions ($(($extensions | Measure-Object).Count) extensions)"
$successCount++
}
else {
Write-WarningMessage "No VSCode extensions found"
$skipCount++
}
}
catch {
Write-ErrorMessage "Failed to export VSCode extensions: $($_.Exception.Message)"
}
}
}
else {
Write-WarningMessage "VSCode CLI (code) not found in PATH"
$skipCount++
}
}
$manifestPath = Join-Path $backupDir "manifest.json"
if ($PSCmdlet.ShouldProcess($manifestPath, "Save backup manifest")) {
try {
$manifest | ConvertTo-Json -Depth 5 | Out-File -FilePath $manifestPath -Encoding UTF8
Write-Success "Saved backup manifest"
}
catch {
Write-ErrorMessage "Failed to save manifest: $($_.Exception.Message)"
}
}
Write-Host ""
Write-InfoMessage "Backup Summary"
Write-Host " Backed up: $successCount items"
Write-Host " Skipped: $skipCount items"
Write-Host " Location: $backupDir"
Write-Host ""
if ($successCount -gt 0) {
Write-Success "Backup complete: $backupDir"
}
else {
Write-WarningMessage "No items were backed up"
}
return $backupDir
}
if ($MyInvocation.InvocationName -ne '.') {
Invoke-DeveloperEnvironmentBackup -BackupPath $BackupPath -IncludeExtensions:$IncludeExtensions
}