-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackup.ps1
More file actions
504 lines (437 loc) · 20.3 KB
/
Copy pathBackup.ps1
File metadata and controls
504 lines (437 loc) · 20.3 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Define colors
$backgroundColor = [System.Drawing.Color]::FromArgb(245, 246, 247)
$accentColor = [System.Drawing.Color]::FromArgb(0, 120, 212)
$buttonHoverColor = [System.Drawing.Color]::FromArgb(0, 102, 204)
$groupBoxBackColor = [System.Drawing.Color]::White
$textBoxBackColor = [System.Drawing.Color]::White
# Get the script's directory
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path
# Create custom fonts
$defaultFont = New-Object System.Drawing.Font("Segoe UI", 9)
$titleFont = New-Object System.Drawing.Font("Segoe UI Semibold", 9)
# Create the main form
$form = New-Object System.Windows.Forms.Form
$form.Text = "File Backup System"
$form.Size = New-Object System.Drawing.Size(600,830)
$form.StartPosition = "CenterScreen"
$form.BackColor = $backgroundColor
$form.Font = $defaultFont
# Create a custom GroupBox style
function New-GroupBox {
param($title, $location, $size)
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Text = $title
$groupBox.Location = $location
$groupBox.Size = $size
$groupBox.Font = $titleFont
$groupBox.ForeColor = $accentColor
$groupBox.BackColor = $groupBoxBackColor
return $groupBox
}
# Create Users GroupBox
$usersGroupBox = New-GroupBox "Select Users to Backup" (New-Object System.Drawing.Point(10,20)) (New-Object System.Drawing.Size(560,150))
$form.Controls.Add($usersGroupBox)
# Create Users Panel (Scrollable)
$usersPanel = New-Object System.Windows.Forms.Panel
$usersPanel.Location = New-Object System.Drawing.Point(5,20)
$usersPanel.Size = New-Object System.Drawing.Size(530,110)
$usersPanel.AutoScroll = $true
$usersGroupBox.Controls.Add($usersPanel)
# Get list of users from Users directory
$users = Get-ChildItem "C:\Users" | Where-Object {
$_.PSIsContainer -and
$_.Name -notmatch '^(Public|Default|Default User|All Users|desktop.ini)$'
}
# Create checkboxes for each user
$userCheckboxes = @{}
$yPos = 0
foreach ($user in $users) {
$checkbox = New-Object System.Windows.Forms.CheckBox
$checkbox.Location = New-Object System.Drawing.Point(5,$yPos)
$checkbox.Size = New-Object System.Drawing.Size(500,20)
$checkbox.Text = $user.Name
$checkbox.ForeColor = [System.Drawing.Color]::FromArgb(50, 50, 50)
$userCheckboxes[$user.Name] = $checkbox
$usersPanel.Controls.Add($checkbox)
$yPos += 25
}
# Create Folders GroupBox
$foldersGroupBox = New-GroupBox "Select Folders to Backup" (New-Object System.Drawing.Point(10,180)) (New-Object System.Drawing.Size(560,150))
$form.Controls.Add($foldersGroupBox)
# Define folder options
$folderOptions = @{
"Downloads" = "Downloads"
"Documents" = "Documents"
"Desktop" = "Desktop"
"Chrome Data" = "AppData+Local+Google+Chrome+User Data"
"Outlook Data" = "AppData+Local+Microsoft+Outlook"
}
# Create checkboxes for each folder
$folderCheckboxes = @{}
$yPos = 20
foreach ($folder in $folderOptions.GetEnumerator()) {
$checkbox = New-Object System.Windows.Forms.CheckBox
$checkbox.Location = New-Object System.Drawing.Point(10,$yPos)
$checkbox.Size = New-Object System.Drawing.Size(530,20)
$checkbox.Text = $folder.Key
$checkbox.ForeColor = [System.Drawing.Color]::FromArgb(50, 50, 50)
$folderCheckboxes[$folder.Value] = $checkbox
$foldersGroupBox.Controls.Add($checkbox)
$yPos += 25
}
# Create Program Files GroupBox
$programFilesGroupBox = New-GroupBox "Program Files (Requires Administrator Privileges)" (New-Object System.Drawing.Point(10,340)) (New-Object System.Drawing.Size(560,70))
$form.Controls.Add($programFilesGroupBox)
# Define program files options
$programFilesOptions = @{
"Program Files" = "SYSTEM+Program Files"
"Program Files (x86)" = "SYSTEM+Program Files (x86)"
}
# Create checkboxes for program files
$yPos = 20
foreach ($folder in $programFilesOptions.GetEnumerator()) {
$checkbox = New-Object System.Windows.Forms.CheckBox
$checkbox.Location = New-Object System.Drawing.Point(10,$yPos)
$checkbox.Size = New-Object System.Drawing.Size(530,20)
$checkbox.Text = $folder.Key
$checkbox.ForeColor = [System.Drawing.Color]::FromArgb(50, 50, 50)
$folderCheckboxes[$folder.Value] = $checkbox
$programFilesGroupBox.Controls.Add($checkbox)
$yPos += 25
}
# Create a function to check if the script is running with admin rights
function Test-AdminRights {
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object System.Security.Principal.WindowsPrincipal($identity)
return $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
# Function to check if any Program Files folders are selected
function Test-ProgramFilesSelected {
$selectedFolders = $folderCheckboxes.Keys | Where-Object { $folderCheckboxes[$_].Checked }
return $selectedFolders | Where-Object { $_.StartsWith("SYSTEM+") }
}
# Create a global variable to track cancellation
$script:cancelBackup = $false
# Status TextBox
$statusTextBox = New-Object System.Windows.Forms.TextBox
$statusTextBox.Location = New-Object System.Drawing.Point(10,470)
$statusTextBox.Size = New-Object System.Drawing.Size(560,200)
$statusTextBox.Multiline = $true
$statusTextBox.ScrollBars = "Vertical"
$statusTextBox.BackColor = $textBoxBackColor
$statusTextBox.Font = New-Object System.Drawing.Font("Consolas", 9)
$form.Controls.Add($statusTextBox)
# Cancel Button (initially hidden)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Text = "Cancel Backup"
$cancelButton.Location = New-Object System.Drawing.Point(10,680)
$cancelButton.Size = New-Object System.Drawing.Size(560,30)
$cancelButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$cancelButton.BackColor = [System.Drawing.Color]::FromArgb(232, 17, 35)
$cancelButton.ForeColor = [System.Drawing.Color]::White
$cancelButton.Font = $defaultFont
$cancelButton.Cursor = [System.Windows.Forms.Cursors]::Hand
$cancelButton.Visible = $false
$cancelButton.Add_MouseEnter({
$this.BackColor = [System.Drawing.Color]::FromArgb(173, 26, 39)
})
$cancelButton.Add_MouseLeave({
$this.BackColor = [System.Drawing.Color]::FromArgb(232, 17, 35)
})
$cancelButton.Add_Click({
$script:cancelBackup = $true
$statusTextBox.AppendText("`r`nCancelling backup... Please wait while current operations complete.`r`n")
$cancelButton.Enabled = $false
})
$form.Controls.Add($cancelButton)
# Location Label
$locationLabel = New-Object System.Windows.Forms.Label
$locationLabel.Location = New-Object System.Drawing.Point(10,420)
$locationLabel.Size = New-Object System.Drawing.Size(560,40)
$locationLabel.Text = "Backups will be saved in:`r`n$scriptDirectory"
$locationLabel.ForeColor = $accentColor
$form.Controls.Add($locationLabel)
# Create a custom button style
function New-Button {
param($text, $location, $size)
$button = New-Object System.Windows.Forms.Button
$button.Text = $text
$button.Location = $location
$button.Size = $size
$button.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$button.BackColor = $accentColor
$button.ForeColor = [System.Drawing.Color]::White
$button.Font = $defaultFont
$button.Cursor = [System.Windows.Forms.Cursors]::Hand
# Add hover effect
$button.Add_MouseEnter({
$this.BackColor = $buttonHoverColor
})
$button.Add_MouseLeave({
$this.BackColor = $accentColor
})
return $button
}
# Select All Users Button
$selectAllUsersButton = New-Button "Select All Users" (New-Object System.Drawing.Point(10,690)) (New-Object System.Drawing.Size(135,30))
$selectAllUsersButton.Add_Click({
foreach ($checkbox in $userCheckboxes.Values) {
$checkbox.Checked = $true
}
})
$form.Controls.Add($selectAllUsersButton)
# Deselect All Users Button
$deselectAllUsersButton = New-Button "Deselect All Users" (New-Object System.Drawing.Point(150,690)) (New-Object System.Drawing.Size(135,30))
$deselectAllUsersButton.Add_Click({
foreach ($checkbox in $userCheckboxes.Values) {
$checkbox.Checked = $false
}
})
$form.Controls.Add($deselectAllUsersButton)
# Select All Folders Button
$selectAllFoldersButton = New-Button "Select All Folders" (New-Object System.Drawing.Point(295,690)) (New-Object System.Drawing.Size(135,30))
$selectAllFoldersButton.Add_Click({
foreach ($checkbox in $folderCheckboxes.Values) {
$checkbox.Checked = $true
}
})
$form.Controls.Add($selectAllFoldersButton)
# Deselect All Folders Button
$deselectAllFoldersButton = New-Button "Deselect All Folders" (New-Object System.Drawing.Point(435,690)) (New-Object System.Drawing.Size(135,30))
$deselectAllFoldersButton.Add_Click({
foreach ($checkbox in $folderCheckboxes.Values) {
$checkbox.Checked = $false
}
})
$form.Controls.Add($deselectAllFoldersButton)
# Backup Button
$backupButton = New-Button "Start Backup" (New-Object System.Drawing.Point(10,730)) (New-Object System.Drawing.Size(560,30))
$backupButton.Add_Click({
$selectedUsers = $userCheckboxes.Keys | Where-Object { $userCheckboxes[$_].Checked }
$selectedFolders = $folderCheckboxes.Keys | Where-Object { $folderCheckboxes[$_].Checked }
if ($selectedUsers.Count -eq 0) {
[System.Windows.Forms.MessageBox]::Show("Please select at least one user to backup.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
if ($selectedFolders.Count -eq 0) {
[System.Windows.Forms.MessageBox]::Show("Please select at least one folder to backup.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
# Check if Program Files are selected and if the script is running with admin rights
$programFilesSelected = Test-ProgramFilesSelected
if ($programFilesSelected -and -not (Test-AdminRights)) {
$result = [System.Windows.Forms.MessageBox]::Show(
"You have selected Program Files folders, but the application is not running with administrator rights.`r`n`r`nTo backup Program Files, you need to restart the application as administrator.`r`n`r`nDo you want to continue without backing up Program Files?",
"Administrator Rights Required",
[System.Windows.Forms.MessageBoxButtons]::YesNo,
[System.Windows.Forms.MessageBoxIcon]::Warning
)
if ($result -eq [System.Windows.Forms.DialogResult]::No) {
return
}
# Remove Program Files from selected folders
$selectedFolders = $selectedFolders | Where-Object { -not $_.StartsWith("SYSTEM+") }
}
# Reset cancel flag
$script:cancelBackup = $false
# Show cancel button and disable backup button
$cancelButton.Visible = $true
$cancelButton.Enabled = $true
$backupButton.Enabled = $false
# Disable selection buttons during backup
$selectAllUsersButton.Enabled = $false
$deselectAllUsersButton.Enabled = $false
$selectAllFoldersButton.Enabled = $false
$deselectAllFoldersButton.Enabled = $false
# Disable checkboxes during backup
foreach ($checkbox in $userCheckboxes.Values) {
$checkbox.Enabled = $false
}
foreach ($checkbox in $folderCheckboxes.Values) {
$checkbox.Enabled = $false
}
$form.BackColor = $backgroundColor
$statusTextBox.Clear()
$errors = @()
# Create date string for backup folder
$date = Get-Date -Format "yyyy-MM-dd"
foreach ($username in $selectedUsers) {
# Check for cancellation
if ($script:cancelBackup) {
$statusTextBox.AppendText("`r`nBackup cancelled by user.`r`n")
break
}
$statusTextBox.AppendText("`r`nProcessing user: $username`r`n")
# Create backup folder for this user
$backupRoot = Join-Path $scriptDirectory "${username}_${date}"
New-Item -ItemType Directory -Path $backupRoot -Force | Out-Null
# Create a log file for this backup
$logFile = Join-Path $backupRoot "logs.txt"
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"Backup started at $timestamp for user: $username" | Out-File -FilePath $logFile -Append
"Selected folders: $($selectedFolders -join ', ')" | Out-File -FilePath $logFile -Append
"`r`n" | Out-File -FilePath $logFile -Append
foreach ($folderPath in $selectedFolders) {
# Check for cancellation
if ($script:cancelBackup) {
$statusTextBox.AppendText("`r`nBackup cancelled by user.`r`n")
break
}
$sourcePath = ""
$folderName = ""
# Check if this is a system folder (Program Files)
if ($folderPath.StartsWith("SYSTEM+")) {
$actualPath = $folderPath.Substring(7).Replace('+', '\')
$sourcePath = "C:\$actualPath"
# Standardize Program Files folder names
if ($actualPath -eq "Program Files") {
$folderName = "Program+Files"
}
elseif ($actualPath -eq "Program Files (x86)") {
$folderName = "Program+Files+(x86)"
}
else {
$folderName = $actualPath.Replace('\', '+')
}
} else {
# Regular user folder
$folderName = "Users+$username+$folderPath"
$sourcePath = "C:\Users\$username\$($folderPath.Replace('+', '\'))"
}
$destPath = Join-Path $backupRoot $folderName
$statusTextBox.AppendText("Processing $folderName...`r`n")
"Processing $folderName..." | Out-File -FilePath $logFile -Append
if (Test-Path $sourcePath) {
try {
# Create the destination directory
if (-not (Test-Path $destPath)) {
New-Item -ItemType Directory -Path $destPath -Force | Out-Null
}
# Get all items to copy (excluding My Music folder)
$items = Get-ChildItem -Path $sourcePath -Recurse | Where-Object {
$relativePath = $_.FullName.Substring($sourcePath.Length + 1)
-not $relativePath.StartsWith("My Music") -and -not $relativePath.Contains("\My Music\")
}
$totalItems = $items.Count
$processedItems = 0
foreach ($item in $items) {
# Check for cancellation periodically
$processedItems++
if ($processedItems % 50 -eq 0) {
if ($script:cancelBackup) {
throw "Backup cancelled by user"
}
# Update progress
$progress = [math]::Round(($processedItems / $totalItems) * 100)
$statusTextBox.AppendText("Progress: $progress% ($processedItems of $totalItems items)`r")
"Progress: $progress% ($processedItems of $totalItems items)" | Out-File -FilePath $logFile -Append
# Refresh the UI
[System.Windows.Forms.Application]::DoEvents()
}
$relativePath = $item.FullName.Substring($sourcePath.Length + 1)
$targetPath = Join-Path $destPath $relativePath
if ($item.PSIsContainer) {
# Create directory
if (-not (Test-Path $targetPath)) {
New-Item -ItemType Directory -Path $targetPath -Force | Out-Null
}
} else {
# Create parent directory if it doesn't exist
$targetDir = Split-Path -Parent $targetPath
if (-not (Test-Path $targetDir)) {
New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
}
# Copy file
Copy-Item -Path $item.FullName -Destination $targetPath -Force
}
}
$statusTextBox.AppendText("`r`nSuccessfully backed up $folderName (excluding My Music folder)`r`n")
"Successfully backed up $folderName (excluding My Music folder)" | Out-File -FilePath $logFile -Append
}
catch {
if ($script:cancelBackup) {
$statusTextBox.AppendText("`r`nBackup of $folderName was cancelled.`r`n")
"Backup of $folderName was cancelled." | Out-File -FilePath $logFile -Append
} else {
$errors += "$folderName`: $_"
$statusTextBox.AppendText("Error backing up $folderName`: $_`r`n")
"Error backing up $folderName`: $_" | Out-File -FilePath $logFile -Append
}
}
}
else {
$errors += "$folderName`: Source path not found"
$statusTextBox.AppendText("Warning: $folderName path not found`r`n")
"Warning: $folderName path not found" | Out-File -FilePath $logFile -Append
}
}
# Break out of user loop if cancelled
if ($script:cancelBackup) {
break
}
}
# Re-enable UI controls
$backupButton.Enabled = $true
$selectAllUsersButton.Enabled = $true
$deselectAllUsersButton.Enabled = $true
$selectAllFoldersButton.Enabled = $true
$deselectAllFoldersButton.Enabled = $true
foreach ($checkbox in $userCheckboxes.Values) {
$checkbox.Enabled = $true
}
foreach ($checkbox in $folderCheckboxes.Values) {
$checkbox.Enabled = $true
}
# Hide cancel button
$cancelButton.Visible = $false
if ($script:cancelBackup) {
$form.BackColor = [System.Drawing.Color]::FromArgb(255, 242, 204)
$statusTextBox.AppendText("`r`nBackup operation was cancelled by user.`r`n")
# Log the cancellation to all log files
foreach ($username in $selectedUsers) {
$backupRoot = Join-Path $scriptDirectory "${username}_${date}"
$logFile = Join-Path $backupRoot "logs.txt"
if (Test-Path $backupRoot) {
$endTimestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"Backup operation was cancelled by user at $endTimestamp." | Out-File -FilePath $logFile -Append
}
}
}
elseif ($errors.Count -eq 0) {
$form.BackColor = [System.Drawing.Color]::FromArgb(230, 255, 230)
$statusTextBox.AppendText("`r`nBackup completed successfully!`r`n")
# Log success to all log files
foreach ($username in $selectedUsers) {
$backupRoot = Join-Path $scriptDirectory "${username}_${date}"
$logFile = Join-Path $backupRoot "logs.txt"
if (Test-Path $backupRoot) {
$endTimestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"Backup completed successfully at $endTimestamp." | Out-File -FilePath $logFile -Append
}
}
}
else {
$form.BackColor = [System.Drawing.Color]::FromArgb(255, 230, 230)
$statusTextBox.AppendText("`r`nBackup completed with errors:`r`n")
# Log errors to all log files
foreach ($username in $selectedUsers) {
$backupRoot = Join-Path $scriptDirectory "${username}_${date}"
$logFile = Join-Path $backupRoot "logs.txt"
if (Test-Path $backupRoot) {
$endTimestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"Backup completed with errors at $endTimestamp." | Out-File -FilePath $logFile -Append
foreach ($error in $errors) {
"$error" | Out-File -FilePath $logFile -Append
}
}
}
foreach ($error in $errors) {
$statusTextBox.AppendText("$error`r`n")
}
}
})
$form.Controls.Add($backupButton)
# Show the form
$form.ShowDialog()